Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Syntax to loop through Textboxes on a Form?

Posted on 2000-04-24
15
329 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

765 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