2

I would like to use FULL OUTER JOIN sql query via VBA excel.Below is my code and tables. Please guide how can i use the same.

1st Table-

enter image description here

2nd Table-

enter image description here

VBA Code:-

Sub SQL()

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

strSQL = "SELECT * FROM [Sheet3$] FULL OUTER JOIN [Sheet2$] ON [Sheet2$].[Sr]=[Sheet3$].[Sr]"
rs.Open strSQL, cn

Sheet5.Range("D1").CopyFromRecordset rs

End Sub

In my above code i am getting below error.

enter image description here

4 Answers 4

1

I'm not sure that your current error is actually even being caused by the query. But MS Access does not support FULL OUTER JOIN. However, you can simulate it using a UNION of two queries:

SELECT s1.col1, s1.col2
FROM [Sheet3$] s1
LEFT JOIN [Sheet2$] s2
    ON s1.[Sr] = s2.[Sr]
UNION ALL
SELECT s2.col1, s2.col2
FROM [Sheet2$] s1
LEFT JOIN [Sheet3$] s2
    ON s1.[Sr] = s2.[Sr]

I arbitrarily chose to select two columns from Sheet3$ called col1 and col2. You are free to select any columns you want, though I would advise you to move away from using SELECT *, as it would generally leave your code prone to breaking at some point down the road. Instead, just select the columns you want explicitly.

Sign up to request clarification or add additional context in comments.

Comments

1

As Harassed Dad said here, ADODB does not support FULL OUTER JOIN or CROSS JOIN. This might work.

SELECT * FROM [Sheet3$], [Sheet2$]

Comments

0

Query #1:

strSQL = "SELECT a.* ,b.*  FROM [Sheet2$] as a LEFT JOIN [Sheet3$] as b ON a.[Sr]=b.[Sr]" 

enter image description here

Query #2:

strSQL = "SELECT a.* ,b.Family  FROM [Sheet2$] as a LEFT JOIN [Sheet3$] as b ON a.[Sr]=b.[Sr]"

enter image description here

2 Comments

The OP specifically asks for a FULL OUTER JOIN and not a LEFT JOIN as proposed in your solution. So, I am not sure why you posted this as a solution. Could you please elaborate?
@Ralph, I think that in adodb, it is not supported full outer join. In sheet2 Sr have all data of sheet3 Sr field. So the result seems to be same with full outer join.
0

ADODB doesn't support FULL OUTER JOIN, but table A OUTER JOIN Table B is equivalent to TableA LEFT JOIN TableB UNION TableB Left Join Table A if you remove duplicates. So

Select DISTINCT * From (select col1 from TableA LEFT Join TableB ON TableA.sr = TableB.sr Union select col1 from TableB Left Join Table A ON TableB.sr = TableA.sr)

will mimic a full outer

1 Comment

Am I missing something or woudn't be sufficient to use a UNION? Like: select col1 from TableA Union select col1 from TableB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.