首先,当我遇到VBA时,我有点新手,所以我所做的一切都有点受欢迎,但通常我最终会找出问题所在。 但是这次我被困了好几天似乎无法找到问题!
我有以下表格和子表格,结构如下。 (Access2013)
主要表格[工作号码]
子表单[Out2](这是用户将条形码扫描到相关字段的位置)
子表单[DS](这是[Out2]中扫描的条形码创建新记录的位置)
子表单[DS]字段:Id,作业号,条形码,描述,日期,用户
我试图通过下面的代码实现,在[DS] BarCode字段的'Before Update'事件中,Dcount函数将检查已经在子窗体容器[DS]中输入的条形码列表,以及如果有 不止一个它将撤消重复条目。不幸的是,输入重复条目时没有发生任何事情。 (甚至不是错误)
P.S。设置表(No Duplicates)对于此DB不起作用。
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
答案 0 :(得分:0)
以下是如何处理:
Private Sub BarCode_BeforeUpdate(Cancel As Integer)
Dim rsc As DAO.Recordset
Dim BarCode As String
Dim Criteria As String
Set rsc = Me.RecordsetClone
BarCode = Nz(Me!BarCode.Value)
Criteria = "[Barcode] = '" & Replace(BarCode, "'", "''") & "'")
rsc.FindFirst Criteria
Cancel = Not rsc.NoMatch
If Cancel = True Then
' Message box warning of duplication
MsgBox "Warning Item Title " _
& BarCode & " has already been entered." _
& vbCrLf & vbCrLf & "You will now been taken to the record.", _
vbInformation, "Duplicate Information"
' Go to record of original Title
Me.Bookmark = rsc.Bookmark
End If
Set rsc = Nothing
End Sub