首先,当涉及到 VBA 时,我有点新手,所以我所做的一切都有些偶然,但通常我最终会找出问题所在。但是这次我已经卡了好几天了,似乎找不到问题!
我有以下表格和具有以下结构的子表格。(Access2013)
主表单 [工作编号]子表单 [
Out2](这是用户将条形码扫描到相关字段的位置)子表单
[DS](这是从 [Out2] 扫描的条形码创建新记录的位置)
子表单 [DS] 字段: ID、作业编号、条形码、描述、日期、用户
我试图用下面的代码实现的是,在 [DS] BarCode 字段的“更新前”事件中,Dcount 函数将检查已在子表单容器 [DS] 中输入的条形码列表,如果有不止一个,它将撤消重复的条目。不幸的是,当输入重复条目时什么都没有发生。(甚至没有错误)
PS 设置表(无重复)的东西不适用于这个数据库。
Private Sub BarCode_BeforeUpdate(Cancel As Integer)
Dim BarCode As String
Dim strLinkCriteria As String
Dim rsc As DAO.Recordset
Set rsc = Me.RecordsetClone
BarCode = Me.BarCode.Text
strLinkCriteria = "[Barcode]=" & "'" & Replace(Me![BarCode], "'", "''")
'Check Items Subform for duplicate BarCode
If DCount("BarCode", "Forms![Job Number]![DS]", strLinkCriteria) > 0 Then
'Undo duplicate entry
Me.Undo
'Message box warning of duplication
MsgBox "Warning Item Title " _
& BarCode & " has already been entered." _
& vbCr & vbCr & "You will now been taken to the record.", _
vbInformation, "Duplicate Information"
'Go to record of original Title
rsc.FindFirst strLinkCriteria
Me.Bookmark = rsc.Bookmark
End If
Set rsc = Nothing
End Sub