Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find all TextBox controls on a form?

Status
Not open for further replies.

Robeen

Programmer
Mar 25, 2003
38
0
0
US
I want to write a 'Clear All' function that can be called from anywhere in my application that will Loop through all the controls on the Form from which the function is called, identify which ones are [for instance] TextBoxes, ListBoxes & ComboBoxes and then clear their contents - setting them to nothing or "" . . .
I've been searching the VB Help for 'control type', 'object type' 'type' 'controltype property' 'collections' - no luck.
I've seen this done in VB - but it was a long time ago & I just can't think of any other keywords to search for.
I'd appreciate any pointers.
Thanks!
Robeen
 
Typeof is what you're looking for

As in:[tt]
Dim X As Control
For Each X In Me.Controls
If TypeOf X Is TextBox Then
X.Text = ""
End If
[/tt]


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Dim MyForm As Form, MyControl As Control
For Each MyForm In Forms
For Each MyControl In MyForm.Controls
If TypeOf MyControl Is TextBox Then
MyControl.Text = ""
ElseIf TypeOf MyControl Is ComboBox Then
MyControl.ListIndex = -1
ElseIf TypeOf MyControl Is ListBox Then
MyControl.ListIndex = -1
End If

Next MyControl
Next MyForm
 
Thank you so much, guys!
I'll give your suggestoins a try next week!
I appreciate your help. I could have looked for a long time in the Help Files and not found the answer.
I sometimes wonder if I'm alone in feeling that the Microsoft VB Help Files are often difficult to extract help from.
Of course it could be that I don't do much programming in VB . . . [but that's when you most NEED Help - isn't it?]
I feel like if I was an expert the Help files would be a lot more friendly!
:)
Robeen
 
>I'm alone in feeling that the Microsoft VB Help Files are often difficult to extract help from

I think it is fair to say that the current documentation often seems to expect that you already know what it is your are looking for before you can find it (er...if that makes any sense)
 
>I think it is fair to say that the current documentation often seems to expect that you already know what it is your are looking for before you can find it (er...if that makes any sense)

If you didn't know what you were looking for, would you know if you'd found it? [poke]

I think it is fair to point out, "you get the best answers by asking the best questions" is a truism for VB help too!



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I think it is fair to say that the current documentation often seems to expect that you already know what it is your are looking for before you can find it (er...if that makes any sense)

Kind of like looking up a word in a dictionary to see how its spelled.


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Whenever I'm going to use a "group" of textboxes to display
information - like information from a particular record in
a recordset - I prefer to create a control array. That makes it easier to maniputate them as a group.
If I have an array of TextBoxes named txText
Then I can use something like this:

Code:
Private Sub ClearBoxes
 Dim ele as Variant
 
 For each ele in txText
   ele.Text = ""
 Next
End Sub

Later if I add 10 new TextBoxes to that group, I don't
need to change the code that clears them
 
Here's what I came up based on DrJava's code, but the debugger stops when I get to a memo field. What do I call that?

Code:
Private Function ClearForm()
Dim MyForm As Form, MyControl As Control
   For Each MyForm In Forms
      For Each MyControl In MyForm.Controls
         If TypeOf MyControl Is TextBox Then
            MyControl.SetFocus
            MyControl.Text = ""
         ElseIf TypeOf MyControl Is ComboBox Then
            MyControl.SetFocus
            MyControl.ListIndex = -1
         ElseIf TypeOf MyControl Is ListBox Then
            MyControl.SetFocus
            MyControl.ListIndex = -1
         End If
    Next MyControl
   Next MyForm
End Function
[code]

Judge Hopkins

[URL unfurl="true"]http://www.psc.state.mo.us/adjudication.asp[/URL]

There are only two rules for success: (1) Never tell everything you know.
 
Sorry; I changed a typo [red](in red)[/red] but I still get an "object required" debbugger error when I get to that line.

Private Function ClearForm()
Dim MyForm As Form, MyControl As Control
For Each MyForm In Forms
For Each MyControl In MyForm.Controls
If TypeOf MyControl Is TextBox Then
MyControl.SetFocus
MyControl.Text = ""
[red] ElseIf MyControl Is ComboBox Then[/red]
MyControl.SetFocus
MyControl.ListIndex = -1
ElseIf TypeOf MyControl Is ListBox Then
MyControl.SetFocus
MyControl.ListIndex = -1
End If
Next MyControl
Next MyForm
End Function


Judge Hopkins


There are only two rules for success: (1) Never tell everything you know.
 
I ran your first code and it works ok. The second it looks like the only difference is that you left out the TypeOf statement, put it in and it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top