Hello, sorry if I didn't explain it very well before. Actaully, what I'm trying to do is populate other fields on the form based on the value from a combo box which displays the id number, but the other fields are from different fields in the table. I'm populating the order_ids into the combo box already, so the next step for me is if the user selects a certain order_id from the combo box, the other fields on the sheet update accordingly. I used to do this with the form/control wizard in Access but not sure how to do it in Excel. Do I need to use a query like 'SELECT order name, order details FROM Orders WHERE order_id = 'Excel combo box selection value'? Just not sure how to approach it.
Here is my code module:
Public Sub PopulateControl()
Dim cnRetailData As ADODB.Connection
Dim rsRetailData As ADODB.Recordset
Dim strCnn As String
' Open connection.
strCnn = "Provider=sqloledb; Data Source=TEST;Initial Catalog=TESTDB;" & _
"User Id=XX;Password=XXXXX; "
Set cnRetailData = New ADODB.Connection
cnRetailData.Open strCnn
' Open Orders table.
Set rsOrders = New ADODB.Recordset
rsOrders.CursorType = adOpenKeyset
rsOrders.LockType = adLockOptimistic
rsOrders.Open "Orders", cnRetailData, , , adCmdTable
' Moves to the first record in the record set.
rsOrders.MoveFirst
' Loops through each entry in the record set and adds the last name
' for each entry into the combo box.
Do Until rsOrders.EOF
ActiveSheet.ComboBox1.AddItem rsOrders!order_number
' To use a ListBox control, use the following statement instead
' of the one above:
' UserForm1.ListBox1.AddItem rsOrders!order_number
'
' If the ComboBox or ListBox is on a worksheet instead of
' a UserForm, reference the worksheet instead of the UserForm:
' ActiveSheet.ComboBox1.AddItem rsOrders!order_number
rsOrders.MoveNext
Loop
' Displays the user form. You don't need this if you are not using
' a UserForm object.
'UserForm1.Show
' Closes the table.
rsOrders.Close
' Closes the connection.
cnRetailData.Close
End Sub
Thanks in advance,
CB
Here is my code module:
Public Sub PopulateControl()
Dim cnRetailData As ADODB.Connection
Dim rsRetailData As ADODB.Recordset
Dim strCnn As String
' Open connection.
strCnn = "Provider=sqloledb; Data Source=TEST;Initial Catalog=TESTDB;" & _
"User Id=XX;Password=XXXXX; "
Set cnRetailData = New ADODB.Connection
cnRetailData.Open strCnn
' Open Orders table.
Set rsOrders = New ADODB.Recordset
rsOrders.CursorType = adOpenKeyset
rsOrders.LockType = adLockOptimistic
rsOrders.Open "Orders", cnRetailData, , , adCmdTable
' Moves to the first record in the record set.
rsOrders.MoveFirst
' Loops through each entry in the record set and adds the last name
' for each entry into the combo box.
Do Until rsOrders.EOF
ActiveSheet.ComboBox1.AddItem rsOrders!order_number
' To use a ListBox control, use the following statement instead
' of the one above:
' UserForm1.ListBox1.AddItem rsOrders!order_number
'
' If the ComboBox or ListBox is on a worksheet instead of
' a UserForm, reference the worksheet instead of the UserForm:
' ActiveSheet.ComboBox1.AddItem rsOrders!order_number
rsOrders.MoveNext
Loop
' Displays the user form. You don't need this if you are not using
' a UserForm object.
'UserForm1.Show
' Closes the table.
rsOrders.Close
' Closes the connection.
cnRetailData.Close
End Sub
Thanks in advance,
CB