StrSQL插入在VBA中抛出错误

时间:2017-11-15 18:53:03

标签: excel vba ms-access

我正在尝试将一些新记录插入到访问数据库中。

这是我的代码,

' OPEN ACCESS APPLICATION
    Set objAccess = CreateObject("Access.Application")
    objAccess.OpenCurrentDatabase DatabasePath

 ' OPEN INTERNAL DATABASE
    Set db = objAccess.CurrentDb


BuName = Ws.Sheets("Config").Range("T1").Value
RepName = Ws.Sheets("Config").Range("T2").Value
NumberofUniqueSheets = Ws.Sheets("Config").Range("T3").Value

For i = 1 To NumberofUniqueSheets

    CmpValue = Ws.Sheets("Config").Range("U1").Value
    ShtNamedec = Split(CmpValue, "$")(0)
    HeadRow = Split(CmpValue, "$")(1)
    AddInfo = Split(CmpValue, "$")(2)
    DateInfo = Split(CmpValue, "$")(3)

    ' PREPARE STATEMENT
    strSQL = "PARAMETERS [BusinessUnitparam] TEXT(255), [ReportNameparam] TEXT(255),[SheetNameparam] TEXT(255), [HeaderRowparam] INTEGER, [AdditionalInfoparam] TEXT(255), [DateRequiredinfoparam] TEXT(255);" _
               & "INSERT INTO ReportTypes ([BusinessUnit], [ReportName], [SheetName], [HeaderRow], [AdditionalInfo], [DateRequiredinfo]);"

    ' BUILD TEMP QUERYDEF
    Set qdef = db.CreateQueryDef("", strSQL)

    ' BIND PARAMS TO VARIABLES
    qdef!BusinessUnitparam = BuName
    qdef!ReportNameparam = RepName
    qdef!SheetNameparam = ShtNamedec
    qdef!HeaderRowparam = HeadRow
    qdef!AdditionalInfoparam = AddInfo
    qdef!DateRequiredinfoparam = DateInfo

    ' EXECUTE ACTION QUERY
    qdef.Execute

Next

    Set qdef = Nothing
    Set db = Nothing
    Set objAccess = Nothing

我在Build Temp QueryDef语句中遇到错误,错误显示为“

  

Insert into Statement中的语法错误。

` 我将值添加到Insert into语句中,如下所示

strSQL = "PARAMETERS [BusinessUnitparam] TEXT(255), [ReportNameparam] TEXT(255),[SheetNameparam] TEXT(255), [HeaderRowparam] INTEGER, [AdditionalInfoparam] TEXT(255), [DateRequiredinfoparam] TEXT(255);" _
               & "INSERT INTO ReportTypes ([BusinessUnit], [ReportName], [SheetName], [HeaderRow], [AdditionalInfo], [DateRequiredinfo]) VALUES (BuName, RepName, ShtNamedec, HeadRow, AddInfo, DateInfo);"

但它给出了另一个关于Execute Action Query语句的说法,说明预期的参数太少了12.这是表格结构供参考 -

ReportTypes

2 个答案:

答案 0 :(得分:1)

此查询适用于数据表设计的参数:

strSQL = "PARAMETERS [BusinessUnitparam] TEXT(255), [ReportNameparam] TEXT(255),[SheetNameparam] TEXT(255), [HeaderRowparam] INTEGER, [AdditionalInfoparam] TEXT(255), [DateRequiredinfoparam] TEXT(255);" _
           & "INSERT INTO ReportTypes ([BusinessUnit], [ReportName], [SheetName], [HeaderRow], [AdditionalInfo], [DateRequiredinfo])" _
           & "SELECT BusinessUnitparam AS Expr1, ReportNameparam AS Expr2, SheetNameparam AS Expr3, HeaderRowparam AS Expr4, AdditionalInfoparam AS Expr5, DateRequiredInfoparam as Expr6;"

答案 1 :(得分:0)

你的查询不好。

此:

INSERT INTO ReportTypes ([BusinessUnit], 
                         [ReportName], 
                         [SheetName], 
                         [HeaderRow], 
                         [AdditionalInfo], 
                         [DateRequiredinfo]) 
VALUES (BuName, 
        RepName,  
        ShtNamedec,  
        HeadRow,  
        AddInfo,  
        DateInfo)

不行。

虽然这可能:

"INSERT INTO ReportTypes ([BusinessUnit], 
                         [ReportName], 
                         [SheetName], 
                         [HeaderRow], 
                         [AdditionalInfo], 
                         [DateRequiredinfo]) 
VALUES ('" + BuName + "', 
        '" + RepName + "',  
        '" + ShtNamedec + "',  
        " + HeadRow + ",
        '" + AddInfo + "',  
        '" + DateInfo + "')"

您可以删除所有参数分配内容。我不认为它在这里增加价值。