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

Set Variable equal to auto-created ComboBoxes in a form

Status
Not open for further replies.

bdmangum

Technical User
Dec 6, 2006
171
US
Hi,
What I need to do is set a variable equal to all the ComboBoxes in a user form. I'm sure there is an easy code line for this, I just don't know what it is. Here is the code which inserts the comboboxes:
Code:
                Set cNewComboBox = .Controls.Add("Forms.ComboBox.1")
                    With cNewComboBox
                        quickcount = counter
                        While Worksheets(2).Cells(quickcount, TempCount + 1).Value <> ""
                            cNewComboBox.AddItem Worksheets(2).Cells(quickcount, TempCount + 1).Value
                            quickcount = quickcount + 1
                        Wend
                        .Height = PosH
                        .Width = PosW
                        .Left = PosL
                        .Top = PosTop
                        .Value = Worksheets(2).Cells(counter, TempCount + 1).Value
                        .Font.Size = 8
                        .Locked = True
                        .TextAlign = 3
                        .TabStop = False
                        .AutoSize = True
                        .MatchRequired = True
                        If cNewComboBox.Width < 82.25 Then
                            .AutoSize = False
                            .Width = 82.25
                        End If
                    End With

This all occurs with a large while loop. Thus each pass by this it will create a new combobox. I also adjust the PosL after each box, so they aren't on top of each other. It also uses some Public Variables.

The reason I'm trying to set a variable equal to all the comboboxes in the form is because I have no idea how many will be created. My ultimate goal is to simply change the ".locked = true" to ".locked = false". The user will select an "edit" button which then unlocks the boxes for editing.

Anybody know what code I should use?
 
The Form already has a collection of all its controls. Just iterate over it checking the type of each control. If it's a combobox, set the .Locked property. This example uses Checkboxes on a Multipage, but it should give you the idea, and you should easily be able to amend it to do what you want.
Code:
Private Sub setAll(check As Boolean)
    
    For Each p In MultiPage1.Pages
        For Each c In p.Controls
            If TypeName(c) = "CheckBox" Then
                c.Value = check
            End If
        Next
    Next
    
End Sub

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Steve!

That works great, with a little tweaking it does exactly what I need it to.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top