Question access error messages (1 Viewer)

Jimcb8

Registered User.
Local time
Today, 03:14
Joined
Feb 23, 2012
Messages
98
Is there a way for me to issue my own more descriptive error message then the messages issued by Access?
For example,
I defined a unique compound index and a user enters a duplicate, access will display a message like "a primary key or index contains a duplicate ETC....."
I would like to say to the user "duplicate dates in the date attended field are not allowed"

Can this be done?

Your thoughts are appreciated.

Jim
 

CraigDolphin

GrumpyOldMan in Training
Local time
Today, 00:14
Joined
Dec 21, 2005
Messages
1,582
Yep...set a trap in your error handling routine.

Something like...
Code:
On Error GoTo Err_Image85_DblClick

<Your VBA here>

Exit_Image85_DblClick:
    Exit Sub

Err_Image85_DblClick:
   If Err.Number = [COLOR="Red"]12345[/COLOR] then
     Msgbox "Your Custom Message here",vbinformation
   Else
      MsgBox Err.Description
   End if
    Resume Exit_Image85_DblClick
 

Jimcb8

Registered User.
Local time
Today, 03:14
Joined
Feb 23, 2012
Messages
98
I am really new to Access so my questions to you may sound lame.
How do I know what the error number is, it does not show when I see it.
What" VBA goes here" what code should I put there?
Lastly, Where does all this code you gave me go, where do I put it?
So Sorry for not knowing these items.

Thanks for your patience with me,

Jim
 

MS$DesignersRCretins

Registered User.
Local time
Today, 02:14
Joined
Jun 6, 2012
Messages
41
Substituting error messages might be a bit ambitious for your experience level, although not prohibitively. I would suggest working your way up to doing that. I will address the part about error number by suggesting that you get handy with debugging.

"Debugging" does not connote "fixing a bug" or correcting anything wrong (though it can do so). It's actually a great learning tool. This is the most efficient way to observe what are the contents of variables, moreover of properties. (It is also a semi-lazy way to confirm just what properties are available. :D) I'm referring to the Immediate Window and Watch Window. Find out what information you can get in these, and consider the amount of effort to get it, and the immediacy of the result. Also consider (and try out) these tradeoffs versus
Application.StatusBar = "Doing item " & i & "; "total so far =" & dTotal
Or maybe debug.print "Doing item " & i & "; "total so far =" & dTotal
Or maybe prefacing either with
if iCount mod 100 < 1 then

Anyway I recommend that any beginning hit on these topics in more than a textual or superficial sense. This is the fast way to learn what is going on in all the guts.

Back to "Err." You can put a breakpoint on the error handler code and at the time of the break, type in the immediate window
?Err.number
Or you can click on a letter in the word number and go shift-F9. Or you can do so on the word Err in your code. You can then proceed to click "Add" and drill its contents in the Watch Window. Note what values it has (or the same value every time). Compare that to error code information you get from hitting F2 key.

The point of all this is that you can really get intimate with Err this way. And if you're time-efficient in doing this (because you can't debug EVERYthing), you can get a lot of intimate reward from your code. Woohoo!

As to "VBA goes here", that's where you'd type in your custom text as you mentioned earlier.

As to where it goes: I would google the topic of error handling in VBA. The general convention is at the end of subroutines and functions, a la
Code:
Sub Mysub
Dim....
On Error GoTo Err1Handler
....
code
....
Err1Handler:
{put the handler code here}
Exit sub 'redundant in this case
End Sub
Hope this at least gets you started. I have limited availability, so others are encouraged to jump in.
 

Jimcb8

Registered User.
Local time
Today, 03:14
Joined
Feb 23, 2012
Messages
98
Thank you
I now have a good road map to follow.

I appreciate the help

JIM
 

Users who are viewing this thread

Top Bottom