下面的代码出现以下错误:
Select语句包含一个保留字或一个自变量名 拼写错误,丢失或标点不正确。
我找不到我使用的拼写错误或保留字,因此我认为某种标点符号丢失或不正确。由于我是新手,因此我很难弄清楚。任何帮助将不胜感激。
我遇到错误的代码的特定部分是
strMakePaTablesSQL = "SELECT [all vendor Rebates].[Key_Code_Name] as [Supplier Name], [all vendor Rebates].[Vendor_Name], " _
& " [all vendor Rebates].[Contract_ID], [all vendor Rebates].[EXP_DATE], " _
& " [all vendor Rebates].[Contract_Status], [all vendor Rebates].[Price Book Priority] as [High Priority Customer], " _
& " [all vendor Rebates].[GPO Or biosite] as [GPO Indicator], " _
& " [all vendor Rebates].[LTM_Rebate_Dollars] as [LTM Rebate Dollars] " _
& " into [" & strTableName & "] From [all vendor Rebates] " _
& " Where [Vendor_Name] = '" & strSupplier & "'"
dbPa.Execute strMakePaTablesSQL
完整代码:
Option Compare Database
Option Explicit
Dim dbXL As DAO.Database
Dim strDate As String
Dim strGenl As String
Dim strTableName As String
Dim fld As DAO.Field
Dim strGroupPASQL As String
Dim strMakePaTablesSQL As String
Dim StrPaID As String
Dim strGenl2 As String
Dim strSupplier As String
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Function MakeTableVN()
DoCmd.SetWarnings False
Dim dbPa As DAO.Database
Dim rstIePa As DAO.Recordset
Dim lngTotRecCount As Long
Dim tdf As DAO.TableDef
Dim strADR_Name As String
Dim VN_Length As Double
DoCmd.OpenQuery "qryCreate_Local_Supplier_Contact_table"
DoCmd.OpenQuery "qryAdd_New_Vendors_to_tblSupplierContact"
DoCmd.OpenQuery "qryUpdate_Supplier_Contact_List"
Set dbPa = CurrentDb
strGroupPASQL = "SELECT [all vendor rebates].Vendor_Name FROM [all vendor rebates] GROUP BY [all vendor rebates].Vendor_Name "
Set rstIePa = dbPa.OpenRecordset(strGroupPASQL, dbOpenDynaset)
With rstIePa
If Not (.BOF And .EOF) Then
.MoveLast
lngTotRecCount = .RecordCount
.MoveFirst
Do While Not .EOF
strSupplier = .Fields("Vendor_Name") & ""
strSupplier = Replace(strSupplier, ".", " ")
strSupplier = Replace(strSupplier, ":", " ")
strSupplier = Replace(strSupplier, "=", " ")
strSupplier = Replace(strSupplier, "/", " ")
strSupplier = Replace(strSupplier, "\", " ")
strSupplier = Replace(strSupplier, "'", "")
strSupplier = Replace(strSupplier, "*", " ")
strTableName = strSupplier
For Each tdf In dbPa.TableDefs
If tdf.Name = strTableName Then
dbPa.TableDefs.Delete tdf.Name
End If
Next
strMakePaTablesSQL = "SELECT [all vendor Rebates].[Key_Code_Name] as [Supplier Name], [all vendor Rebates].[Vendor_Name], " _
& "[all vendor Rebates].[Contract_ID], [all vendor Rebates].[EXP_DATE], [all vendor Rebates].[Contract_Status], [all vendor Rebates].[Price Book Priority] as [High Priority Customer], [all vendor Rebates].[GPO Or biosite] as [GPO Indicator], [all vendor Rebates].[LTM_Rebate_Dollars] as [LTM Rebate Dollars] into [" & strTableName & "] From [all vendor Rebates] Where [Vendor_Name] = '" & strSupplier & "'"
dbPa.Execute strMakePaTablesSQL
Debug.Print "C:\Testing\Expiring rebates\Vendor_Files\" & strSupplier & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strTableName, "C:\Testing\Expiring rebates\Vendor_Files\" & strSupplier & ".xls", True
DoCmd.DeleteObject acTable, strTableName
答案 0 :(得分:0)
但是,再次经典的是需要使用参数化,这是在应用程序层(这里是VBA)上运行SQL时的行业最佳实践,但是概念可以转移到大多数其他语言。具体来说,在您的制作表过程中考虑使用MS Access' QueryDefs,该过程无需使用其他定制名称,并在WHERE
子句中参数化该值。
以下使用在VBA中引用的存储的Access查询(比VBA字符串查询更有效)。
SQL (另存为查询对象:mySavedParamQuery)
PARAMETERS
子句仅在Access SQL语言中兼容。
PARAMETERS paramSupplier TEXT(255);
SELECT v.[Key_Code_Name] as [Supplier Name], v.[Vendor_Name],
v.[Contract_ID], v.[EXP_DATE],
v.[Contract_Status], v.[Price Book Priority] as [High Priority Customer],
v.[GPO Or biosite] as [GPO Indicator],
v.[LTM_Rebate_Dollars] as [LTM Rebate Dollars]
INTO tmpVendorTable
FROM [all vendor Rebates] v
WHERE v.[Vendor_Name] = paramSupplier;
SQL (另存为查询对象:mySavedAggregateQuery)
SELECT v.Vendor_Name
FROM [all vendor rebates] v
GROUP BY v,Vendor_Name
VBA (不显示SQL)
...
Dim qdef As QueryDef
Dim strFile As String
Set rstIePa = dbPa.OpenRecordset("mySavedAggregateQuery", dbOpenDynaset)
With rstIePa
If Not (.BOF And .EOF) Then
.MoveLast
lngTotRecCount = .RecordCount
.MoveFirst
Do While Not .EOF
strSupplier = .Fields("Vendor_Name")
strFile = "C:\Testing\Expiring rebates\Vendor_Files\" & strSupplier & ".xls"
For Each tdf In dbPa.TableDefs
If tdf.Name = "tmpVendorTable" Then
dbPa.TableDefs.Delete tdf.Name
End If
Next tdf
Set qdef = dbPa.QueryDefs("mySavedParamQuery") ' INITIALIZE QUERYDEF
qdef!paramSupplier = strSupplier ' BIND PARAMETER
qdef.Execute dbFailOnError ' EXECUTE ACTION
Debug.Print strFile
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"tmpVendorTable", strFile, True
.MoveNext
Loop
End If
End With