如果表中已有记录,则Access不会从表单中插入记录

时间:2017-03-29 19:24:34

标签: database vba ms-access

基本上我有一个包含数据的表格,然后我想要一个表格,其中包含表格中每个字段的文本框,主表格的子表格合并到表格中。用户使用信息填写文本框,然后将其添加到表格中,或者他们可以单击记录并对其进行编辑或使用表单上的相应按钮将其删除。

我在使用某些语法时遇到了一些问题,最近修复了这个问题,所以现在我终于在我的表单上调整了一些内容。我现在唯一的问题是,如果表中已有记录,单击添加按钮后表单将不会插入新记录。我填写文本框并单击“添加”按钮,表单刷新,如添加命令工作,但然后新记录不会显示在表中。但是,如果我返回到表并删除表中已有的任何记录,然后返回到表单并再次填写框并单击“添加”按钮,则会在表单中插入新记录。 / p>

我在我正在处理的数据库之前创建了一个数据库,它运行正常。它与我现在正在处理的数据库非常相似,所以我只是复制它,然后重命名字段和文本框以及所有内容以匹配此数据库将要处理的信息。

我有

任何想法在这里发生了什么以及我如何解决?

以下链接指向数据库的一些屏幕截图:https://imgur.com/a/QvCY2

这是我的代码:

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, model, serial, descr, dateRec, dateRem, dispo, flgDispo, project, AMCA, UL, comments) " & _
        " VALUES(" & Me.txtICN & ", '" & Me.txtManu & "', '" & Me.txtModel & "', '" & Me.txtSerial & "', '" & Me.txtDescr & "', '" & Me.txtDateRec & "', '" & Me.txtDateRem & "', '" & Me.txtDispo & "', '" & Me.chkFlg & "', '" & Me.txtProject & "', '" & Me.txtAMCA & "', '" & Me.txtUL & "', '" & Me.txtComments & "')"
    Else
        'otherwise (Tag of txtICN stores the ICN of item to be modified)
        CurrentDb.Execute "UPDATE tblInventory " & _
        " SET ICN = " & Me.txtICN & _
        ", manu = '" & Me.txtManu & "'" & _
        ", model = '" & Me.txtModel & "'" & _
        ", serial = '" & Me.txtSerial & "'" & _
        ", descr = '" & Me.txtDescr & "'" & _
        ", dateRec = '" & Me.txtDateRec & "'" & _
        ", dateRem = '" & Me.txtDateRem & "'" & _
        ", dispo = '" & Me.txtDispo & "'" & _
        ", flgDispo = '" & Me.chkFlg & "'" & _
        ", project = '" & Me.txtProject & "'" & _
        ", AMCA = '" & Me.txtAMCA & "'" & _
        ", UL = '" & Me.txtUL & "'" & _
        ", comments = '" & Me.txtComments & "'" & _
        " WHERE ICN = " & Me.txtICN.Tag
    End If

    'clear form
    cmdClear_Click
    'refresh data in list on form
    frmInventorySub.Form.Requery

End Sub

Private Sub cmdClear_Click()
    Me.txtICN = ""
    Me.txtManu = ""
    Me.txtModel = ""
    Me.txtSerial = ""
    Me.txtDescr = ""
    Me.txtDateRec = ""
    Me.txtDateRem = ""
    Me.txtDispo = ""
    Me.chkFlg = ""
    Me.txtProject = ""
    Me.txtAMCA = ""
    Me.txtUL = ""
    Me.txtComments = ""

    'focus on ID 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.frmInventorySub.Form.Recordset.EOF And Me.frmInventorySub.Form.Recordset.BOF) Then
        'confirm delete
        If MsgBox("Are you sure you want to delete this item?", vbYesNo) = vbYes Then
            'delete now
            CurrentDb.Execute "DELETE FROM tblInventory " & _
                "WHERE ICN =" & Me.frmInventorySub.Form.Recordset.Fields("ICN")
            'refresh data in list
            Me.frmInventorySub.Form.Requery
        End If
    End If
End Sub

Private Sub cmdEdit_Click()
    'check whether there exists data in list
    If Not (Me.frmInventorySub.Form.Recordset.EOF And Me.frmInventorySub.Form.Recordset.BOF) Then
        'get data to text box control
        With Me.frmInventorySub.Form.Recordset
            Me.txtICN = .Fields("ICN")
            Me.txtManu = .Fields("manu")
            Me.txtModel = .Fields("model")
            Me.txtSerial = .Fields("serial")
            Me.txtDescr = .Fields("descr")
            Me.txtDateRec = .Fields("dateRec")
            Me.txtDateRem = .Fields("dateRem")
            Me.txtDispo = .Fields("dispo")
            Me.chkFlg = .Fields("flgDispo")
            Me.txtProject = .Fields("project")
            Me.txtAMCA = .Fields("AMCA")
            Me.txtUL = .Fields("UL")
            Me.txtComments = .Fields("comments")
            'store id of item in Tag of txtICN in case ICN 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

0 个答案:

没有答案