Combo box controlling visibility

C

chis

I have a user form in VB that contains multiple
comboboxes. I am looking to haave the first combobox
control the Visible property of the other boxes. any
help is appreciated
 
T

Tom Ogilvy

Private Sub combobox1_Click()
dim cbox = MSForms.Combobox
for each ctrl in Userform1.Controls
if typeof ctrl is MsForms.Combobox then
set cbox = ctrl
if right(cbox.name,1) <> 1 then
cbox.visible = False
end if
end if
Next
Controls("Combobox" & combobox1.Listindex + 2).Visible = True
Me.Repaint
End Sub

would be a guess at what you want.
 
C

Chris LeBlanc

Hello again. Ithink I might have been a little vague with what I was
looking for. I have combobox1 and it has a few variables. When the
user selects variable 1 I would like combobox2 to become visible. If
they choose variable 2 then combobox3 becomes visible and so on.
I don't knw why I didn't put it that way the first time.
 
T

Tom Ogilvy

That is what I assumed, that is what I gave you although I hide all but
combobox1 first and then the appropriate combobox is made visible. It
assumes comboboxes are named combobox1, combobox2, etc

so if the select item is the first item, then listindex is zero and zero
plus 2 is 2, so combobox & (listindex + 2) is made visible (combobox2).
 
C

Chris

Hi there again. I pasted the code into the form's codes and I receive
an error. The error is that "dim cbox = MSForms.ComboBox" is a compile
error:Syntax error.
 
T

Tom Ogilvy

dim cbox = MSForms.Combobox
should be
dim cbox as MSForms.Combobox

while I apologize for the typo, should you really be attempting this?

Anyway, I copied this form a working USERFORM

Private Sub combobox1_Click()
Dim cbox As MSForms.ComboBox
For Each ctrl In UserForm1.Controls
If TypeOf ctrl Is MSForms.ComboBox Then
Set cbox = ctrl
If Right(cbox.Name, 1) <> 1 Then
cbox.Visible = False
End If
End If
Next
Controls("Combobox" & ComboBox1.ListIndex + 2).Visible = True
Me.Repaint
End Sub

Private Sub UserForm_Initialize()
Dim cbox As MSForms.ComboBox
For i = 1 To 5
For j = 1 To 4
Controls("ComboBox" & i).AddItem "Item " & j
Next
Next

For Each ctrl In UserForm1.Controls
If TypeOf ctrl Is MSForms.ComboBox Then
Set cbox = ctrl
If Right(cbox.Name, 1) <> 1 Then
cbox.Visible = False
End If
End If
Next

End Sub

Tested in xl97, SR2
 
C

Chris

It is probably over my head but I have been trying to figure a quick way
to do what has taken an entire page of activex controls. Thanks for all
the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top