Custom error messages?

ekta

Registered User.
Local time
Yesterday, 21:51
Joined
Sep 6, 2002
Messages
160
Hi:

I want to create my own custom error message dialog boxes. For example I have a number field on my form. If user enters text in that field I want to display my own error message instead of the error message displayed by access.

Thanks

Ekta
 
That link is definitely useful but for a textbox, you want to evaluate the value before it is updated and reject it based on our conditions.

Code:
Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)
    If Not IsNumeric(Me.MyTextBox) Then
        MsgBox "Illegal characters in textbox.", vbExclamation, "Error"
        Cancel = True
    End If
End Sub
 
Will do.

Mile-O-Phile,

I tried the code but it doesn't do anything. It still shows the error message by Access.

Thanks,

Ekta
 
Private Sub Months_BeforeUpdate(Cancel As Integer)

If Not IsNumeric(Me.Months) Then
MsgBox "You cannot type text in this field. Please enter only number for months", vbExclamation, "Error"
Cancel = True
End If

End Sub
 
Why not slap them if they dare strike a non numeric key by testing which
keys are pressed in the text boxes KeyPress event?
Code:
Private Sub YourTextBoxName_KeyPress(KeyAscii As Integer)
On Error GoTo Err_YourTextBoxName_KeyPress
    
    If KeyAscii > Asc("9") Or KeyAscii = Asc(" ") Then
        KeyAscii = 0
        Beep
        MsgBox "The ''special field'' can not contain non numeric characters.", vbInformation, "Invalid Key Stroke"
    Else
        'do nothing since they keyed a number
    End If
    
Exit_YourTextBoxName_KeyPress:
    Exit Sub
    
Err_YourTextBoxName_KeyPress:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_YourTextBoxName_KeyPress
    
End Sub
HTH
 
Last edited:
It gives me compile error.."Sub or Function not defined" and highlights "ErrorMsg"
 
That looks like code copied straight from one of ghudson's databases. It's code he has to store a log of all errors that happen within his database.

You can cut the error handling part out for now:

Code:
Private Sub YourTextBoxName_KeyPress(KeyAscii As Integer)
    
    If KeyAscii > Asc("9") Or KeyAscii = Asc(" ") Then
        KeyAscii = 0
        Beep
        MsgBox "The ''special field'' can not contain non numeric characters.", vbInformation, "Invalid Key Stroke"
    End If
    
End Sub
 
purrrfect. Thanks
Does it make a difference if you don't include the error handling part?
 
Not particularly.

If you can anticipate an error happening then its good to intercept it and deal with it.
 

Users who are viewing this thread

Back
Top Bottom