How do I work around MS Access Error 3134 in VBA pass-through querydef to SQL Server?
Dim strQuery As String, strPlutus As String
Dim qDef As QueryDef
Dim db As DAO.Database
strPlutus = "qryPT_toPlutus"
Set db = CurrentDb
Set qDef = db.CreateQueryDef(strPlutus)
strQuery = "SELECT " & vbCrLf
strQuery = strQuery & "top 100 " & vbCrLf
strQuery = strQuery & "Carrier_UID " & vbCrLf
strQuery = strQuery & ", CarrierName " & vbCrLf
strQuery = strQuery & "from vw_ODBC_Carriers " & vbCrLf
strQuery = strQuery & "where TableKey=141 " & vbCrLf
strQuery = strQuery & "and Carrier_UID <> 1;" & vbCrLf
qDef.Connect = "ODBC;DSN=OAK;Description=OAK;UID=xxxxxx;Trusted_Connection=Yes;DATABASE=OAK"
qDef.SQL = strQuery
qDef.ODBCTimeout = 600
qDef.Close
db.Execute "INSERT INTO tblCarriers " & strPlutus
Your Pass-through query is ok, the problem is with this:
db.Execute "INSERT INTO tblCarriers " & strPlutus
which is
db.Execute "INSERT INTO tblCarriers qryPT_toPlutus"
which is obviously a syntax error and should be
db.Execute "INSERT INTO tblCarriers SELECT * FROM " & strPlutus
Note #1: SELECT TOP 100
should have an ORDER BY
clause (but it's probably just for testing).
Note #2: qDef.Close
doesn't do anything and can be removed. If anything, use Set qDef = Nothing
, but since it's a local variable, that's redundant too.
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.