Using SQL in VBA LEFT JOIN to fetch data from multiple sheet

Rajkumar_h

New Member
Joined
Oct 4, 2013
Messages
20
Hi Expert,

I have three sheets of data using LEFT JOIN i am supposed to pull the required columns as result. This is the main motive.
With the help of this link
https://analystcave.com/excel-using-...on-excel-data/
i took the code which does connection to my excel sheets.

I need an experts help to fix my issue, here is the code for your reference.

Code:
[LEFT][COLOR=#333333][FONT=monospace]Sub RunSELECT()
    Dim cn As Object, rs As Object, output As String, sql As String
    
    '---Connecting to the Data Source---
    Set cn = CreateObject("ADODB.Connection")
    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
        .Open
    End With
    
    
    '---Run the SQL SELECT Query---

sql = "SELECT [MainData$].[PO], [Invoice], [Customer_Number], [Sls_Amt], [Shipping_Location],[GLList$].[Date], [new_Amount], [POList$].[Date], [PO_No], [ST] FROM [MainData$] LEFT JOIN [GLList$] ON [MainData$].[WOD_PO]=[GLList$].[WOD_PO] LEFT JOIN [POList$] ON [MainData$].[WOD_PO]=[POList$].[WOD_PO]"

Set rs = cn.Execute(sql)
 
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Temp"
    With Sheets("Temp").Range("A1")
        Do
        i = 0
            For Each fld In rs.Fields
                ActiveCell.Offset(0, i).Value = fld
                i = i + 1
            Next fld
            rs.MoveNext
            ActiveCell.Offset(1, 0).Select
        Loop Until rs.EOF
    End With

    
    '---Clean up---
    rs.Close
    cn.Close
    Set cn = Nothing
    Set rs = Nothing
End Sub[/FONT][/COLOR][/LEFT]

Waiting for the reply from an expert

Regards,
Raj
<strike></strike>
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Sorry, I totally missed this point. I get the error message "
Run-time error '-214721900(80040e14)':
Syntax error (missing operator) in query expression '[MainData$].[WOD_PO]=[GLList$].[WOD_PO] LEFT JOIN [POList$] ON [MainData$].[WOD_PO]=[POList$].[WOD_PO]'.
"

I am able to run the code without any issue by excluding dates columns which are present in other two tables, but I face this issue when I add them in the query.

Not sure should I want to change the name of date column and then try it. Please suggest.

Regards,
Raj
 
Last edited:
Upvote 0
Sorry, I totally missed this point. I get the error message "Run-time error '-214721900(80040e14)':
Syntax error (missing operator) in query expression '[MainData$].[WOD_PO]=[GLList$].[WOD_PO] LEFT JOIN [POList$] ON [MainData$].[WOD_PO]=[POList$].[WOD_PO]'."



I am able to run the code with single join without any issue but including another join it throws this error.
The below Single SQL query works without any issue, but adding another additional JOIN for which am facing problem.


sql = "SELECT [MainData$].[PO], [Invoice], [Customer_Number], [Sls_Amt], [Shipping_Location], [DateGL], [Form], [new_Amount] FROM [MainData$] LEFT JOIN [GLList$] ON [MainData$].[WOD_PO]=[GLList$].[WOD_PO]"

Regards,
Raj
 
Upvote 0
Got the solution from link 'https://www.morgantechspace.com/2015/09/multiple-left-joins-in-ms-access-database.html'.

Thanks & Regards,
Raj
 
Upvote 0

Forum statistics

Threads
1,223,627
Messages
6,173,420
Members
452,514
Latest member
cjkelly15

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