Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Custom error messages in MS Access


Message #1 by "Janis Elmeris" <janise@h...> on Sun, 19 Jan 2003 20:18:11
Hi!

I have made a "MS Access" database and would like to know how can default 
error messages be intercepted and displayed messages written by me instead.

For example, I have set input mask for a date field in a form. If I enter 
the date incorrectly, this message appears:
_______________
The value you entered isn't valid for this field. For example, you may 
have entered text in numeric field or a number that is larger than the 
FieldSize setting permits.
_______________

Another message example, I want to change:
__________________
The field 'Sar.Datu' cannot contain a Null value because the Required 
property for this field is set to True. Enter a value in this field.
__________________

It is especially important to change the message because I in fact need it 
in another language! And also my computer-user interface lecturer requests 
it ;) .

Can anybody help?

Thanks! 

Janis
Message #2 by "Gregory Serrano" <SerranoG@m...> on Tue, 21 Jan 2003 18:56:16
Janis,

<< I have made a "MS Access" database and would like to know how can 
default error messages be intercepted and displayed messages written by me 
instead. >>

Suppose you have a button you click.  If you anticipate an error, find out 
what the error number is for that error; for example, purposely create the 
error and use a message box to tell you the error number (Err.Number).  
Once you know the error number(s), you can do this in your "On Click" 
event for that button.

   Private Sub cmdMyButton_Click()

      On Error GoTo Err_cmdMyButton_Click

      'Code here to do something when button is clicked w/no errors.

   Exit_cmdMyButton_Click:
      Exit Sub

   Err_cmdMyButton_Click:
      Select Case Err.Number
         Case XXXX
            'Code that runs with error no. = XXXX
            MsgBox "Error in blah...", vbExclamation, "Error " & Err.Number
         Case YYYY
            'Code that runs with error no. = YYYY
            MsgBox "Error in blah...", vbExclamation, "Error " & Err.Number
         Case Else
            'Code that runs with any other error no. NOT XXXX or YYYY.
            MsgBox Err.Description, vbExclamation, "Error " & Err.Number
      End Select
      Resume Exit_cmdMyButton_Click

   End Sub


The last case statement is Case Else, and in that one you can have the 
program give you the default "generic" Access error message 
(Err.Description) for any error number that you didn't anticipate.

Greg
Message #3 by "Janis E" <janise@h...> on Wed, 22 Jan 2003 00:35:49
Hello, Greg!

Thank you very much that you try to help me! :)
I hope, I will manage.

You said,
<<If you anticipate an error, find out what the error number is for that 
error; for example, purposely create the error and use a message box to 
tell you the error number (Err.Number).>>

Sorry, but I don't know how to find out the error number (using message 
box?). Could you, please, tell me that, too?

And, yes, I need to change error messages for actions on button clicks. 
But how will I change other errors? For example, those mentioned in my 
first post don't appear after a button click. They happen when I want to 
enter another field.

Thanks,
Janis
Message #4 by "Gregory Serrano" <SerranoG@m...> on Wed, 22 Jan 2003 18:18:06
Janis,

<< Sorry, but I don't know how to find out the error number (using message 
box?). Could you, please, tell me that, too?

And, yes, I need to change error messages for actions on button clicks. 
But how will I change other errors? For example, those mentioned in my 
first post don't appear after a button click. They happen when I want to 
enter another field. >>

That probably means you want to use it after one of the field's "After 
Update" event instead of a button's "On Click" event.  Go to the field 
that gives you the error message after you update it and put that same 
coding in its "After Update" event.

For the time being, change XXXX to an arbitrary number like 9999.  Get rid 
of the Case YYYY and its message box for now.  Run the program.  You'll 
get an error message.  The error number will not be 9999 (most likely) so 
the Case Else will run.  A message box should appear that says the error 
description and the error number at the top of the box.

Go back to the code.  Change the 9999 to the actual number you saw in the 
message box previously.

Greg
Message #5 by "Janis E" <janise@h...> on Thu, 23 Jan 2003 23:31:28
Hello, Greg!

* << For the time being, change XXXX to an arbitrary number like 9999.  
Get rid 
of the Case YYYY and its message box for now.  Run the program.  You'll 
get an error message.  The error number will not be 9999 (most likely) so 
the Case Else will run.  A message box should appear that says the error 
description and the error number at the top of the box.

Go back to the code.  Change the 9999 to the actual number you saw in the 
message box previously. >>

Thanks about that! Now I understood it and could it do earlier if had 
tried your code then...

By the way, Steve Klein is also trying to help me. :) And he has sent me 
the full list of errors and their numbers. If you want, I can sen it to 
you!

** << That probably means you want to use it after one of the 
field's "After 
Update" event instead of a button's "On Click" event.  Go to the field 
that gives you the error message after you update it and put that same 
coding in its "After Update" event. >>

Either the "After Update" field is not what I need, or I do something 
wrong.
I tried changing the code as you said for a _button_ On Click event and it 
worked! :)

I said "changing" because there already was a procedure for On Click for 
that button. And I understand, that I have to change the procedure that is 
responsible for handling with errors. So I did using the code you provided.

But, as I said, I need to change also error messages for edit boxes or 
combo boxes.
For example, I have an edit box that has an input mask for date entry. If 
I enter the numers that don't represent a valid date, this message appears:

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

So, what I tried to do in this case is:

1) went to TextBox "Datums" -> Properties -> Event -> After Update = 
[Event Procedure]

I wrote this code there in the Code view:
___________________________________________________
Private Sub Datums_AfterUpdate()
On Error GoTo Err_Datums_AfterUpdate

Err_Datums_AfterUpdate:
    Select Case Err.Number
         Case 2046
            'Code that runs with error no. = 2046
            MsgBox "Nevar atsaukt ierakstu, jo nekas jauns vēl nav 
ievadīts.", vbExclamation, "Šo darbību nevar veikt"
         Case 8888
            'Code that runs with error no. = YYYY
            MsgBox "Error in blah...", vbExclamation, "Error " & Err.Number
         Case Else
            'Code that runs with any other error no. NOT XXXX or YYYY.
            MsgBox Err.Description, vbExclamation, "Error " & Err.Number
      End Select

End Sub
__________________________________________________


But it didn't worked. :(

I understand, that this particular code wouldn't solve my problem 
completely, as I have yet to find the error number and write the 
corresponding code. I simply copied the above code from that I used for 
the button, only changed the procedure names.

Still, I can say that the code doesn't work properly (or simply is not 
used, is ignored), because the error box should at least show error's 
number now, but it is the same old default Access error message.

So, what should I do?


*** I have also discovered that the form itself has property fields for 
setting event on error:
Form Properties -> Event -> On Error = [Event Procedure]

I also tried writing the customization code there, but it didn't work, 
too. In fact, when I now entered an invalid date, at first a small message 
box appeared telling that there has been an error with number 0 occured. 
In addition, this error didn't have a description, so the box appeared 
really small.
And when I clicked on "OK", the box closed but the default error-message 
for invalid date-entry appeared as usual. So, now there were even 2 error 
messages for each error and that I don't want.


**** Another question. How can I change this message that is not an error 
but only an confirmation:

You are about delete 1 record(s)
..................................

That appears (of course) when I try to delete a record. I have a delete 
button for that in the form.
I have changed the error message for that button and it works fine - if I 
click on "No" (I have changed my mind and don't want to delete it, for 
example), my custom message appears, that informs me that the operation 
has cancelled.
But I must change also the confirmation about deletion.


***** And another question. How can I change names for buttons "OK", 
Cancel", "Yes", "No" within those error message boxes?


Again, thank you very much for helping and excuse me for my English as I 
come from Latvia. :)

Janis
Message #6 by "Janis E" <janise@h...> on Sun, 26 Jan 2003 22:52:17
Hello, Greg!

Could you (or anyone else), please, answer my further questions about the 
issue, mentioned in my previous reply here? I need this problem be solved 
soon, as I have to hand in my work for a mark at the university.

Thanks!
Janis
Message #7 by "Steve Klein" <Stephen@K...> on Mon, 27 Jan 2003 07:43:38 -0000
can you repeat the questions?


Steve K

p.s. did my example work?

-----Original Message-----
From: Janis E [mailto:janise@h...]
Sent: 26 January 2003 22:52
To: Access
Subject: [access] Re: Custom error messages in MS Access


Hello, Greg!

Could you (or anyone else), please, answer my further questions about the 
issue, mentioned in my previous reply here? I need this problem be solved 
soon, as I have to hand in my work for a mark at the university.

Thanks!
Janis

Message #8 by "Gregory Serrano" <SerranoG@m...> on Mon, 27 Jan 2003 21:34:49
Janis,

<< Still, I can say that the code doesn't work properly (or simply is not 
used, is ignored), because the error box should at least show error's 
number now, but it is the same old default Access error message. >>

Oh, that's because you altered the third part of the message box from what 
I had.  You wrote:

   MsgBox "Nevar atsaukt ierakstu, jo nekas jauns vēl nav ievadīts.", _
   vbExclamation, "Šo darbību nevar veikt"

You didn't keep what I orginally had for the third part.  Do it this way:

   MsgBox "Nevar atsaukt ierakstu, jo nekas jauns vēl nav ievadīts.", _
   vbExclamation, "Error " & Err.Number


In this way, the message box will put the phrase "Error ####" at the top 
left corner of the title bar.  Right now, all your message box will say 
is "Šo darbību nevar veikt".

Greg

  Return to Index