Custom Error Messages

D

DMc2004

What i would like to be able to do is to customise the error messages that appear in Access.

For example when you create a Table and you specify that Required is set to True, if a user does not enter data into that field then a unfrendly error message appears.

How can i change this?

Does anybody also have any links to usefull websites on similiar topics.

D
 
D

Duane Hookom

You should have all of your data entry through forms. This allows you to
trap errors in the Form Error event. The first step is to add a message box
in the form's error event that displays the error number and description.
You can then handle these error numbers with your own messages.

--
Duane Hookom
MS Access MVP


DMc2004 said:
What i would like to be able to do is to customise the error messages that appear in Access.

For example when you create a Table and you specify that Required is set
to True, if a user does not enter data into that field then a unfrendly
error message appears.
 
D

Duane Hookom

Here is some code that I have used in many apps for years. It is probably a
little dated since MsgBox() change in version 2000. There is a msgbox
replacement function for 2000 and later at
http://www.trigeminal.com/usenet/usenet015.asp?1033.

Public Function FormOnError(pintDataErr As Integer) As Integer
'===================================================================+
'This function can be called from the On Error event of a form |
' to trap data errors such as input mask or invalid data |
'Allow Access to handle other types of errors such as referential |
' integrity, record locking etc. |
'To implement this function, use the following syntax |
' Private Sub Form_Error(DataErr As Integer, Response As Integer) |
' Response = FormOnError(DataErr) |
' End Sub |
' Programmer: Duane Hookom |
' Date: 10/29/1998 |
' Parameters: pintDataErr is the Form Error number |
'===================================================================+
On Error GoTo FormOnError_Err
Dim strErrMsg As String 'For Error Handling
Const INPUTMASK_VIOLATION = 2279
Const DATAVALUE_VIOLATION = 2113 'bad date or too large of number or
text in numeric
Const LIMITTOLIST_VIOLATION = 2237
Const VALIDATION_VIOLATION = 2107 'violation of Validation
Select Case pintDataErr
Case INPUTMASK_VIOLATION, DATAVALUE_VIOLATION,
LIMITTOLIST_VIOLATION, VALIDATION_VIOLATION
Case Else 'Get out and let Access Handle all other errors
FormOnError = acDataErrDisplay
Exit Function
End Select
Dim strMsg As String
Dim strCtlName As String
Dim strCaption As String
Dim strAccessError As String
Dim strCurText As String
Dim intReturn As Integer
Dim ctrl As Control
Set ctrl = Screen.ActiveControl
intReturn = acDataErrContinue

strCtlName = ctrl.Name
strCurText = "The value entered"
On Error Resume Next 'in case ctrl doesn't have a text property or Label
strCurText = ctrl.Text
If ctrl.Controls.Count > 0 Then 'Get the label caption if present
strCaption = ctrl.Controls(0).Caption
End If
On Error GoTo FormOnError_Err 'set error handling back on
strAccessError = Application.AccessError(pintDataErr)
Select Case pintDataErr
Case VALIDATION_VIOLATION
strMsg = "The value " & strCurText & " isn't a valid entry.@" &
_
"The validation rule is: " & strCaption & " " _
& ctrl.ValidationRule & "." & vbCrLf & _
"Enter a valid value or press [Escape] to cancel your
changes.@"
Case INPUTMASK_VIOLATION
Select Case Left(ctrl.InputMask, 10)
Case "99/99/0000"
strMsg = strCurText & " is an invalid date format." & _
"@All dates must contain the century 'm/d/yyyy'.@"
Case Else
strMsg = "An input mask violation occurred in control "
strMsg = strMsg & strCtlName & "." & "@The text/numbers
must " & _
"match the format " & ctrl.InputMask & ".@"
End Select
Case DATAVALUE_VIOLATION
strMsg = strCurText & " is an incorrect value." & "@" & _
"For instance an invalid date or text in a " & _
"numeric field or value too large for the data field@"
Case LIMITTOLIST_VIOLATION
strMsg = strCurText & " is not in the list of options." & _
"@You must enter a value from the list.@"
Case Else
strMsg = "Form data error: " & pintDataErr & vbCrLf &
strAccessError
End Select
Beep 'Get the user's attention
MsgBox strMsg, vbInformation + vbOKOnly, strCaption & " Data Error"
On Error Resume Next
' Select the full text
ctrl.SelStart = 0
ctrl.SelLength = Len(ctrl.InputMask)

FormOnError_Exit:
FormOnError = intReturn
Exit Function

FormOnError_Err:
Select Case Err
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) &
vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.Description
MsgBox strErrMsg, vbInformation, "FormOnError"
Resume FormOnError_Exit
End Select
End Function
 
T

Tim Ferguson

How I trap errors?

Is there a list of error codes around?

In addition to Duane's catch-all error trapping, you can also use the
BeforeUpdate event on the form to look for particular things:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(txtMyRequiredControl.Value) Then
' send the user to the right control
txtMyRequiredControl.SetFocus
' tell him or her why
MsgBox "You left RequiredControl empty!", etc
' and call off the update
Cancel = True

ElseIf DCount("*", "Payments", "PayerID = " & txtIDNumber.Value) _
=0 Then
' No payments?
MsgBox "You haven't made any payments yet!"
sfmPaymentsSubForm.SetFocus
'
Cancel = True

ElseIf etc.IsWrong Then
Cancel = True

End If

' if Cancel is still false then things must be okay, so
' just do nothing

End Sub


and so on. The other smart thing to do is to provide DefaultValues for all
required fields -- because otherwise you can get unexpected errors when
doing INSERT commands in SQL.

Hope that helps


Tim F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top