Solved

Syntax to loop through Textboxes on a Form?

Posted on 2000-04-24
15
323 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
Back Up Your Microsoft Windows Server®

Back up 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 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
 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

803 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