Solved

Syntax to loop through Textboxes on a Form?

Posted on 2000-04-24
15
306 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

744 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

11 Experts available now in Live!

Get 1:1 Help Now