简体   繁体   中英

Syntax error in Vba-excel

I have one sheet each in two different workbooks. Both sheets are same, except that sheet 2 has 3-4 records more. I use a left join to get the those records but getting an syntax error. Any help is appreciated. Thanks. Eg:-

Workbook1     Workbook2
Sheet 1       Sheet 1

ID  Name      ID Name
123  Jim      123  Jim
255  jack     255  jack
275  alice    275  alice
300  Bob     

so as you see first one has 4 records and second one has 3, which will be always the case. I just need to find that extra record in first sheet and record.

Sub get_unknowns()

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Set x = Workbooks.Open("A.xlsx")
Set y = Workbooks.Open("B.xlsx")

With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
    "Extended Properties=Excel 8.0;"
.Open
End With

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

rs.Open "SELECT * FROM [x.Worksheets("Sheet1")] LEFT JOIN [y.Worksheets("Sheet1")] ON [x.Worksheets("Sheet1")$].[PrimaryID] NOT IN " & _
"[[y.Worksheets("Sheet1")$].[primaryId]", cn

With Worksheets("Sheet3")
    .Cells(2, 1).CopyFromRecordset rs
End With

rs.Close
cn.Close

End Sub

As suggested I made some changes and tried, I have two sheets in the same workbook and trying to use a left join

Sub get_employees()

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
cn.ConnectionString = _
    "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=" & ThisWorkbook.Path & "H:\testing\demo\test1.xlsx;" & _
    "Extended Properties=""Excel 12.0"";"

rs.Open "SELECT * FROM [Sheet1$] as A LEFT JOIN [Sheet2$] as B ON A.[Id] =B.[Id]", cn

With Worksheets("Sheet3")
.Cells(2, 1).CopyFromRecordset rs
End With

rs.Close
cn.Close

End Sub

Now its throwing a Run time error 3709 that the connection cannot be used to perform this operation. it is either closed or invalid in this context.

Well, there are several issues:

a) You cannot open an xlsx file with JET, you need ACE, like:

  cn.ConnectionString = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & ThisWorkbook.Path & "\Workbookname.xlsx;" & _
        "Extended Properties=""Excel 12.0"";"

b) You don't need to open the workbooks in VBA; the connection does that on its own, but:
c) The connection counts only for one workbook. I don't think cross-workbook-queries are possible.
d) The syntax for a table in the FROM part is [Sheetname$] , you must fill this correctly into the string (the name of a VBA variable won't help inside the query string). This sheet must be in the workbook that is opened through the connection string.

Your connection string appears to be incorrect and you havent opened the connection. Try something like this

Sub get_employees()

    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset

    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=Yes"";"
        .Open
    End With

    rs.Open "SELECT * FROM [Sheet1$] as A LEFT JOIN [Sheet2$] as B ON A.[Id] =B.[Id]", cn, adOpenKeyset, adLockReadOnly

    With Worksheets("Sheet3")
        .Cells(2, 1).CopyFromRecordset rs
    End With

    rs.Close
    cn.Close

End Sub

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