插入到参数太少

时间:2018-12-11 15:35:02

标签: sql vba ms-access

我有一个名为 tb_tools 的表,其中包含以下字段:

  • 工具ID (短文本)[key_field]
  • 说明(短文本)
  • RACK(短文本)
  • 列(数字)
  • 评论(长文本)

在名为frm_insertion的表单中,我有一个带有点击事件的按钮

Private Sub btn_add_Click()
    Dim part_temp As String
    Dim tempNum As Integer
    Dim tempStr As String

    tempNum = 12
    tempStr = "test"

    tool_temp = Nz(DLookup("[TOOL_ID]", "tb_tools", "[TOOL_ID]='" & [box_dien] & "'"), "-1")

    If StrComp(tool_temp, "-1", vbTextCompare) = 0 Then
        CurrentDb.Execute "INSERT INTO [tb_dies] ([TOOL_ID], [DESCRIPTION], [RACK], [COLUMN], [COMMENTS]) " _
            & "VALUES (" & tool_temp & "," & tempStr & "," & tempStr & "," & tempNum & "," & tempStr & ")"
    End If
End Sub

在INSERT INTO行中出现错误“参数太少。预期为1”。我检查了字段名称。实际上,我直接从表中将它们复制并粘贴到了代码中。

我也使用了此处提供的代码: http://forums.devarticles.com/microsoft-access-development-49/check-if-a-field-exists-in-a-table-using-vba-58559.html 检查字段是否可用

2 个答案:

答案 0 :(得分:2)

在查询中使用参数时,这可能更容易理解。

Private Sub btn_add_Click()

    Dim qdf As DAO.QueryDef

    Set qdf = CurrentDb.CreateQueryDef("", _
        "PARAMETERS lToolID LONG, DescText TEXT (255), RackText TEXT (255), " & _
        "ColumnNum LONG, CommentText LONGTEXT; " & _
        "INSERT INTO tb_dies (TOOL_ID, DESCRIPTION, RACK, COLUMN, COMMENTS) " & _
        "VALUES (lToolID, DescText, RackText, ColumnNum, CommentText)")

    With qdf
        .Parameters("lToolID") = 1
        .Parameters("DescText") = "Some Text"
        .Parameters("RackText") = "Some Rack Text"
        .Parameters("ColumnNum") = 5
        .Parameters("CommentText") = "Some really long comments"
        .Execute
    End With

    Set qdf = Nothing

End Sub  

SQL读取为:

PARAMETERS  lToolID LONG
            , DescText TEXT (255)
            , RackText TEXT (255)
            , ColumnNum LONG
            , CommentText LONGTEXT; 
INSERT INTO tb_dies (TOOL_ID, DESCRIPTION, RACK, COLUMN, COMMENTS) 
VALUES      (lToolID, DescText, RackText, ColumnNum, CommentText);

答案 1 :(得分:0)

您的SQL字符串包含:

private void PasteInNewLine()
{
    Word.Range cursorPosition = GetCurrentRange();
    cursorPosition.InsertParagraphAfter();
    cursorPosition.GoToNext(Word.WdGoToItem.wdGoToLine).Paste(); // wrong
    cursorPosition.Next();
    cursorPosition.Paste(); // also wrong
}

private Word.Range GetCurrentRange()
{
    object start = Globals.ThisAddIn.Application.Selection.Range.Sentences.First.Start;
    object end = Globals.ThisAddIn.Application.Selection.Range.Sentences.Last.End;
    return Globals.ThisAddIn.Application.ActiveDocument.Range(ref start, ref end);
}

这不是有效的SQL-现在,数据库服务器会将INSERT INTO [tb_dies] ([TOOL_ID], [DESCRIPTION], [RACK], [COLUMN], [COMMENTS]) VALUES (-1,temp,temp,12,temp,temp) 的这些出现视为变量或列名; temp可能需要使用单撇号(表示“字符串”),例如:

temp

因此,您可能希望自己的VB为:

INSERT INTO [tb_dies] ([TOOL_ID], [DESCRIPTION], [RACK], [COLUMN], [COMMENTS]) 
VALUES (-1,'temp','temp',12,'temp','temp')

PS;作为MS Access,有可能标准SQL不起作用,并且单个撇号不用于字符串。如果用双引号tempStr = "'test'" 表示访问中的字符串,您将看到VBlike:

"

评论者提出有效观点;您不应该使用字符串连接来构建SQL查询。.值得一读http://bobby-tables.com,以获得一些有关其坏处的背景信息,然后您将有能力做好准备走上一条软件开发,避免编写易受这种特殊(和常见)黑客形式攻击的软件