Every bottle of research medication has a unique barcode. When checking in a new shipment I am trying to prevent accidentally scanning the same bottle twice. I have an unbound form that adds up to 16 barcode records at a time. Each of the 16 barcode controls are named txtUnitBC1 thru txtUnitBC16. A combobox controls how many of the 16 are visible and added to the record set.
I wrote the below function and entered = NoDuplicateBC() into each of the txtUnitBC Before Update Event. The function works as far as finding a duplicate barcode and alerts the user, but then moves to the After Update event function on handling the barcode rather than remaining on the Active control that caused the problem. I have tries several iterations of the below, but this was the lastest.
I understand how to this in a Sub txtUnitBC1_BeforeUpdate(Cancel As Integer), but I am trying to prevent writing 16 separate sub routines. I did read that Cancel = True is a property of the BeforeUpdate object and will not work elsewhere (a beginners interpretation of what I read). But I am lost on how to do the same in a function outside the BeforeUpdate object.
Any help would be appreciated.
You don't know what you don't know...
I wrote the below function and entered = NoDuplicateBC() into each of the txtUnitBC Before Update Event. The function works as far as finding a duplicate barcode and alerts the user, but then moves to the After Update event function on handling the barcode rather than remaining on the Active control that caused the problem. I have tries several iterations of the below, but this was the lastest.
I understand how to this in a Sub txtUnitBC1_BeforeUpdate(Cancel As Integer), but I am trying to prevent writing 16 separate sub routines. I did read that Cancel = True is a property of the BeforeUpdate object and will not work elsewhere (a beginners interpretation of what I read). But I am lost on how to do the same in a function outside the BeforeUpdate object.
Any help would be appreciated.
Code:
Private Function NoDuplicateBC()
Dim frm As Form
Dim ctrl As Access.Control
Dim strTempBC As String
Dim j As Integer
Dim strUnitBC As String
Set ctrl = Screen.ActiveControl
Set frm = Screen.ActiveForm
strUnitBC = "txtUnitBC"
strTempBC = ctrl.Value
For j = 1 To Me.cboQtyPackage
If ctrl.Name <> Me.Controls(strUnitBC & j).Name Then
If strTempBC = Me.Controls(strUnitBC & j).Value Then
MsgBox "Duplicate scanned barcode", vbExclamation
With Me.Controls(ctrl.Name)
.Undo
.SelStart = 0
.SelLength = Len(ctrl.Name)
End With
Exit Function
End If
End If
Next j
End Function
You don't know what you don't know...