Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

add combox with code

0 views
Skip to first unread message

Dawna

unread,
Nov 23, 2009, 9:43:03 AM11/23/09
to
Good Morning,

Could someone help with a code to add a combobox to a userform using code.
I'd like to have a command button "add" which will add this combobox.

Thanks in advance.
Dawna

Dave Peterson

unread,
Nov 23, 2009, 10:13:01 AM11/23/09
to
How about an alternative...

Add your combobox to the userform while you're in design mode--but hide it and
show it using the commandbutton.

It makes things lots easier.

--

Dave Peterson

Paul

unread,
Nov 23, 2009, 10:33:01 AM11/23/09
to
Dawna

You've got two main choices :

1. Add the combobox in your design, but make it Visible = False. When the
time comes during your code use
MyForm.MyCombo.Visible = True

2. Add the combobox using vba code.
This falls into two bits
a. Add the combobox
b. Fill it with your data

The downside of 2 is that you'll need to make sure it doesn't get placed
over other controls.

If you want more info on 2, post a reply.

Dawna

unread,
Nov 23, 2009, 10:41:02 AM11/23/09
to
Hi Paul. Thank you for the reply. Could you help with code for the second
choice?
Thank you again.
Dawna

Paul

unread,
Nov 23, 2009, 11:23:01 AM11/23/09
to
This should add your box :

Private Sub Create_Combo()
Dim MyCombo As MSforms.ComboBox
Set MyListBox = MyForm.Controls.Add("Forms.ComboBox.1")
With MyListBox
.Top = 10
.Left = 20
.Height = 50
.Name = "myBox"
End With

End Sub

Then to add some data :

Sub Add_Combo_Data()
dim myArray(Number_of_Items)
' Add the items for your list into the array as follows :
For nCount = 1 to Number_of_Items
myArray(nCount) = Whatever ' each of your items
Next
MyNewForm.MyBox.List = myArray

End Sub

You can get very clever with more columns, but positioning becomes seriously
more difficult.

Hope that helps get you started.

Dawna

unread,
Nov 23, 2009, 11:21:01 PM11/23/09
to
Thank you as well Dave.

"Dave Peterson" wrote:

> .
>

Dawna

unread,
Nov 23, 2009, 11:21:01 PM11/23/09
to
Thank you for the help! Much appreciated.
0 new messages