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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help a newbie :-)

Status
Not open for further replies.

jflaurin

Technical User
Dec 1, 2007
12
I'm trying to use the code below to loo thought all the combobox on a Userform and change their .text property but nothing seems to work....

<code>
Private Sub CommandButton1_Click()
Dim cCont As Control

For Each cCont In Me.Controls
If TypeName(cCont) = "ComboBox" Then
'What should i put here ??????
End If
Next cCont

End Sub
</code>
 
Here's some help:
[ol]
[li]Use a good subject line instead of "Help a newbie"[/li]
[li]tell us what application you are using[/li]
[li]proof read your message before submitting[/li]
[li]provide some context to your question so we know what you are attempting to do and why[/li]
[li]if this is Access, don't use the "text" property[/li]
[li]use a naming convention so "Button1" is more descriptive[/li]
[li]use proper TGML ([]s not <>s)[/li]
[/ol]

Duane
Hook'D on Access
MS Access MVP
 

First, in your VBA editor, go to Tools - Options... and on Editor tab check "Require Variable Declaration" checkbox. It is very important that you have it checked - it will put "option Explicit" at the top of your code auto-'magically'.

And to answer your question:

Code:
Dim cCont As Control

For Each cCont In Me.Controls
    If TypeOf cCont Is ComboBox Then
        cCont.Text = "ABC"
    End If
Next cCont

Have fun.

---- Andy
 
Why are you doing this?

Andy's code (although quite correct) will write "ABC" as the text for every combobox, regardless of whether "ABC" is one of the listed items.

My question though is WHY would you want to write the same thing as a user input into all comboboxes when the text you are writing is NOT one of the listed items? Seems odd to me.

I would also agree that it is better to use TypeOf (with properly named controls), rather than TypeName and default naming. This goes for using "Button" as well.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top