简体   繁体   中英

How can I do a simple join on a subquery?

I've been at this for 2 hours and have no idea what the issue is. Although I have worked a fair amount with SQL, I'm struggling with the idiosyncrasies in Access queries relative to SQL queries when using the ADO ACE connection to query Excel worksheets. My goal at it's most basic is to do a join with a subquery. I have simplified the query considerably to display the issue, so please excuse the fact that it wouldn't really make sense to run a query like this.

The error I keep getting is Syntax Error in From Clause . The worksheet only has 2 columns, Account Number and Cost

Sub stackoverflow()

Dim acctcon As New ADODB.Connection
Dim acctrec As New ADODB.Recordset

acctcon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\name\Documents\Book1.xlsx;" & _
                            "Extended Properties=" & Chr(34) & "Excel 12.0 Xml;HDR=YES" & Chr(34) & ";"

acctcon.Open

querystr = "Select [Accounts$].[Account Number], [mm].[Cost] " & _
        "FROM [Accounts$] " & _
        "JOIN (Select [Account Number], [Cost] " & _
            "FROM [Accounts$]) As mm " & _
        "ON [Accounts$].[Account Number] = [mm].[Account Number]"

acctrec.Open querystr, acctcon

'Syntax Error on From Clause

End Sub

Try to reset the Close bracket behind As mm

querystr = "Select [Accounts$].[Account Number], [mm].[Cost] " & _
           "FROM [Accounts$] " & _
           "INNER JOIN (Select [Account Number], [Cost] " & _
           "FROM [Accounts$] As mm) " & _
           "ON [Accounts$].[Account Number] = [mm].[Account Number]"

But you have to replace the inner join, because it makes no sense like @krish KM stated. I suppose you took it as a placeholder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM