I need to:
1. Connect to the database via VBA (done)
2. Populate the RowSource of a combo box with a recordset created by querying a table in the SQL Server database
This is how I have started, but looks like I may be going down the wrong path.
Code: Select all
Dim cnn As New ADODB.Connection
Dim rst2 As New ADODB.Recordset
Dim strSQL As String
On Error GoTo Form_Load_Error
' ==============================
Dim objCon As ADODB.Connection
Dim objCat As ADOX.Catalog
Dim objTbl As ADOX.Table
Dim objInd As ADOX.Index
Dim objCol As ADOX.Column
Dim strCon As String
Dim ConnectionExists As Boolean
fCreateConnectionString ' Function to create the connection string
strCon = Me.txtConnectionString
Set objCon = New ADODB.Connection
objCon.Open strCon
Set objCat = New ADOX.Catalog
Set objCat.ActiveConnection = objCon
For Each objTbl In objCat.Tables
If objTbl.Type = "TABLE" Then
' If objTbl.Type = "View" Then
If objTbl.Name = "tblStateNames" Then
strSQL = "SELECT STATE_txtAbbreviation, STATE_txtState " & _
"FROM tblStateNames " & _
"ORDER BY STATE_txtAbbreviation;"
Me.S_txtState.RowSource = strSQL
GoTo ExitHere
End If
End If
Next objTbl
ExitHere:
Form_Load_Exit:
Exit Sub
Form_Load_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of VBA Document Form_frmSiteDynCorp2011"
GoTo Form_Load_Exit
Ken