VBA - working with data videos | How do I populate a listbox using an ADO recordset in VBA?

Posted by Andrew Gould on 21 April 2021


This video shows you how to extract the results of an ADO recordset using the GetRows method and use it to populate a Listbox on a user form in VBA. You'll learn about ActiveX Data Objects connections and recordsets, how to add values to a listbox using both the AddItem method and the List property and how to transpose an array in VBA.

See our full range of VBA training resources, or test your knowledge of VBA with one of our VBA skills assessment tests.

This video has the following accompanying files:

File name Type Description
ADODB Recordset to Listbox.xlsm Excel workbook with macros
Movies.xlsx Excel workbook

Click to download a zipped copy of the above files.

There are no exercises for this video.

Making a  video bigger

You can increase the size of your video to make it fill the screen like this:

View full screen

Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown at its bottom right-hand corner.

 

When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.

Improving the quality of a video

To improve the quality of a video, first click on the Settings icon:

Settings icon

Make sure you're playing your video so that the icons shown appear, then click on this gear icon at the bottom right-hand corner.

 

Choose to change the video quality:

Video quality

Click on Quality as shown to bring up the submenu.

 

The higher the number you choose, the better will be your video quality (but the slower the connection speed):

Connection speed

Don't choose the HD option unless you have a fast enough connection speed to support it!

 

Is your Wise Owl speaking too slowly (or too quickly)?  You can also use the Settings menu above to change your playback speed.

This page has 1 thread Add post
20 Feb 22 at 15:17

Hello,

Thank you for posting the video. I tried using the sample Excel files that came with the video. When I run the program to populate the listbox using data from the Movies.xlsx file, I received the following error:

1. Run-time error '-2147467259 (80004005)': Failure creating file

Private Sub UserForm_Initialize()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Set cn = New ADODB.Connection
    
    cn.ConnectionString = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & ThisWorkbook.Path & "\Movies.xlsx;" & _
        "Extended Properties='Excel 12.0 Xml;HDR=YES';"

    cn.Open
    
    Set rs = New ADODB.Recordset
    
    rs.ActiveConnection = cn
    rs.Source = _
        "SELECT [Title], [Release Date], [Run Time] FROM [Sheet1$] " & _
        "WHERE [Title] LIKE '%star%' ORDER BY [Title]"
        
    rs.Open
    
    With ListBox1
        .ColumnCount = rs.Fields.Count
        .ColumnWidths = "300;50;50"
        .List = WorksheetFunction.Transpose(rs.GetRows)
    End With
    
    rs.Close
    cn.Close
    
End Sub

This error most likely occurred at the VBA line: cn.open

 

I am using Excel Office 365 with Windows 10 64-bit and Microsoft OneDrive. The path to the Movies.xlsx file in OneDrive has the following extension:

https://d.docs.live.net/

 

References to the following libraries have been turned on in the Excel VBA Editor-Tools\References menu:

1. Visual Basic for Applications

2. Microsoft Excel 16.0 Object Library

3. OLE Automation

4. Microsoft Office 16.0 Object Library

5. Microsoft ActiveX Data Objects 6.1 Library

I am not sure whether the problems in creating the ADO connection has to do with the connection string, the OneDrive path, or the object library references in Excel.

Thank you

20 Feb 22 at 19:33

Hi! It's almost certainly the OneDrive file location. I believe the accepted workaround is to use a local version of the source file, synced to the version saved to OneDrive.

https://stackoverflow.com/questions/54081879/how-can-i-run-a-sql-query-within-an-excel-workbook-saved-to-onedrive?rq=1

I hope it helps!