Solved

Syntax to loop through Textboxes on a Form?

Posted on 2000-04-24
15
316 Views
Last Modified: 2006-11-17
Please correct this syntax:

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

Thank you,

David

0
Comment
Question by:DavidWare
  • 5
  • 3
  • 3
  • +3
15 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 2744002
You will have to loop through the control's of the form and test for the controltype = textbox to initialize them.
0
 
LVL 3

Expert Comment

by:PunisherDude
ID: 2744022
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
 

Author Comment

by:DavidWare
ID: 2744079
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
 
LVL 3

Expert Comment

by:PunisherDude
ID: 2744256
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
 
LVL 14

Accepted Solution

by:
mgrattan earned 50 total points
ID: 2744279
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
 
LVL 10

Expert Comment

by:paasky
ID: 2744334
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
 
LVL 10

Expert Comment

by:paasky
ID: 2744344
mgrattan, almost like yours, but I used .value property instead of .text which requires .setfocus before assigning data. :o)
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 54

Expert Comment

by:nico5038
ID: 2744382
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
 
LVL 9

Expert Comment

by:BrianWren
ID: 2744416
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
 
LVL 10

Expert Comment

by:paasky
ID: 2744624
nico5038, this will also work (my code originally looked like this):

For Each ctl In Me.Controls
....
0
 
LVL 54

Expert Comment

by:nico5038
ID: 2745533
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
 

Author Comment

by:DavidWare
ID: 2747244
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
 

Author Comment

by:DavidWare
ID: 2747267
oh, you too, nico5038!
0
 

Author Comment

by:DavidWare
ID: 2780940
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
 

Author Comment

by:DavidWare
ID: 2840237
PunisherDude,
   Wake Up!  You got Points! (read above)
David
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now