Customizing System Messages.

S

Sameer

In my access project where i have set-up suitable relationships among
the tables, when an error occurs during data entry which violates the
relationship constraints, access displays a suitable message. It may be
like 'XXX databse doe not contain a related record.'
Can we customize this message so that it can more readable and does not
contain names of tables and will be more application specific and
general?
-Sameer
 
A

Allen Browne

If you have designed forms, you can use the Error event of the form to catch
these engine-level messages and replace them with your own.
 
J

Jey

Allen

I've tried this but the error is not caught
Do you have a sample of this?

I would like to catch data integrity problem.

Thanks in advance
 
A

Allen Browne

Here is a sample of a function to place in a standard module.

Then in the Form's Error event procedure:
Call FormError(Me, DataErr, Response)


Public Function FormError(frm As Form, DataErr As Integer, _
Response As Integer) As Integer
On Error GoTo Err_FormError
'Purpose: Generic Form_Error handler.
'Return: Response.
'Usage: In a form's Error event procedure:
' Call FormError(Me, DataErr, Response)
Dim strMsg As String
Const conDupeIndex As Integer = 3022
Const conFileMissing As Integer = 3024
Const conRelatedRecordRequired As Integer = 3201
Const conRequiredField As Integer = 3314
Const conInvalidType As Integer = 2113

Select Case DataErr
Case conDupeIndex
Response = acDataErrContinue
strMsg = "The record cannot be saved, as it would create a
duplicate."

Case conRelatedRecordRequired
Response = acDataErrDisplay

Case conRequiredField
strMsg = "This is a required field." & vbCrLf & vbCrLf & "Make an
entry, or press <Esc> to undo."
Response = acDataErrContinue

Case conInvalidType
strMsg = "Wrong type of data."
Response = acDataErrContinue

Case conFileMissing
strMsg = "Data file is currently unavailable."
Response = acDataErrDisplay

Case Else
Response = acDataErrDisplay
End Select

If Len(strMsg) > 0 Then
MsgBox strMsg, vbExclamation, "Invalid data"
End If
FormError = Response

Exit_FormError:
Exit Function

Err_FormError:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_FormError
End Function
 
J

Jey

Thanks Allen...
My problem now is that my sub is not callinf the Form_Error sub even if an
error happens...

My statments that are raising data integrity errors are :
DOCMD.OpenQuery or DOCMD.RUNSQL

thanks for help
 
A

Allen Browne

The form itself triggers Form_Error if an engine-level error occurs. VBA
does not call Form_Error.

To generate a trappable error when an action query fails, use
dbEngine(0)(0).Execute with the dbFailOnError switch, e.g.:
dbEngine(0)(0).Execute "Query1", dbFailOnError
 
Top