I have a combobox on a MS Word userform called cboCustName which I want populated by a SQL database. I've already successfully created the connection and have populated the combobox with the correct data.
My next problem is this: How do I pull more than one value into the combobox?
I want the ID number of the recordset in the combobox to be the bound column, whereas I would reveal the strCustName value to the user. This is like a basic pulldown in an Access database. Is this possible in Word? I've only seen combobox examples where only one column is used.
Here is my working code as is, without pulling the ID column from the recordset.
<code>
Private Sub UserForm_Initialize()
Dim cn As ADODB.Connection ' define connection string
Dim cmd As New ADODB.Command ' define ADO command string
Dim rsTest As New ADODB.Recordset 'define ADO recordset
Dim strSQLString As String ' SQL command string
Dim strConnection As String ' SQL Connection String
strConnection = connection stuff here
strSQLString = "SELECT * FROM CUSTOMERS"
'Connection
Set cn = New ADODB.Connection
With cn
.ConnectionString = strConnection
End With
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = strSQLString
rsTest.CursorLocation = adUseClient
rsTest.Open cmd, , adOpenStatic, adLockBatchOptimistic
Do While Not (rsTest.EOF)
Dim intCustID As Integer
Dim strCustName As String
intCustID = rsTest!CustomerID
strCustName = rsTest!CustomerName
Me.cboCustName.List = Array(strCustName)
rsTest.MoveNext
Loop
cn.Close
End Sub
</code>
Got any ideas?
Thanks.
"Sleep is the best meditation." - Dalai Lama