PDA

View Full Version : Rowsource for a combo box



talytech
06-10-2011, 05:50 AM
I have a UserForm with a combo box named: cboVendor

I have opened up an MS Access database via DAO and I want the rowsource for that combo box to be a sql statement. when I try to open the form it produces an error. Here's what I have:

Dim wrkJet As Workspace
Dim dbsContractorsBI As Database
Dim rstVendorList As Recordset
Dim myRow As String

'Open Microsoft Jet and ODBCDirect workspaces, Microsoft
'Jet database, and ODBCDirect connection.
DBEngine.SystemDB = "C:\Documents and Settings\e3utbl\Desktop\Devl Databases\ContractorsDbaseStuff\CSSGrp.mdw"
Set wrkJet = CreateWorkspace("", "test", "test2", dbUseJet)
Set dbsContractorsBI = wrkJet.OpenDatabase("C:\Documents and Settings\e3utbl\Desktop\Devl Databases\ContractorsDbaseStuff\ConBI_FE.mdb")

myRow = "SELECT tblVendorList.CompanyID, tblVendorList.CompanyName FROM tblVendorList ORDER BY tblVendorList.CompanyName;"

cboVendor.RowSourceType = myRow

Kenneth Hobs
06-10-2011, 06:29 AM
I normally use ADO for that kind of thing.

Sub FillLBControl(theControl As MSForms.ListBox, mdbName As String, sSQL As String)
'Requires Reference to Microsoft ActiveX Data Objects 2.8 Library
'Used for Listbox control for one column only.
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim rcArray As Variant
Dim sConnect As String
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbName & ";"

'On Error GoTo CloseADO

'Open connection to the database
cnt.Open sConnect

'Open recordset and copy to an array
rst.Open sSQL, cnt
rcArray = rst.GetRows

'Place data in the Control
With theControl
.Clear
.ColumnCount = 1
.List = Application.Transpose(rcArray)
.ListIndex = 0
End With

CloseADO:
'Close ADO objects
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
End Sub