Consulting

Results 1 to 4 of 4

Thread: Error while populating a combobox from Access database

  1. #1
    VBAX Regular
    Joined
    Jun 2006
    Posts
    8
    Location

    Question Error while populating a combobox from Access database

    Hi,

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

    [vba]
    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
    [/vba]
    When I call the above Sub, I use the following code:

    [vba]
    Call fillBox(myForm, myCombo, "sName", "tblState")
    [/vba]
    Now, where exactly am I going wrong? Please help.

    Peace,

    Shivboy
    Last edited by Ken Puls; 06-29-2006 at 12:12 PM. Reason: added VBA tags

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Don't know much about access but many here can help. Did you set a reference?

    From a search of the Excel forum: search titles for access
    http://www.vbaexpress.com/forum/show...ghlight=access

    http://www.vbaexpress.com/forum/show...ghlight=access
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Jun 2006
    Posts
    8
    Location
    I need help with passing the object name as a parameter because that is where the error is occurring. When I pass the name of the form, it accepts it, but when I pass the name of the combo box, it generates the error. Where exactly am I going wrong?



    Quote Originally Posted by lucas
    Don't know much about access but many here can help. Did you set a reference?

    From a search of the Excel forum: search titles for access
    http://www.vbaexpress.com/forum/show...ghlight=access

    http://www.vbaexpress.com/forum/show...ghlight=access

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    This is untested, but I would try specifiying your object a little more explicitly:

    [vba]
    Sub fillBox(oList As MSForms.Combobox, 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
    oList.AddItem rs(1)
    rs.MoveNext
    Loop
    rs.Close
    con.Close
    End Sub[/vba]

    And from your calling userform:
    [vba]Dim cBox as msforms.combobox
    Set cBox as me.Combobox1
    [/vba]
    [vba]Call fillBox(cbox, "sName", "tblState") [/vba]

    BTW, I also edited your post to use our VBA tags over the board code tags. They make things a little more readable.

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •