我上周发布了这个帖子,但我的婚礼是在本周末,所以我已经离开过去5天了,所以我再试一次,看看能否得到一些帮助
很多人问我为什么不使用绑定表格,我试过这个并且无法让它做我想做的事情。我一般都是Access和VBA的新手,所以我的知识范围是关注YouTube教程,但无论如何,如果有人可以指导我如何创建绑定表单和所有内容,我会接受你的建议。< / 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