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!

Catching Access Textbox error

Status
Not open for further replies.

markronz

IS-IT--Management
Mar 20, 2007
93
US
Hello everyone-
I have a textbox on my form that is set to have a format of "general number". So it will not allow the user to type in any text into that field. However, if they copy and paste text into the field, it still lets them. Once you tab into different field, and there is text in there, if gives this error:

The value you entered isn't valid for this field.
For example, you may have entered text in a numerica field or a number that is larger than the FieldSize setting permits.

This is working exactly as planned, however, I'd like to be able to display my own custom error message for this error. Does anyone know how to catch this error? And where I would put that code?

Let me know if you need more details.

Thanks!
-Mark
 
Have a look at the Error event of the Form object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hello. Thank you both for your suggestions. I have tried playing around with both the Error event and Before Update event and I can't seem to get them to work correctly, so I was wondering if you be so kind to help me a little more. I was able to figure out that the error message that I mentioned above is error number 2113.

First, I'm not able to get the Form_Error event to work like I want it to because there are several textboxes on my page which could throw the same error message. Here is my code that I came up with:

Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
    If DataErr = 2113 Then
        Response = acDataErrContinue
        MsgBox "You messed up! Fix it!"
   End If
End Sub

Like I said, this works, and I can put a custom message in there, but I'd like to be able to tell which specific textbox caused the error. Does anyone know how to do that? Or do you think that the BeforeUpdate event would work better?

I tried playing with the BeforeUpdate event and this is my code:

Code:
Private Sub IDField_BeforeUpdate(Cancel As Integer)
    On Error GoTo Err_InvalidEntry
        If (Not IsNull(IDField.Value)) Then
            MsgBox "Valid Entry"
            Cancel = False
        Else
            MsgBox "Null Entry"
            Cancel = True
        End If
Err_InvalidEntry:

    If Err.Number = 2113 Then
        MsgBox "This in not numeric. Fix it!", , "Invalid Entry"
        Exit Sub
    Else
        'Write out the error and exit the sub
        MsgBox Err.Description & Err.Number
        Exit Sub
    End If
        
End Sub

If it's a valid entry or a null entry it still seems to throw a '0' error message for some reason. Not sure why that is. But my little catch I put in there for the 2113 error doesn't work at all. It still displays the default error message and ignores the custom one that I made.

So is anyone able to help me get my code to work with either the Error or BeforeUpdate events?

Thanks!
-Mark
 
I'd like to be able to tell which specific textbox caused the error
Perhaps you may use the ActiveControl property of the form object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That'll work! Thanks a lot for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top