For each

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

Hi,

Can someone assist with helping me with code to select each text box one a
form and get the name of them.

Adapting other sample data I have I believe it is something like....

For Each tb In CurrentDb.?????
The_Current_Textbox = tb.?????
Some_Code.....
Next tb
 
Just replace the message box in this code with whatever you want:

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
MsgBox ctl.Name
End If
Next ctl


--
Bob Larson
Access World Forums Super Moderator
Utter Access VIP
Tutorials at http://www.btabdevelopment.com
If my post was helpful to you, please rate the post.
__________________________________
 
Hi Andrew,

Here's something to serve as a basis for what you ask.

Public Function ListTextBoxNames()
Dim aobj As AccessObject
Dim frm As Form
Dim ctl As Control

'loop through all forms
For Each aobj In CurrentProject.AllForms
DoCmd.OpenForm aobj.Name, acDesign, , , , acHidden
Set frm = Forms(aobj.Name)
Debug.Print "TextBox Controls on Form: " & frm.Name
'loop through all controls
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Then
Debug.Print " " & ctl.Name
End If
Next ctl
Set ctl = Nothing
DoCmd.Close acForm, aobj.Name
Next aobj
Set frm = Nothing
Set aobj = Nothing
End Function

Note that there is no error-trapping on this routine. Depending on what you
are wanting to do, you may need to add that. Also note that this will close
any form that is open when it is run. If you are wanting to change some
design aspect of your form(s), you will need to add the parameter to save
you changes to the close form line, thus:
DoCmd.Close acForm, aobj.Name, acSaveYes

HTH,

Rob
 
Hi,

Can someone assist with helping me with code to select each text box one a
form and get the name of them.

Adapting other sample data I have I believe it is something like....

For Each tb In CurrentDb.?????
The_Current_Textbox = tb.?????
Some_Code.....
Next tb

If you're looking for textboxes in Tables you're on the wrong track. Tables
don't HAVE textboxes! To you mean Forms?

What exactly are you trying to accomplish? You can use the database's
Containers collection, a collection of Documents (objects, really) to loop
through the Tables or Forms, and then within each of those loop through its
Fields (for tables) or Controls (for forms) collections... see the online help
for Documents in the VBA editor.
 
Hi John,

Thanks for your reply. Bob Larson's reply was basically exactly what I
needed. Rob's was good as well just a little overkill for my little needs.
 
Hi Rob,

Thanks for your reply. I've used Bob's reply as that is all I needed to do
but I've copied your reply into my 'How To' examples.
--
Andrew


Rob Parker said:
Hi Andrew,

Here's something to serve as a basis for what you ask.

Public Function ListTextBoxNames()
Dim aobj As AccessObject
Dim frm As Form
Dim ctl As Control

'loop through all forms
For Each aobj In CurrentProject.AllForms
DoCmd.OpenForm aobj.Name, acDesign, , , , acHidden
Set frm = Forms(aobj.Name)
Debug.Print "TextBox Controls on Form: " & frm.Name
'loop through all controls
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Then
Debug.Print " " & ctl.Name
End If
Next ctl
Set ctl = Nothing
DoCmd.Close acForm, aobj.Name
Next aobj
Set frm = Nothing
Set aobj = Nothing
End Function

Note that there is no error-trapping on this routine. Depending on what you
are wanting to do, you may need to add that. Also note that this will close
any form that is open when it is run. If you are wanting to change some
design aspect of your form(s), you will need to add the parameter to save
you changes to the close form line, thus:
DoCmd.Close acForm, aobj.Name, acSaveYes

HTH,

Rob
 
Thanks for your reply. Bob Larson's reply was basically exactly what I
needed.

Well... it's not what you asked for <g>. It will get all controls in the
current Form (not the current database, which you implied).
 
I read it as just on one form since it looked to me like a typo when typing:

"Can someone assist with helping me with code to select each text box one a
form and get the name of them. "

instead of ONE A FORM, I read it as ON A FORM, which would make me believe
that it was only looking for the text boxes on a single form.

--

Bob Larson
Access World Forums Super Moderator
Utter Access VIP
Tutorials at http://www.btabdevelopment.com
If my post was helpful to you, please rate the post.
__________________________________
 
Back
Top