MSAccessRookie
AWF VIP
- Local time
- Yesterday, 21:32
- Joined
- May 2, 2008
- Messages
- 3,428
I am trying to simulate an Acccess Combo Box on an Excel Spreadsheet. The effect would be to click on a cell and have a dropdown box with a list of choices appear. The User would then select one of the available choices. I am aware of how to do this via Excel ranges (both on the same WorkSheet, and on a separate Worksheet within the same SpreadSheet, but what I really need is to have the data source be an SQL Query to an SQL Server Table. The following Code updates a User Form with what I believe is the correct list of data, but since the User Form is not a part of the SpreadSheet, this was not deemed an acceptable solution at this time. Does anyone have any further ideas?
Code:
Public Sub GetIngredientList()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String
With cn
On Error GoTo ConnectionError
.ConnectionString = "Provider=SQLOLEDB; " & _
"Data Source=SQL; " & _
"Initial Catalog=RDI;" & _
"User ID=sa; Password=; Trusted_Connection=yes"
.Open
End With
On Error GoTo GeneralSQLError
[B][COLOR=green]strSQL = "SELECT tblIngredients.Ingredient FROM tblIngredients " & _[/COLOR][/B]
[B][COLOR=green] "ORDER BY tblIngredients.Ingredient"[/COLOR][/B]
rs.Open strSQL, cn, adOpenStatic, adLockBatchOptimistic
rs.MoveFirst ' Moves to the first record in the record set.
' Loops through each entry in the record set and adds the last name
' for each entry into the combo box.
Do Until rs.EOF
UserForm1.ComboBox1.AddItem rs!Ingredient
rs.MoveNext
Loop
UserForm1.Show
rs.Close ' Closes the RecordSet.
cn.Close ' Closes the connection.
ExitHere:
On Error Resume Next
cn.Close: Set cn = Nothing
Err.Clear
Exit Sub
ConnectionError:
MsgBox "Connection not propertly defined.", vbExclamation
Resume ExitHere
GeneralSQLError:
MsgBox "General SQL Error - " & Err.Description, vbExclamation
Resume ExitHere
End Sub