添加错误" 3134" INSERT INTO语句中的语法错误 - 更新和删除错误' 3061'参数太少

时间:2017-03-21 18:18:36

标签: sql vba ms-access

我对Access的经验有限。几个月前,我关注了一些YouTube教程并制作了一个正常运行的数据库。

我改编了第一个DB,它实际上是在更改Access文件中表格中的字段名称。

我无法让新数据库正常运行。我有一个带有主表子表的表单,它有一些文本字段来填写要输入的信息。然后它有一些按钮,一边添加到表,从表中删除,清除文本字段,关闭表单,编辑一个选定的字段,然后编辑一个字段后,添加按钮更改为更新,以便在您对其进行更改后,可以单击“更新”以更新所选字段。

所有这些都在我的第一个数据库中工作,理论上它在更改新数据库中的字段名称和相应的txt字段名称后应该完全相同,依此类推。我很难将其用于添加,更新或删除。

添加的错误是运行时错误" 3134" INSERT INTO语句中的语法错误。

更新和删除错误是运行时错误' 3061'参数太少。预期1。

清除工作,以及关闭和编辑。

以下是代码:

Option Compare Database

Private Sub cmdAdd_Click()
    'when we click on button Add there are two options
    '1. for insert
    '2. for update
    If Me.txtICN.Tag & "" = "" Then
        'this is for insert new
        'add data to table
        CurrentDb.Execute "INSERT INTO tblInventory(ICN, manu, modelNum, serialNum, descr, dateRec, projectNum, dispo, flgDispo, dateRemoved, comments)" & _
        " VALUES(" & Me.txtICN & ", '" & Me.txtManu & "', '" & Me.txtModel & "', '" & Me.txtSerial & "', '" & Me.txtDescrip & "', '" & Me.txtDateRec & "', '" & Me.txtProjectNum & "', '" & Me.txtDispo & "', '" & Me.chkFlag & "', '" & Me.txtDateRemoved & "', '" & Me.txtComments & "')"
    Else
        'otherwise (Tag of txtICN store the Lab Inventory Control Number to be modified)
        CurrentDb.Execute "UPDATE tblInventory " & _
        " SET ICN = " & Me.txtICN & _
        ", manu = '" & Me.txtManu & "'" & _
        ", modelNum = '" & Me.txtModel & "'" & _
        ", serialNum = '" & Me.txtSerial & "'" & _
        ", descr = '" & Me.txtDescrip & "'" & _
        ", dateRec = '" & Me.txtDateRec & "'" & _
        ", projectNum = '" & Me.txtProjectNum & "'" & _
        ", dispo = '" & Me.txtDispo & "'" & _
        ", flgDispo = '" & Me.chkFlag & "'" & _
        ", dateRemoved = '" & Me.txtDateRemoved & "'" & _
        ", comments = '" & Me.txtComments & "'" & _
        " WHERE ICN = " & Me.txtICN.Tag
    End If

    'clear form
    cmdClear_Click
    'refresh data in list on form
    tblInventorySub.Form.Requery
End Sub

Private Sub cmdClear_Click()
    Me.txtICN = ""
    Me.txtManu = ""
    Me.txtModel = ""
    Me.txtSerial = ""
    Me.txtDescrip = ""
    Me.txtDateRec = ""
    Me.txtProjectNum = ""
    Me.txtDispo = ""
    Me.chkFlag = ""
    Me.txtDateRemoved = ""
    Me.txtComments = ""

    'focus on ICN text box
    Me.txtICN.SetFocus
    'set button edit to enable
    Me.cmdEdit.Enabled = True
    'change caption of button add to Add
    Me.cmdAdd.Caption = "Add"
    'clear tag on txtICN for reset new
    Me.txtICN.Tag = ""
End Sub

Private Sub cmdClose_Click()
    DoCmd.Close
End Sub

Private Sub cmdDelete_Click()
    'delete record
    'check existing selected record
    If Not (Me.tblInventorySub.Form.Recordset.EOF And Me.tblInventorySub.Form.Recordset.BOF) Then
        'confirm delete
        If MsgBox("Are you sure you want to delete this inventory entry?", vbYesNo) = vbYes Then
            'delete now
            CurrentDb.Execute "DELETE FROM tblInventory " & _
                "WHERE ICN = " & Me.tblInventorySub.Form.Recordset.Fields("ICN")
            'refresh data in list
            Me.tblInventorySub.Form.Requery
        End If
    End If
End Sub

Private Sub cmdEdit_Click()
    'check whether there exists data in list
    If Not (Me.tblInventorySub.Form.Recordset.EOF And Me.tblInventorySub.Form.Recordset.BOF) Then
        'get data to text box control
        With Me.tblInventorySub.Form.Recordset
            Me.txtICN = .Fields("ICN")
            Me.txtManu = .Fields("manu")
            Me.txtModel = .Fields("modelNum")
            Me.txtSerial = .Fields("serialNum")
            Me.txtDescrip = .Fields("descr")
            Me.txtDateRec = .Fields("dateRec")
            Me.txtProjectNum = .Fields("projectNum")
            Me.txtDispo = .Fields("dispo")
            Me.chkFlag = .Fields("flgDispo")
            Me.txtDateRemoved = .Fields("dateRemoved")
            Me.txtComments = .Fields("comments")
            'store ICN in Tag of txtICN in case id is modified
            Me.txtICN.Tag = .Fields("ICN")
            'change caption of button add to Update
            Me.cmdAdd.Caption = "Update"
            'disable button edit
            Me.cmdEdit.Enabled = False
        End With
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

dateRec,flgDispo,dateRemoved是哪些数据类型? DateTime字段的值必须用#not'分隔。 flgDispo是Yes / No类型吗?如果是这样,这是数字类型字段,数字值没有分隔符。

为什么要删除记录?你真的想失去历史吗?为什么不标记为“已存档”或“无效”?删除记录应该是一种罕见的事件。

我总是给子表单容器一个与它们所持有的对象不同的名字,比如ctrInventory:
CurrentDb.Execute“DELETE FROM tblInventory WHERE ICN =”& Me.ctrInventory!ICN

答案 1 :(得分:0)

首先,所有日期表达式的格式必须如下:

", dateRec = #" & Format(Me.txtDateRec.Value, "yyyy\/mm\/dd") & "#" & _

或者您可以应用我的CSql函数来处理所有这些:

Convert a value of any type to its string representation

然而,看起来你可以通过简单地将表单绑定来让自己的生活更轻松 - 然后删除所有这些代码,因为表单将处理编辑,插入和删除自动。