Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Looping Through Form Fields
Rick R 
      
15 years ago
Here's a good question I received from a student, asking about looping through fields on a form...



I have a form fixed with 9 rows(textbox) and 3 columns(textbox) . The users have a choice of entering in the text box from 1 to  9. Instead of hard code all of the 9 rows. Can I use the loop to insert all the rows into certain table??

test1 = Forms![New Contracts]![Bid Group F].Form![Contract1].Value
test2 = Forms![New Contracts]![Bid Group F].Form![IssueBy1].Value
test3 = Forms![New Contracts]![Bid Group F].Form![supplypartner1].Value   
 
  DoCmd.RunSQL "INSERT INTO [Contract All2] ( contract, [supplier partner], [Requested By] ) " & _
  "Values( '" & test1 & "' ,  '" & test2 & "' , '" & test3 & "' ); "



My Reply...

Well, my first thought is "why are you not using a SUBFORM?" This way you're not limited to just 9 items AND you could easily loop through all of the records. You could just run through a loop with either a Recordset or using GoToRecord commands.

However, YES, you could create a loop based on the text box names... I don't think I cover it in my classes, but you could say something like:

For X = 1 to 9
  TextBoxName = "Contract" & X
  TextBoxValue = Me(TextBoxName)
  Docmd.RunSQL ' put your INSERT command here
Next X

See what this does? Instead of having to refer to a form field as:

  Me!FirstName

You can refer to it as:

  Me("FirstName")

This is extremely handy if you want to dynamically change the NAME of the field you're looking for in your code.

Give it a try and let me know if this helps.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/17/2026 9:54:30 AM. PLT: 1s