ackdaddy
ackdaddy

Reputation: 131

Trying to SetFocus Based on If Criteria

I am trying to create a form where two employees will scan their badges when performing a certain task. What I am trying to do is make sure that both employees scan their badges and that one employee cannot scan his badge twice. A way to drive accountability if you will. I have a picture of a bar code which equals "OK" that the user will scan when the message box appears (just in case there was a question). Everything else works fine - I just cannot get the focus to go back to "Badge2" when Badge2 = "".

Code:

Private Sub Badge2_AfterUpdate()
If Me.Badge2.Value = Me.Badge.Value Then
Me.Badge2.Value = ""
MsgBox "You Must Scan Another Employee's Badge to Confirm Result! Scan OK to Scan Another Employee's Badge."
End If

If Me.Badge2.Value = "" Then
Me.Badge2.SetFocus
Else
Me.EID2.Value = DLookup("EID", "EmployeeID", "BADGENUMBER = form.Badge2")
Me.Employee2.Value = DLookup("Name", "EmployeeID", "BADGENUMBER = form.Badge2")

Sleep 2000

End If

End Sub 

Upvotes: 1

Views: 75

Answers (2)

ackdaddy
ackdaddy

Reputation: 131

Sorry for the jumbled code before HansUp. Still trying to get accustomed to formatting here. I believe what you wrote was right on the money, but I just needed to put in the Me.Badge2.Undo function and then the Cancel = True function to maintain focus on the Badge2. Thanks for putting up with my sloppiness and always helping.

Code:

Private Sub Badge2_BeforeUpdate(Cancel As Integer)
If Me.Badge2.Value = Me.Badge.Value Then
    Me.Badge2.Undo
    Cancel = True
    MsgBox "You Must Scan Another Employee's Badge to Confirm Result! Scan OK to Scan Another Employee's Badge."

Else
    Me.EID2.Value = DLookup("EID", "EmployeeID", "BADGENUMBER = form.Badge2")
    Me.Employee2.Value = DLookup("Name", "EmployeeID", "BADGENUMBER = form.Badge2")

    Sleep 2000

End If

End Sub

Upvotes: 1

HansUp
HansUp

Reputation: 97101

Use the Badge2_BeforeUpdate event instead of Badge2_AfterUpdate. When Badge2.Value does not satisfy your criteria, Cancel the update and focus will remain on Badge2.

Here is an abbreviated sample. Refine it to add in the rest of the functionality you need.

Private Sub Badge2_BeforeUpdate(Cancel As Integer)
    If Me.Badge2.Value = Me.Badge.Value Or Me.Badge2.Value = "" Then
        Cancel = True
    End If
End Sub

Upvotes: 1

Related Questions