SQL Query in Excel

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to replicate this SQL query:

Code:
SELECT Table1.Fruit, Table1.Store, Table2.Store
FROM Table1 INNER JOIN Table2 ON Table1.Fruit = Table2.Fruit;

In Sheet1, I have data in the range A1 to B6, Sheet2's data ranges from A1 to B4.

This is my code:

Code:
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Dim rsField As ADODB.Field
    
    Dim strcon As String
    Dim strSQL As String
        
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    rs.CursorLocation = adUseClient
    
    strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & ThisWorkbook.FullName & ";" & _
             "Extended Properties=""Excel 12.0 Macro;" & _
             "HDR=Yes;" & _
             "IMEX=1;" & _
             "MaxScanRows=0"";"
             
    cn.Open ConnectionString:=strcon
    
    Dim Address As String
    
    Address = Sheet1.Cells(1, 1).CurrentRegion.Address(0, 0)
    
    Dim Address2 As String
    
    Address2 = Sheet2.Cells(1, 1).CurrentRegion.Address(0, 0)
   
    strSQL = "SELECT [Sheet1$].[Fruit], [Sheet1$].[Store], [Sheet2$].[Store] " & _
             "FROM [Sheet1$] INNER JOIN [Sheet2$] ON [Sheet1$].[Fruit] = [Sheet2$].[Fruit]"
            
    rs.Open Source:=strSQL, _
            ActiveConnection:=cn

but it fails on this line:

Code:
 rs.Open Source:=strSQL, _
            ActiveConnection:=cn

with an error message of:

Code:
no value given for one or more parameters.

Inspecting strSQL in the Immediate Window shows:

Code:
SELECT [Sheet1$].[Fruit], [Sheet1$].[Store], [Sheet2$].[Store] FROM [Sheet1$] INNER JOIN [Sheet2$] ON [Sheet1$].[Fruit] = [Sheet2$].[Fruit]

Can someone please explain what is the problem?

Thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I am trying to replicate this SQL query:

Code:
SELECT Table1.Fruit, Table1.Store, Table2.Store
FROM Table1 INNER JOIN Table2 ON Table1.Fruit = Table2.Fruit;

In Sheet1, I have data in the range A1 to B6, Sheet2's data ranges from A1 to B4.

This is my code:

Code:
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
   
    Dim rsField As ADODB.Field
   
    Dim strcon As String
    Dim strSQL As String
       
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
   
    rs.CursorLocation = adUseClient
   
    strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & ThisWorkbook.FullName & ";" & _
             "Extended Properties=""Excel 12.0 Macro;" & _
             "HDR=Yes;" & _
             "IMEX=1;" & _
             "MaxScanRows=0"";"
            
    cn.Open ConnectionString:=strcon
   
    Dim Address As String
   
    Address = Sheet1.Cells(1, 1).CurrentRegion.Address(0, 0)
   
    Dim Address2 As String
   
    Address2 = Sheet2.Cells(1, 1).CurrentRegion.Address(0, 0)
  
    strSQL = "SELECT [Sheet1$].[Fruit], [Sheet1$].[Store], [Sheet2$].[Store] " & _
             "FROM [Sheet1$] INNER JOIN [Sheet2$] ON [Sheet1$].[Fruit] = [Sheet2$].[Fruit]"
           
    rs.Open Source:=strSQL, _
            ActiveConnection:=cn

but it fails on this line:

Code:
rs.Open Source:=strSQL, _
            ActiveConnection:=cn

with an error message of:

Code:
no value given for one or more parameters.

Inspecting strSQL in the Immediate Window shows:

Code:
SELECT [Sheet1$].[Fruit], [Sheet1$].[Store], [Sheet2$].[Store] FROM [Sheet1$] INNER JOIN [Sheet2$] ON [Sheet1$].[Fruit] = [Sheet2$].[Fruit]

Can someone please explain what is the problem?

Thanks
I've sorted it, my mistake with the field names.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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