Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function in Before Update Event works but does not prevent moving to After Update 1

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
US
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.

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...
 
Thanks MajP, worked perfectly.

You don't know what you don't know...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top