Results 1 to 9 of 9
  1. #1
    PATRICK is offline Advanced Beginner
    Windows 8 Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    32

    Populate listbox from Recordset

    So Im trying to populate the listbox using string connection and having this error: "the object you entered is not a valid recordset property"

    here's the code:
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset

    Set cnn = New ADODB.Connection

    cnn = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=xxx;DATABASE=xxx;Port=xxx;USER=xxx; PASSWORD=xx;OPTION=3;"

    cnn.Open

    Set rst = cnn.Execute("SELECT date, timein, timeout FROM table")


    Me.List5.RowSourceType = "Table/Query"


    Set Me.List5.Recordset = rst

    rst.Close
    cnn.Close

    Set rst = Nothing


    Set cnn = Nothing


    Where is the error??? am doing right or is there any better way to populate listbox from string connection.

    Thanks a lot...

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I would guess that this line:
    Set Me.List5.Recordset = rst
    should really be
    Me.List5.Rowsource = rst

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Select queries are assigned to the Rowsource of listboxes.

    I have never tried to assign a recordset to a list box.

    My listbox properties usually look like this

    me.lstTemp.RowSource = strSQL





    Set Me.List5.Recordset = rst

  4. #4
    PATRICK is offline Advanced Beginner
    Windows 8 Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    32
    RuralGuy, I tried your suggestion but then another error is happening: "Error 91 - Object variable or With block variable not set"

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Did you compile your code from the debugger? Did it stop on a variable that was not declared?

  6. #6
    PATRICK is offline Advanced Beginner
    Windows 8 Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    32
    if I put the line like ruralguy said : "Me.List5.Rowsource = rst" then when I debugger I got error Mismatch"......but if I the line is .recordset = rst, then it's debugging but then I have the error "the object you entered is not a valid recordset property"

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Well, mismatch is telling you it does not like the fact that you are assigning a recordset to the rowsource of the listbox.

    Maybe using your "connection" you can build an SQL string that will SELECT a table or query object. Maybe a query def or an ADOX table or something.

    I don't know exactly how. I just have never been able to get a string directly out of a recordset.

  8. #8
    PATRICK is offline Advanced Beginner
    Windows 8 Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    32
    ok, I've got this code working, but it is too slow. I don't have even 2.000 rows and it takes almost 50 seconds to populate my list box !!!

    code:

    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset

    Dim strSQL As String

    Set cnn = New ADODB.Connection
    cnn = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=xxx;DATABASE=xxx;Port=xxx;USER=xxx; PASSWORD=xxx;OPTION=3;"

    cnn.Open

    strSQL = "SELECT date, timein, timeout FROM table"

    Set rst = cnn.Execute(strSQL)

    Do While Not rst.EOF
    Me.List5.AddItem rst!date
    rst.MoveNext
    Loop


    rst.Close
    cnn.Close


    Set rst = Nothing
    Set cnn = Nothing
    ''

    Any idea what is making this to running so slow???!!

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Try doing something else with your connection. You are creating a recordset and I don't think that is what you need.

    Check out this thread. Post #5 talks about ADOX. Looks like there is some code there to get a string you can pass to the listbox. There is another method discussed in the thread too.
    http://www.dbforums.com/microsoft-ac...ef-object.html

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 10
    Last Post: 06-18-2013, 02:00 PM
  2. Replies: 19
    Last Post: 11-01-2012, 08:03 AM
  3. Replies: 4
    Last Post: 08-23-2012, 12:28 AM
  4. Populate a Combo Box from a RecordSet
    By dreamnauta in forum Programming
    Replies: 1
    Last Post: 01-27-2012, 03:18 PM
  5. Populate unbound listbox with VBA
    By usmcgrunt in forum Forms
    Replies: 1
    Last Post: 09-23-2010, 09:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums