ADODB SQL Syntax - Access table Inner Join with Excel worksheet

匆匆过客 提交于 2021-02-19 02:36:09

问题


I have a project where users will need to fill out an Excel file and then export the data to an Access database. The data collected in the Excel file will need to be exported in 3 steps: (1) export data set 1 record, (2) query Access for the primary key (auto-number) of the newly imported record, (3) export data set 2 record, which includes the primary key populated as the foreign key.

I am able to accomplish the first step by establishing and opening an ADODB connection. However, I am running into trouble in the second step where I need to do an inner join on the Access table and the Excel.

wlodb.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\" &userSID & "\Desktop\WLO R&C Database_10-4-16.accdb"

sqlFindREMPK = "Select ID " _
& "FROM [test1] a " _
& "INNER JOIN [Excel 8.0;HDR=YES;IMEX=2;DATABASE=C:\User\RED-WIP.xlsm].[REM Upload$] b " _
& "ON a.[REM_ID_Database] = b.[REM_ID_Database] " _
& "WHERE (((a.[REM_ID_Database])=""REM9811044""));"

WLOrs.Open sqlFindREMPK, wlodb

ActiveSheet.Range("A10").CopyFromRecordset (WLOrs)

The table and the worksheet name have the same field names. The problem is with the SELECT clause. If I leave as is, I will get an error saying the field could refer to more than one table in the FROM clause. If I add the table name such as [test1].[ID] then I will get the message saying no value given for required parameters. If I change the Excel field name slightly to ID1 and leave the SELECT clause as just ID the code runs fine.


回答1:


Once you provide an alias for a table, you have to use that alias when referring to that table. You cannot access the table by its original name any more.

sqlFindREMPK = "Select a.ID " _
  & "FROM [test1] a " _
  & "INNER JOIN [Excel 8.0;HDR=YES;IMEX=2;DATABASE=C:\User\RED-WIP.xlsm].[REM Upload$] b " _
  & "ON a.[REM_ID_Database] = b.[REM_ID_Database] " _
  & "WHERE (((a.[REM_ID_Database])=""REM9811044""));"


来源:https://stackoverflow.com/questions/40054856/adodb-sql-syntax-access-table-inner-join-with-excel-worksheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!