By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,841 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,841 IT Pros & Developers. It's quick & easy.

Cycling through textboxes in a form

P: 5
Hey everyone,
I'm having a little trouble figuring out how to do what I'm guessing is a simple procedure. I want to cycle through every textbox I have on a form and if the name of the textbox includes the word "Account" then I want to gather the information in that textbox. Unfourtunatly I can't figure out the base code for how to loop through all textboxes/comboboxes/checkboxes or anything. Any advice?
Oh, this is in access 2000 VBA on windows XP.
thanks.
Dec 27 '06 #1
Share this Question
Share on Google+
14 Replies


ADezii
Expert 5K+
P: 8,669
Hey everyone,
I'm having a little trouble figuring out how to do what I'm guessing is a simple procedure. I want to cycle through every textbox I have on a form and if the name of the textbox includes the word "Account" then I want to gather the information in that textbox. Unfourtunatly I can't figure out the base code for how to loop through all textboxes/comboboxes/checkboxes or anything. Any advice?
Oh, this is in access 2000 VBA on windows XP.
thanks.
Expand|Select|Wrap|Line Numbers
  1. Dim myCtl As Control
  2.  
  3. 'Loop through each Control in the Current Form
  4. For Each myCtl In Me.Controls
  5.   If myCtl.ControlType = acTextBox Then     'Control is a Text Box
  6.     If myCtl.Name Like "*" & "Account" & "*" Then   'Is the Name like *Account*
  7.       'Procesing code here
  8.     End If
  9.   End If
  10. Next
Dec 27 '06 #2

Expert 5K+
P: 8,434
Hey everyone,
I'm having a little trouble figuring out how to do what I'm guessing is a simple procedure. I want to cycle through every textbox I have on a form and if the name of the textbox includes the word "Account" then I want to gather the information in that textbox. Unfourtunatly I can't figure out the base code for how to loop through all textboxes/comboboxes/checkboxes or anything. Any advice?
Oh, this is in access 2000 VBA on windows XP.
thanks.
This may need adjustment for Access, but in VB6 you could do something along these lines (untested)...
Expand|Select|Wrap|Line Numbers
  1. Dim ctl as Control
  2. Dim SearchFor As String
  3. SearchFor = "Account"
  4. For Each ctl In Me.Controls
  5.   If TypeOf ctl Is TextBox Then
  6.     If Instr(ctl.Name, SearchFor Then
  7.       ' Do your thing here.
  8.     End If
  9.   End If
  10. Next
  11.  
Dec 27 '06 #3

Expert 5K+
P: 8,434
Expand|Select|Wrap|Line Numbers
  1. Dim myCtl As Control
  2. ...
  3.  
Damn, you beat me to it! :)

I really prefer the Instr( ) over Like, though. Makes for simpler code, in my opinion. But that's just personal preference, I guess.
Dec 27 '06 #4

missinglinq
Expert 2.5K+
P: 3,532
And you forgot your closing parenthesis, Killer!

If Instr(ctl.Name, SearchFor) Then
Dec 27 '06 #5

100+
P: 1,646
Damn, you beat me to it! :)

I really prefer the Instr( ) over Like, though. Makes for simpler code, in my opinion. But that's just personal preference, I guess.
Ah yes, Killer. Always the bridesmaid, never the bride :)
Dec 27 '06 #6

missinglinq
Expert 2.5K+
P: 3,532
But we know who gets the most action at the wedding, don't we Killer!
Dec 27 '06 #7

Expert 5K+
P: 8,434
And you forgot your closing parenthesis, Killer!

If Instr(ctl.Name, SearchFor) Then
Oh, nuts! I'm usually on the lookout for that kind of detail. Of course, it's a lot easier to spot in someone else's code than your own.
Dec 27 '06 #8

Expert 5K+
P: 8,434
But we know who gets the most action at the wedding, don't we Killer!
I'm not touching that one. :D
Dec 27 '06 #9

P: 5
This may need adjustment for Access, but in VB6 you could do something along these lines (untested)...
Expand|Select|Wrap|Line Numbers
  1. Dim ctl as Control
  2. Dim SearchFor As String
  3. SearchFor = "Account"
  4. For Each ctl In Me.Controls
  5.   If TypeOf ctl Is TextBox Then
  6.     If Instr(ctl.Name, SearchFor Then
  7.       ' Do your thing here.
  8.     End If
  9.   End If
  10. Next
  11.  
thanks alot, that got me going. My next question is, I have a loop going to gather info from all the combos on a form. I've got two columns of combo boxes, one column is all names of types of doughnuts. The second column is amounts of the doughnut that is in the combo box next to it. I've named each combo box in the columns either "cmbDoughnutName1" to "cmbDoughnutName12" or "cmbAmount1" to "cmbAmount12".

for k=1 to 12
'need code for here
next k

what I want in that spot is code that will grab the name and amount of the doughnut for each time it goes through the loop on the first time it would grab cmbDoughnutName1 and cmbAmount1. Do I need to loop through all the combo boxes each time to find the values or is there a way to find a combobox on a form with instr(combobox,"Amount"+str(k)) sort of thing quickly. Hope this makes sense.
thanks,
Lobster
Dec 28 '06 #10

Expert 5K+
P: 8,434
Hi.

Sorry, I only have a moment (should be able to get back here tonight or tomorrow) but briefly, you'll find it a lot easier if you put the combo boxes in two control arrays. That way, rather than cmbDoughnutName1, cmbDoughnutName2 and so on, you can refer to cmbDoughnutName(number). It's much easier to work with an array.
Dec 28 '06 #11

NeoPa
Expert Mod 15k+
P: 31,660
You could do a For Each ... Next for processing through them (ignore where name not Like "cmbDoughnut*") and build name of cmbAmount? from the Doughnut name.
This will avoid the necessity of building up the arrays outside of the loop.
Just a thought...
Dec 28 '06 #12

Expert 5K+
P: 8,434
You could do a For Each ... Next for processing through them (ignore where name not Like "cmbDoughnut*") and build name of cmbAmount? from the Doughnut name.
This will avoid the necessity of building up the arrays outside of the loop.
Just a thought...
Sorry, I keep forgetting - Access doesn't do control arrays, does it?
Dec 29 '06 #13

100+
P: 1,646
Sorry, I keep forgetting - Access doesn't do control arrays, does it?
No but it can be simulated
Dec 29 '06 #14

P: 5
Thanks everyone, I think I got it. Take a five year break from programming and you forget the simplest things.
Dec 29 '06 #15

Post your reply

Sign in to post your reply or Sign up for a free account.