Filter data using SQL

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Usually if I have data in an Access / SQL Server db (or even another Excel workbook), I can extract the data using ADO and SQL like:

Code:
Sub GetData()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Dim strcon As String
    Dim strSQL As String
        
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    rs.CursorLocation = adUseServer
    
    strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & "C:\MyData.xlsx" & ";" & _
             "Extended Properties=""Excel 12.0 Macro;" & _
             "HDR=Yes;" & _
             "IMEX=1;" & _
             "MaxScanRows=0"";"
             
    cn.Open ConnectionString:=strcon
        
    strSQL = "SELECT * " & _
             "FROM [DataSheet$] " & _
             "WHERE [DataSheet$].[Fruit] =""orange"""
    
    rs.Open Source:=strSQL, _
            ActiveConnection:=cn
    
    wksData.Cells(1,1).CopyFromRecordset Data:=rs
        
    rs.Close
    cn.Close
            
    Set rs = Nothing
    Set cn = Nothing
       
End Sub

But what if my data is already within this workbook?

This does not work:

Code:
 strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & Thisworkbook.Worksheets("Data") & ";" & _
             "Extended Properties=""Excel 12.0 Macro;" & _
             "HDR=Yes;" & _
             "IMEX=1;" & _
             "MaxScanRows=0"";"

(If I had Office 365, I would use the Filter function but alas, I don't).

Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How does it not work?
It crashes on this line:

Code:
 strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & ThisWorkbook.Worksheets("Data") & ";" & _
             "Extended Properties=""Excel 12.0 Macro;" & _
             "HDR=Yes;" & _
             "IMEX=1;" & _
             "MaxScanRows=0"";"

with an error message:

Code:
Run-time error 438

Object does not support this property or method
 
Upvote 0
It crashes on this line:

Code:
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & ThisWorkbook.Worksheets("Data") & ";" & _
             "Extended Properties=""Excel 12.0 Macro;" & _
             "HDR=Yes;" & _
             "IMEX=1;" & _
             "MaxScanRows=0"";"

with an error message:

Code:
Run-time error 438

Object does not support this property or method


Sorted:

It should be:

Code:
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & ThisWorkbook.FullName & ";" & _
             "Extended Properties=""Excel 12.0 Macro;" & _
             "HDR=Yes;" & _
             "IMEX=1;" & _
             "MaxScanRows=0"";"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top