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!

Dealing with errors 1

Status
Not open for further replies.

sparkbyte

Technical User
Sep 20, 2002
879
US
I need a way to keep the users from continuing to the next field until the error has been corrected.

Here is one of the data validation rules that I have setup.

I was thinking of wrapping a do while loop around it or parts of it but can't seem to get it right.

Could someone show an example so that I could apply that to the rest of my code.

Thanks!!

Code:
Private Sub BoxNumber_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler

Dim dl As String
dl = vbNewLine & vbNewLine

If Len(Me.BoxNumber) < 14 Then
    Select Case left(UCase(Me.BoxNumber.Value), 3)
        Case UCase("nbc")
            Exit Sub
        Case "SRC", "LIN", "WAC", "EAC", "MSC"
            Cancel = True
            BeepWhirl
            MsgBox Me.BoxNumber & " Appears to be a Reciept Number" & dl & _
                   "Please Correct the Tracking Number ...", _
                   vbExclamation + vbOKOnly, _
                   "ERROR!"
            Exit Sub
        Case Else
            Cancel = True
            BeepWhirl
            MsgBox Me.BoxNumber & " is not a Valid Tracking Number" & dl & _
                   "Please Correct the Tracking Number ...", _
                   vbExclamation + vbOKOnly, _
                   "ERROR!"
            Exit Sub
    End Select
End If

endit:
Exit Sub

Err_Handler:
 If StandardErrors(Err) = False Then
    BeepWhirl
    MsgBox Err & ": " & Err.Description
 End If
Resume endit

End Sub

Thanks

John Fuhrman
 
Just a little more information.

The problem I am having is when a user scans a barcode for a file number rather that a Tracking Number. (Gets out of order) If they are not paying attention and scan 2 more File Numbers, The first scan gets rid of the error message (msgbox) and the next scan makes the Tracking number 2 File Numbers back to back which passes the data validation and allows them to continue.

What I need it to do is clear all fields and force them to rescan the Tracking Number.



Thanks

John Fuhrman
 
Would it make sense to use the On Exit property of the control? If the field isn't properly completed, you could cancel the exit - forcing them to stay IN the field until they do it right. ??
 
Cancel = True
[!]Me.Undo[/!]
BeepWhirl

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Not sure it the On Exit event would work.

I am currently using the Before Update and After Update.

Here is the code for both.

Code:
Private Sub BoxNumber_AfterUpdate()
On Error GoTo Err_Handler

    If left(UCase(Me.BoxNumber.Value), 3) = "NBC" Then
        Me.BoxNumber.Value = UCase("nbc" & DatePart("yyyy", Now) & _
        DatePart("m", Now) & DatePart("d", Now) & DatePart("h", Now) & _
        DatePart("n", Now) & DatePart("s", Now)) & "-" & LRandomNumber()
    ElseIf left(UCase(Me.BoxNumber.Value), 3) = "HQB" Then
        Me.BoxNumber.Value = UCase("HQB" & DatePart("yyyy", Now) & _
        DatePart("m", Now) & DatePart("d", Now) & DatePart("h", Now) & _
        DatePart("n", Now) & DatePart("s", Now)) & "-" & LRandomNumber()
    Else
        'Do Nothing
    End If
    
endit:
Exit Sub

Err_Handler:
 If StandardErrors(Err) = False Then
    BeepWhirl
    MsgBox Err & ": " & Err.Description
 End If
Resume endit

End Sub
Code:
Private Sub BoxNumber_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler

Dim dl As String
dl = vbNewLine & vbNewLine

If Len(Me.BoxNumber) < 14 Then
    Select Case left(UCase(Me.BoxNumber.Value), 3)
        Case UCase("nbc")
            Exit Sub
        Case "SRC", "LIN", "WAC", "EAC", "MSC"
            Cancel = True
            BeepWhirl
            MsgBox Me.BoxNumber & " Appears to be a Reciept Number" & dl & _
                   "Please Correct the Tracking Number ...", _
                   vbExclamation + vbOKOnly, _
                   "ERROR!"
        Case Else
            Cancel = True
            BeepWhirl
            MsgBox Me.BoxNumber & " is not a Valid Tracking Number" & dl & _
                   "Please Correct the Tracking Number ...", _
                   vbExclamation + vbOKOnly, _
                   "ERROR!"
    End Select
    
End If

endit:
Exit Sub

Err_Handler:
 If StandardErrors(Err) = False Then
    BeepWhirl
    MsgBox Err & ": " & Err.Description
 End If
Resume endit

End Sub

I will try the me.undo and let you know.

Thanks

John Fuhrman
 
John - you won't need to do an undo with the code on the on exit property. You might need to display the message before you do the cancel = true line. Putting that cancel on the on exit will make it so the user cannot leave the field at all - which sounds like it's what you need!
 
A couple things come to mind when I think of your problem.

Im assuming that when they scan a barcode, it issues an ASCII 13 or 32 (Enter or Space) which causes the OK button to be selected on the message box.

One way that might work: (this only works if a TAB is not issued)

Set your msgbox up with OK and CANCEL buttons.
Place your message box in a loop that only exits with the OK button pressed.
Set the focus of you message box to the CANCEL button.

Use the following example

Dim hAnswer As Long

'vbOK value = 1
'vbCancel value = 2
'Clicking the CLOSE button does not affect the value of hAnswer

Do While hAnswer <> vbOK
hAnswer = MsgBox("MESAGE TO USER", vbDefaultButton2 + vbExclamation + vbOKCancel, "TITLE OF MSGBOX")
Loop



As long as the keep scanning, it will press the button with the focus (CANCEL) until a tab is pressed or OK is selected with the mouse.

Another way is to create your own custom message box using a form. As long as your OK button doesnt have the focus, it won't close.

One more idea. If they are required to enter the data in a specific order. You can 'disable' all the textbox controls on the form, then when a control passes the validation checks, the current control is set to disable, and the next is enabled.

If the focus is moved to the next control, but fails the validation check, your "After_Update" event can issue the Application.Screen.PreviousControl.SetFocus to return the focus back the the control with the bad data, clear the value, and be ready to accept new data.
 
Thanks for the suggestions Rusty! That is quite a bit to think about. I will try each of them and see which one works out the best.

Sorry it took so long to respond. I had some last minute trouble shooting to do on the production DB.



Thanks

John Fuhrman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top