Error while populating a combobox from Access database

S

shivboy

Hi,

I coded the following Sub to populate a combobox / listbox but it is
generating a Type mismatch error.


Code:
--------------------

Sub fillBox(ByVal oForm As Form, ByVal oList As Object, ByVal oField As String, ByVal oTab As String)

dbPath = "C:\abc.mdb"

Dim sql As String

sql = "SELECT " & oField & " FROM " & oTab

Set con = New ADODB.Connection
With con
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open dbPath
End With

Set rs = New ADODB.Recordset
rs.CursorLocation = adUseServer
rs.Open Source:=sql, ActiveConnection:=con, CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, Options:=adCmdText

Do Until rs.EOF
oForm.Controls(oList).AddItem rs(1)
rs.MoveNext
Loop
rs.Close
con.Close
End Sub

--------------------


When I call the above Sub, I use the following code:


Code:
--------------------

Call fillBox(myForm, myCombo, "sName", "tblState")

--------------------


Now, where exactly am I going wrong? Please help.

Peace,

Shivboy
 
N

NickHK

oForm.Controls(oList).AddItem rs.Fields(1).Value
'Or Fields(0), can't remember if it's 0 or 1 based.

NickHK

shivboy said:
Hi,

I coded the following Sub to populate a combobox / listbox but it is
generating a Type mismatch error.


Code:
--------------------

Sub fillBox(ByVal oForm As Form, ByVal oList As Object, ByVal oField As String, ByVal oTab As String)

dbPath = "C:\abc.mdb"

Dim sql As String

sql = "SELECT " & oField & " FROM " & oTab

Set con = New ADODB.Connection
With con
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open dbPath
End With

Set rs = New ADODB.Recordset
rs.CursorLocation = adUseServer
rs.Open Source:=sql, ActiveConnection:=con,
CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic,
Options:=adCmdText
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top