【问题标题】:Statement includes a reserved word or an argument name that is misspelled or missing or punctuation is incorrect语句包含拼写错误或缺失或标点不正确的保留字或参数名称
【发布时间】:2019-02-27 15:43:01
【问题描述】:

我收到以下代码的以下错误:

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

【问题讨论】:

  • 每次使用的错误是什么时候? SQL 的评估结果是什么,在执行之前strMakePaTablesSQL 是什么?
  • 你为什么要从你的主键中删除字符? strSupplier 被引用,所以唯一重要的是'。我建议使用参数化语句,这样您就不必担心这一点。如果您坚持使用 concat,请将其转义到 '' 而不是删除它。
  • 另外,无需提取供应商名称为[Vendor_Name] = '" & strSupplier & "'"

标签: sql vba ms-access


【解决方案1】:

然而,再次需要使用参数化,这是在应用程序层运行 SQL 时的行业最佳实践,这里是 VBA,但概念可以转移到大多数其他语言。具体来说,为您的生成表过程考虑 MS Access' QueryDefs,它不需要具有不同的定制名称并参数化 WHERE 子句中的值。

Below 使用 VBA 中引用的存储访问查询(比 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多