Filter data using SQL

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,414
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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
How does it not work?
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,414
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
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,414
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"";"
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,129,916
Messages
5,638,976
Members
417,063
Latest member
thematulaak

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
Top