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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to trap an error number? 1

Status
Not open for further replies.

nakedbamboo

Technical User
Oct 8, 2003
36
0
0
US
I am trying to remember the way to get an error number so that I can figure out how to skip it if it comes up.
 
If it is VBA you are using. I use this code to move on

ON ERROR RESUME NEXT
(code that will possibly provide an error)
On Error GoTo 0
 
I actually want to use the error to display a custom message. But I only want to display it for certain errors, therefore, I would say

If Error = ##### Then
Message
Else
Skip errors
End If

I just don't know how to get that number.
 
The .Number property of the Err object will provide that information. I would suggest that you establish an error handler routine, and inside the error handler, you can with a case statement can react differently to each type of error.
Code:
Private Sub ProcName()
   On Error GoTo Error_Handler
  
   <your code>

Exit_ProcName:
Exit Sub

Error_Handler:

   Select Case Err.Number
      Case ERR_DEL_NOT_DONE_YET
         DoEvents
         Resume
      Case ERR_TYPE_MISMATCH
         Resume Exit_ProcName
      Case ERR_UNKNOWN_TABLE_COLUMN
         MsgBox &quot;this type of error shown here&quot;
         ColName = DefaultColumnName
         Resume Next
      Case Else
         ErrMessage &quot;&quot;, (Me.Name & &quot;.Form_AfterDelConfirm&quot;)
         Resume Next
   End Select

End Sub
I would also suggest that in a module, you define your errors as symbolic constants as in the above example, so that when you pick up the code, it's easy to see how which error is handled.
Code:
Global Const ERR_TYPE_MISMATCH = 13
Global Const ERR_DEL_NOT_DONE_YET = 2105
Global Const ERR_UNKNOWN_TABLE_COLUMN = 3265
Global Const ERR_SAVE_BEFORE_REQUERY = 2118
Global Const ERR_FORM_INVALID_REFERENCE = 2455

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Ok, I got it to spit out a number, butI am sure it is not the correct one for the displayed message. I have a box to enter the date in as mm/yyyy. If the numbers do not fit a date validation rule supplied by the system, it spits out a message saying:

&quot;The Value you entered is valid for this vield.&quot;

Plus an attempt at explaining what the problem may be. I was wanting to make my own message with a better explanation and hide theirs. If I put a message in the OnError for the form, it displays it and then their message. Can this error even be turned off?
 
Hi, nakedbamboo
An easy way to to display an Example of the format to use next to the input field. Date:___________ (MM/YYY) this way they know in advance what the format should be an therefore should not be surprised if they get an error message.

If you are using Numbers you can use the Validation rule and Validation Text(Field Prperties). If the rule does not match the user's info, then the Validation Text is displayed

Example:
Validation rule &quot;> 6 and < 100&quot;
Validation Text, &quot; you must enter a number Greater than 6, but less then 100.&quot;

If you are using an input mask you, could create your own using VB. This way access will not display an error unless its a data type error(text instead of numbers). (You would place this in the Lost Focus Event)

Hope this helps
Dalain
 
Excellent advice CajunCenturion!! This makes for a much more robust program. Access error numbers vary between MDAC's, Access versions, etc., and this way you maintain the code in ONE modularized section.


Jeffrey R. Roberts
Insight Data Consulting
Access, SQL Server, & Oracle Development
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top