Error Traping with VBA

Ammarhm

Beginner User
Local time
Today, 13:59
Joined
Jul 3, 2008
Messages
80
Hi All
Need some help with error traping
I have attached a file that illustrates the problem
The user enters 2 values a name (text ) and a number (number)
If the user enters a text in stead of number in the number text field, access protests with a text box
I have been trying to trap the error with VBA and to display a custom text box using (msgbox) command and at the same time disable access own message box
could anyone please help me with that??
 

Attachments

Personally I would choose prevention over cure.

If you use a function like IsNumeric() to check that the input the user has entered into a text box is in fact a number rather than a string of letters then you can stop the error occuring rather than trying to deal with it after its happened.

Hope that this helps :)
 
Hi All
Need some help with error traping
I have attached a file that illustrates the problem
The user enters 2 values a name (text ) and a number (number)
If the user enters a text in stead of number in the number text field, access protests with a text box
I have been trying to trap the error with VBA and to display a custom text box using (msgbox) command and at the same time disable access own message box
could anyone please help me with that??

I think this is similar to what you want to do.

http://www.databasedev.co.uk/custom-error-message.html
 
While doing error handling is important it should only be used for dealing with things that you haven't been able to deal with appropriately through the code.

Something as simple as this should be handled through a simple if statement
Code:
if not isnumeric(me.text1) then
    msgbox "You must enter a valid number"
else
    'Do what ever you was going to do
end if
 
I agree that prevention is generally better, however if the form has more than one data entry point handling errors may actually be a more effecient way of approaching the issue rather than putting code into each before update event for every control.

I did not download the attachment so I'm not sure what the OP's form looks like.
 
I didn't download the attachment either :P

I was actually thinking of having it when the user goes to click on "Save" or something like that. Yes you could check that it was appropriate BeforeUpdate but well as you said then you've got to have validation code on every control, whereas if you have it when you go to save the record or actually do something with the data then the code is in one place for validating the values which have been supplied by the user.
 
Thank you all for very valuable replies
However the problem is not solved by the code above, it might be a good idea to look at the database i attached
The thing is that the data entered into the text box is linked to a "control source" ie a column in a table that only accepts numeric data entries
the result is that as soon as the user types something in the text box which is not a number, as soon as the user "tabs away" ie the focus is shifted from the text box then the access pops up the error message
Please try to download the database to look at the data base constuction¨
I really appreciate any other suggestions as i am stuck here!
 
try the forms on error event, and intercept the error code

if its a invalid key entry - ie non numeric

then issue a sendkeys {esc} to undo the entry
 

Users who are viewing this thread

Back
Top Bottom