尝试使用Access

时间:2017-03-22 18:53:08

标签: database ms-access access-vba

我昨天发布了这个,并且已经做了更改并试图尝试修复它并且仍然遇到问题。

我正在处理数据库工作,并且在尝试向表单添加记录时遇到编译错误。

它本质上是一个库存系统,我有一个包含仓库库存的表格,我试图创建一个表格,其中包含一个设备上每条信息的文本框。因此,您转到表单,用信息填写每个文本框,然后在文本框的侧面有5个框,您单击以执行不同的操作。下面是主库存表的子表单。

有一个添加,编辑,删除,清除和关闭按钮。 “添加”按钮将记录添加到此子表单,从而将主表格添加到您在文本框中键入的任何信息。要使用“编辑”框,可以单击子窗体中的记录,然后单击“编辑”按钮,它将使用所选记录中的信息填充文本框,以便您可以编辑信息。同时,单击“编辑”后,它会将“添加”按钮上的标签更改为“更新”,以便在编辑数据后单击“更新”,它将更新子窗体和主表中的数据。其他3个按钮执行他们应该执行的操作,单击一个记录并单击删除以删除记录,单击关闭以退出表单并单击清除以清除文本框中填充的所有信息。

我遇到的问题是“添加/更新”按钮不起作用 - 当我点击它时出现此错误:编译错误:找不到方法或数据成员。每个其他按钮的工作完全正常,我已经编写并重写了两次这段代码,并且无法弄清楚发生了什么。

最奇怪的部分是我基本上将这个代码复制到我编码的另一个数据库中,该数据库完全符合预期,并且与此类型非常相似。切换到此数据库时,我唯一需要更改的是标签和表头的名称。

有人可以帮助我吗?

代码:

Option Compare Database

Private Sub cmdAdd_Click()
    'when we click on Add button there are two options
    '1. for inserting new data
    '2. for updating selected data
    If Me.txtICN.Tag & "" = "" Then
        'add data to table after clicking the add button
        CurrentDb.Execute "INSERT INTO tblInventory(ICN, manu, modelNum, serialNum, descr, dateRec, projectNum, dispo, flgDispo, dateRemoved, comments, ulNum, amcaNum)" & _
            "VALUES (" & Me.txtICN & ", '" & Me.txtManu & "', '" & Me.txtModel & "', '" & Me.txtSerial & "', '" & Me.txtDesc & "', '" & Me.txtDateRec & "', '" & Me.txtProjectNum & "','" & _
            Me.txtDispo & "', '" & Me.chkDispo & "', '" & Me.txtDateRemoved & "', '" & Me.txtComments & "', '" & Me.txtULNum & "', '" & Me.txtAMCANum & "')"

    Else
        'otherwise
        CurrentDb.Execute "UPDATE tblInventory " & _
            " SET ICN =" & Me.txtICN & _
            ", manu ='" & Me.txtManu & "'" & _
            ", modelNum ='" & Me.txtModel & "'" & _
            ", serialNum ='" & Me.txtSerial & "'" & _
            ", descr ='" & Me.txtDescr & "'" & _
            ", dateRec ='" & Me.txtDateRec & "'" & _
            ", projectNum ='" & Me.txtProjectNum & "'" & _
            ", dispo ='" & Me.txtDispo & "'" & _
            ", flgDispo ='" & Me.chkDispo & "'" & _
            ", dateRemoved ='" & Me.txtDateRemoved & "'" & _
            ", comments ='" & Me.txtComments & "'" & _
            ", ulNum ='" & Me.txtULNum & "'" & _
            ", amcaNum ='" & Me.txtAMCANum & "'" & _
            " WHERE ICN =" & Me.txtICN.Tag
    End If

    'clear form after data has been added to the table
    cmdClear_Click
    'refresh data in list on form after form has been cleared
    frmInventorySub.Form.Requery

End Sub

Private Sub cmdClear_Click()

    Me.txtICN = ""
    Me.txtManu = ""
    Me.txtModel = ""
    Me.txtSerial = ""
    Me.txtDesc = ""
    Me.txtDateRec = ""
    Me.txtProjectNum = ""
    Me.txtDispo = ""
    Me.chkDispo = ""
    Me.txtDateRemoved = ""
    Me.txtComments = ""
    Me.txtULNum = ""
    Me.txtAMCANum = ""

    'set focus to ICN number
    Me.txtICN.SetFocus
    'set edit button to enabled after data has been cleared from form
    Me.cmdEdit.Enabled = True
    'change caption of button add to Add from Edit
    Me.cmdAdd.Caption = "Add"
    'clear tag on txtICN
    Me.txtICN.Tag = ""
End Sub

Private Sub cmdClose_Click()
    DoCmd.Close
End Sub

Private Sub cmdDelete_Click()
    'delete record
    'check existingselected 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 record?)", 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 to see if data is already in the form
    If Not (Me.frmInventorySub.Form.Recordset.EOF And Me.frmInventorySub.Form.Recordset.BOF) Then
        'pull data from selected record into the text boxes
        With Me.frmInventorySub.Form.Recordset
            Me.txtICN = .Fields("ICN")
            Me.txtManu = .Fields("manu")
            Me.txtModel = .Fields("modelNum")
            Me.txtSerial = .Fields("serialNum")
            Me.txtDesc = .Fields("descr")
            Me.txtDateRec = .Fields("dateRec")
            Me.txtProjectNum = .Fields("projectNum")
            Me.txtDispo = .Fields("dispo")
            Me.chkDispo = .Fields("flgDispo")
            Me.txtDateRemoved = .Fields("dateRemoved")
            Me.txtComments = .Fields("comments")
            Me.txtULNum = .Fields("ulNum")
            Me.txtAMCANum = .Fields("amcaNum")
            'store ICN in tag of txtICN in case ICN is modified
            Me.txtICN.Tag = .Fields("ICN")
            'change caption of add button to Update
            Me.cmdAdd.Caption = "Update"
            'disable edit button
            Me.cmdEdit.Enabled = False

        End With
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

好的,我现在看到了。如果记录是否已编辑,则使用TAG属性作为指示符。即如果是新记录,则TAG =“”,如果是现有记录,则将ICN复制到TAG中。

如果是这种情况,那么将IF语句改为:

If Me.txtICN.Tag = "" Then