A combobox control works much like a picklist, in that it presents a number of selectable values to be stored in that control. You can set available values at development time using the items property of the control, but for greater flexablility, you can populate the control at run-time as well. For this example, we'll populate a combobox with a distinct list of Account Type values:
First we want to clear the combobox of existing items, so we aren't adding duplicates:
ComboBox1.Items.Clear
Next, we'll create a recordset containing a distinct list of account type values.
Dim objRS
With objRS
Set objRS = Application.CreateObject("ADODB.Recordset")
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.Open "select distinct type from account", Application.BasicFunctions.GetNewConnection
Finally, we loop through the recordset, adding each returned item in turn.
While Not (.BOF or .EOF)
ComboBox1.Items.Add .Fields("TYPE").Value
.MoveNext
Wend
.Close
End With
Set objRS = Nothing
That's all there is to it! All in all, this is a pretty simple process. This can be used to populate the ListBox or CheckListBox controls as well. I hope you find this code snippet useful. Thanks for reading!