• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Syntax to loop through Textboxes on a Form?

Please correct this syntax:

Dim MyTextbox As Textbox
For Each MyTextbox In [Forms]![MyForm]
     .Text = ""
Next

Thank you,

David

0
DavidWare
Asked:
DavidWare
  • 5
  • 3
  • 3
  • +3
1 Solution
 
nico5038Commented:
You will have to loop through the control's of the form and test for the controltype = textbox to initialize them.
0
 
PunisherDudeCommented:
I did this for one of my reports.  Just assing the Tag property of the text boxes to 1

Dim MyObjects As Object

For Each MyObjects In Me.Detail.Controls
  If MyObjects.Tag = 1 Then
    MyObjects.Text = "" 
  End If
Next
0
 
DavidWareAuthor Commented:
PunisherDude,

Thanks for that code.

I mistakenly told you they were textboxes - they are comboboxes.

I get a type mismatch on the line:

If MyObjects.Tag = 1 Then

What number is a combobox (and where is that listed?  I looked at the Tag property in Help, but it isn't there)?

David
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
PunisherDudeCommented:
Oh, it doesn't matter what you use for a tag. That's up to you.

It's probably because not all object don't have a tag.  Use a resume next error or something like that.
0
 
mgrattanCommented:
Dim ctl As Control

For Each ctl In Forms!MyForm.Controls
     If ctl.ControlType = acComboBox Then
        ctl.SetFocus
        ctl.Text = ""
    End If
Next ctl

PunisherDude's code has possibilities if you set the Tag property of all combo boxes to "1" and reference the Tag property in his code as a text value--i.e., test for "1" instead of 1.

0
 
paaskyCommented:
Hello DavidWare,

Dim ctl As Control
   
    For Each ctl In Forms("MyForm").Controls
        If ctl.ControlType = acComboBox Then
            ctl.Value = ""
        End If
    Next ctl

Regards,
Paasky
0
 
paaskyCommented:
mgrattan, almost like yours, but I used .value property instead of .text which requires .setfocus before assigning data. :o)
0
 
nico5038Commented:
OK if all of you wanna give code samples just one with another addition:

Dim ctl As Control
     
    For Each ctl In Forms(Me.Name).Controls
        If ctl.ControlType = acComboBox Then
            ctl.Value = "" 
        End If
    Next ctl

This "Me.Name" makes the code working for every form without changing it.
0
 
BrianWrenCommented:
I was very surprised to find that

 If TypeOf ctl Is CommandButton Then

didn't work.

I came up with almost the same thing:

     Dim i As Integer

     For i = 0 To Me.Count - 1
          If Me(i).ControlType = acComboBox Then
                Me(i) = ""
          End If
     Next i


..Value is the default.

I prefer the

     For Each ctl In Me.Controls
   
   
Brian
            
0
 
paaskyCommented:
nico5038, this will also work (my code originally looked like this):

For Each ctl In Me.Controls
....
0
 
nico5038Commented:
Paasky,

I hope you won't take this agains ME. :-)

So we can all agree on:

Dim ctl As Control
     
    For Each ctl In Me.Controls
        If ctl.ControlType = acComboBox Then
            ctl.Value = "" 
        End If
    Next ctl

To have a piece of re-usable code !
Nice cooperation guy's !

However, somewhere it's agains my principle to just give code. As I think that DavidWare is best served when he tries to write this loop himself, thus encountering the problems and learning from them. It makes it easier the next time!
OK that's the teacher (preacher?) part in me, can't help it's just there!

In each case I learned about the .value setting from you paasky so it was an interesting question for me!

Nico
0
 
DavidWareAuthor Commented:
Thanks to everybody for their input.  mgrattan's code worked first, but I appreciate all effort!  I did other things in the loop (basically looked at the several comboboxes and tested for certain values) so the actual code is different than your comment, but I needed the syntax to do the loop right.  I really learned alot about it - thank you all.

PunisherDude, paasky, BrianWren - please look for 25 points each for your effort.

David
0
 
DavidWareAuthor Commented:
oh, you too, nico5038!
0
 
DavidWareAuthor Commented:
Hey PunisherDude,

There are points for your help in this question at this link.

David

http://www.experts-exchange.com/jsp/qShow.jsp?ta=msaccess&qid=10333575 
0
 
DavidWareAuthor Commented:
PunisherDude,
   Wake Up!  You got Points! (read above)
David
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now