将 MS-SQL 数据导入 Access Table ADO

Import MS-SQL data into an Access Table ADO

希望您能帮助我或为我指明正确的方向。多年来,我一直使用一种非常简洁的方法将数据(在 VB6 中,是的!)从 MS-SQL table 导入 MS-Access database/table,如下所示: -

没有内连接的工作查询[=​​40=]

Dim myConnection As ADODB.connection
Dim mySQL As String
Set myConnection = New ADODB.connection
myConnection.Open "Driver={Microsoft Access Driver (*.mdb)};" & "Dbq=DB1.mdb;" & "DefaultDir=C:\Temp;" & "Uid=Admin;Pwd=;"
mySQL = "SELECT * INTO [Xray] FROM [ODBC;Driver=SQL Server; SERVER=myServer;DATABASE=myDatabase;UID=myUID;PWD=myPassword;].[ShipTo] WHERE [ShipTo].Company='ABC001'"
myConnection.Execute mySQL
Set myConnection = Nothing

但是,我现在第一次需要开始使用内部联接。我以为我可以做一些像上面一样简单的事情,但我已经摆脱了相当严重的困境,不知道如何进步。我的内部联接查询如下:-

SELECT        
    dbo.InvcHead.InvoiceNum AS DocNum, 
    SUM(dbo.InvcTax.TaxAmt) AS DocTaxAmt
FROM                        
    dbo.InvcTax
        INNER JOIN
    dbo.InvcHead ON (dbo.InvcTax.InvoiceNum = dbo.InvcHead.InvoiceNum) 
GROUP BY 
dbo.InvcHead.InvoiceNum**

由于我明显缺乏使用我久经考验的方法连接的经验,我收到一个错误,这可能是因为我的语法,但我就是看不到光。下面是新连接string/query尝试:-

内连接查询失败

Dim myConnection As ADODB.connection
Dim mySQL As String

Set myConnection = New ADODB.connection
myConnection.Open "Driver={Microsoft Access Driver (*.mdb)};" & "Dbq= DB1.mdb;" & "DefaultDir= C:\Temp;" & "Uid=Admin;Pwd=;"

mySQL = ""
mySQL = mySQL & "SELECT "
mySQL = mySQL & "dbo.InvcHead.InvoiceNum AS DocNum, "
mySQL = mySQL & "SUM(dbo.InvcTax.TaxAmt) As Doctaxamt "
mySQL = mySQL & "INTO [Xray] FROM [ODBC;Driver=SQL Server; SERVER= myServer;DATABASE= myDatabase;UID= myUID;PWD=myPassword;].[dbo.InvcTax] "
mySQL = mySQL & "Inner Join dbo.InvcHead ON (dbo.InvcTax.InvoiceNum = dbo.InvcHead.InvoiceNum)"
mySQL = mySQL & "Group By dbo.InvcHead.Invoicenum"

myConnection.Execute mySQL
Set myConnection = Nothing

我收到的错误消息是“连接操作中的语法错误”

是否可以通过内连接查询使用此方法进行提取?我怀疑我的问题与我引用连接的第一个 table 的脚本区域有关。

.[dbo.InvcTax] "

如果这不可能,是否有办法动态创建 table(正如我所做的那样),而不是从记录集中创建?

如能提供正确方向的任何帮助,我们将不胜感激。

谢谢, 杰克

dbo 前缀在 Access SQL 中失败,您错过了第二个 table 的数据库引用:

mySQL = ""
mySQL = mySQL & "SELECT "
mySQL = mySQL & "T1.InvoiceNum AS DocNum, "
mySQL = mySQL & "SUM(T1.TaxAmt) As Doctaxamt "
mySQL = mySQL & "INTO [Xray] FROM [ODBC;Driver=SQL Server;SERVER=myServer;DATABASE=myDatabase;UID=myUID;PWD=myPassword;].dbo.InvcTax AS T1 "
mySQL = mySQL & "INNER JOIN [ODBC;Driver=SQL Server;SERVER=myServer;DATABASE=myDatabase;UID=myUID;PWD=myPassword;].dbo.InvcHead AS T2 ON (T1.InvoiceNum = T2.InvoiceNum)"
mySQL = mySQL & "Group By T2.Invoicenum"