简体   繁体   English

Excel-SQL'INSERT INTO'查询语法错误

[英]Excel - SQL 'INSERT INTO' Query Syntax Error

I'm attempting a simple insertion query to an MS Access database with Data from Excel. 我正在尝试使用Excel中的数据对MS Access数据库进行简单的插入查询。 I'm using a parameterized query with hardcoded values for testing. 我正在使用带有硬编码值的参数化查询进行测试。 Running the query directly in Access works, but attempting to execute it via VBA results in a syntax error. 直接在Access中运行查询是可行的,但是尝试通过VBA执行查询会导致语法错误。 The query is currently inserting every field except for the auto-generated ID in the Access table. 查询当前正在插入除Access表中自动生成的ID以外的每个字段。

Sub Test_SQL()
    Dim conn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim result
    Dim id As Integer

    Dim year As Integer
    Dim month As String
    Dim combocode As String
    Dim credamt As Double
    Dim qty As Integer
    Dim itemcode As String
    Dim sepbundleamt As Double
    Dim prodname As String

    '*********query variables********************
    year = 2017
    month = "July"
    combocode = "COMBCOD1"
    credamt = 420
    qty = 21
    itemcode = "ITEMCOD1"
    sepbundleamt = 12
    prodname = "Test Product"

    '********open DB connection and execute insertion******
    On Error GoTo ErrorHandler

    Set conn = New ADODB.Connection
    Set cmd = New ADODB.Command
    With conn
        .Provider = "Microsoft.ACE.OLEDB.12.0;"
        .ConnectionString = [redacted]
        .Open
    End With

    With cmd
        .ActiveConnection = conn
        .CommandType = adCmdText
        .CommandText = "INSERT INTO SB_DSLR (Year, Month, ComboCode, CreditAmt, Qty, ItemCode, SepBundleAmt, ProductName) VALUES (?,?,?,?,?,?,?,?);"
    End With
    With cmd.Parameters
        .Append cmd.CreateParameter("Year", adInteger, adParamInput, 4, year)
        .Append cmd.CreateParameter("Month", adWChar, adParamInput, 10, month)
        .Append cmd.CreateParameter("ComboCode", adWChar, adParamInput, 10, combocode)
        .Append cmd.CreateParameter("CreditAmt", adDouble, adParamInput, 10, credamt)
        .Append cmd.CreateParameter("Qty", adInteger, adParamInput, 10, qty)
        .Append cmd.CreateParameter("ItemCode", adWChar, adParamInput, 8, itemcode)
        .Append cmd.CreateParameter("SepBundleAmt", adDouble, adParamInput, 10, sepbundleamt)
        .Append cmd.CreateParameter("ProductName", adWChar, adParamInput, 100, prodname)
    End With

    cmd.Execute 
    conn.Close
    Set cmd = Nothing
    Exit Sub

ErrorHandler:
    MsgBox (Err.Number & ": " & Err.Description)
    If conn.State = adStateOpen Then
        conn.Close
    End If
    Set cmd = Nothing
End Sub

There are multiple reasons for that general error but the fact that same query works in the MSAccess.exe suggests reserved words is the cause. 导致该一般错误的原因有多种,但同一查询在MSAccess.exe中起作用的事实表明是保留字

Indeed, Month and Year are Jet reserved words and Year is an MS Access reserved word . 实际上, 月份年份Jet保留字年份MS Access保留字 Both are named functions and parameters to other methods. 两者都被命名为其他方法的函数和参数。 Sometimes the .exe can be lenient whereas ODBC connections are more restrictive. 有时.exe可能比较宽松,而ODBC连接的限制更为严格。 Therefore, consider escaping such words with backticks or square brackets, or altogether avoiding such words. 因此,请考虑使用反引号或方括号对此类单词进行转义,或者完全避免使用此类单词。

.CommandText = "INSERT INTO SB_DSLR (`Year`, `Month`, ComboCode, CreditAmt, Qty, ItemCode, SepBundleAmt, ProductName) VALUES (?,?,?,?,?,?,?,?);"

.CommandText = "INSERT INTO SB_DSLR ([Year], [Month], ComboCode, CreditAmt, Qty, ItemCode, SepBundleAmt, ProductName) VALUES (?,?,?,?,?,?,?,?);"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM