Populating table or range to listbox in userform to get the values of the columns inside the table or range in closed workbook

0 votes

he problem is that the code is not working with me and gives "type mismatch error"

   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=F:\Book1.xlsx;" & _
                     "Extended Properties='Excel 12.0 Xml;HDR=YES';"
    
    
      
    cn.Open
     Set rs = New ADODB.Recordset
    
    rs.ActiveConnection = cn

    rs.Source = "select [date] ,[factory] ,[records] from [sheet1$]"
    
    rs.Open
     With Me.ListBox1
     .ColumnCount = rs.RecordCount
     .List = Application.WorksheetFunction.Transpose(rs.GetRows)
      
     End With

    rs.Close
    cn.Close
Feb 24, 2023 in Others by Kithuzzz
• 38,010 points
834 views

1 answer to this question.

0 votes
  1. Because rs.RecordCount is of type LongLong, which VBA cannot automatically convert into a long, you receive your type mismatch error. Using CLng, you may convert it explicitly (rs.RecordCount)
  2. When using Excel as data source, RecordCount will always be -1 as the Excel engine doesn't support RecordCount (see https://learn.microsoft.com/en-us/sql/ado/reference/ado-api/recordcount-property-ado?view=sql-server-ver16).
  3.  You probably don't want to have RecordCount as number of columns for your listview anyhow - I assume you want to get the number of fields as column count:
With Me.ListBox1
    .Clear
    .ColumnCount = rs.Fields.Count
    .List = Application.WorksheetFunction.Transpose(rs.GetRows)
End With
answered Feb 24, 2023 by narikkadan
• 63,700 points

Related Questions In Others

0 votes
1 answer

How to divide data in excel into 4 columns whose sum is almost equal to 1/4 of the sum of all values/

5049 is the sum of all numbers, ...READ MORE

answered Feb 7, 2023 in Others by narikkadan
• 63,700 points
449 views
0 votes
1 answer
0 votes
2 answers

How to get the URL of the current tab in Google Chrome?

Its so simple.... If you want to ...READ MORE

answered Aug 12, 2020 in Others by Steve
• 200 points
3,125 views
0 votes
1 answer
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
944 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,274 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
569 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
799 views
0 votes
1 answer

How to programmatically get the values of a spilled Excel range in VBA?

By using the Text property, I was ...READ MORE

answered Mar 23, 2023 in Others by narikkadan
• 63,700 points
772 views
0 votes
1 answer

How to get the total of every monday to saturday or tuesday to sunday in a month dynamically

Try this: Option Explicit Sub demo() ...READ MORE

answered Jan 29, 2023 in Others by narikkadan
• 63,700 points
377 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP