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>
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Rajkumar_h

New Member
Joined
Oct 4, 2013
Messages
20
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:

Rajkumar_h

New Member
Joined
Oct 4, 2013
Messages
20
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
 

Rajkumar_h

New Member
Joined
Oct 4, 2013
Messages
20
Got the solution from link 'https://www.morgantechspace.com/2015/09/multiple-left-joins-in-ms-access-database.html'.

Thanks & Regards,
Raj
 

Watch MrExcel Video

Forum statistics

Threads
1,090,334
Messages
5,413,847
Members
403,504
Latest member
rosebo

This Week's Hot Topics

Top