Question Customising Default Error Messages (1 Viewer)

Ezz

New member
Local time
Today, 19:48
Joined
Jan 14, 2010
Messages
5
Hi I have customised two error messages using the vb script below, which were copied from the net. The new error messages are only displayed when using the forms navigation buttons. Also on my form is a command "Save" button which when pressed only activates the default error massages.
Is there a way to make my "Save" button display the new error messages when pressed?
I am a novice to access so don't get too technical.



Private Sub Form_Error(DataErr As Integer, Response As Integer)
' If an error occurs because of duplicate data in a required field
' Display own custom error message

Const conErrDuplicateKey = 3022
Const conErrZeroKey = 3058

Select Case DataErr
Case conErrDuplicateKey
MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
Response = acDataErrContinue

Case conErrZeroKey
MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"
Response = acDataErrContinue

Case Else

'Display a standard error message
Response = acDataErrDisplay
End Select
End Sub
 

Guus2005

AWF VIP
Local time
Today, 20:48
Joined
Jun 26, 2007
Messages
2,641
Here's a sample. This is one way to do it.
Code:
Option Compare Database
Option Explicit

Sub SomeProcedureWithLinenumbers()

6 On Error GoTo Err_SomeProcedureWithLinenumbers

8    Dim intX As Integer

    'Generating error :Div/0
11    intX = intX / 0
    
'Body of procedure here
    
Exit_SomeProcedureWithLinenumbers:
16    Exit Sub
    
Err_SomeProcedureWithLinenumbers:
19    ErrorProc Err, Error$, "SomeProcedureWithLinenumbers", "YourModuleName", Erl
20    Resume Exit_SomeProcedureWithLinenumbers
End Sub
Sub SomeProcedureWithoutLinenumbers()

On Error GoTo Err_SomeProcedureWithoutLinenumbers

    Dim intX As Integer

    'Generating error :Div/0
    intX = intX / 0
    
'Body of procedure here
    
Exit_SomeProcedureWithoutLinenumbers:
    Exit Sub
    
Err_SomeProcedureWithoutLinenumbers:
    ErrorProc Err, Error$, "SomeProcedureWithoutLinenumbers", "YourModuleName", Erl
    Resume Exit_SomeProcedureWithoutLinenumbers
End Sub

[B]Public Sub ErrorProc(pintErr As Integer, pstrError As String, pstrProc As String, pstrMod As String, Optional lngErrorLine As Long = 0)
  On Error GoTo ErrorProc_Err
  
    Dim strMsg As String
    Dim strErrorLoc As String

    'Use a select case statement(lengthy) or retrieve the values from the database.
    Select Case pintErr
    Case 3022
        pstrError = "DuplicateKey"
    Case 3058
        pstrError = "ZeroKey"
    Case 6
        pstrError = "Division by Zero"
    Case Else
        'No error message made up yet. System provided error is used.
    End Select
    
    ' Inform the user what happend.
    strMsg = Application.Name & " encountered an unexpected error." & vbCrLf & vbCrLf & vbCrLf & _
             "Function: " & pstrProc & vbCrLf & "Module: " & pstrMod & "." & vbCrLf & _
             "Access returned error number " & pintErr & "." & vbCrLf & vbCrLf & "" & vbCrLf & _
              pstrError

    If lngErrorLine > 0 Then strMsg = strMsg & vbCrLf & vbCrLf & "On line : " & lngErrorLine

    On Error Resume Next
    
    MsgBox strMsg, vbCritical, Application.Name

ErrorProc_Exit:
    On Error GoTo 0
    Exit Sub

ErrorProc_Err:
    strMsg = Application.Name & " encountered an unexpected error" & vbCrLf & vbCrLf & "" & vbCrLf
    strMsg = strMsg & " in the System Error handler." & vbCrLf
    strMsg = strMsg & "Access returned error number " & Err & "." & vbCrLf & vbCrLf & "" & vbCrLf
    strMsg = strMsg & Error$

    MsgBox strMsg, vbCritical, "ErrorProc procedure"
  
    Resume ErrorProc_Exit

End Sub[/B]
Two procedures, one with linenumbers and one without.
One error procedure ErrorProc which shows the customised error messages.

It is not very complicated. Copy and paste the code into a module and run SomeProcedure...

HTH:D

[edit]Added sample database with userdefined error messages from table[/edit]
 

Attachments

  • UserDefinedErrorProc.mdb
    196 KB · Views: 123
Last edited:

Ezz

New member
Local time
Today, 19:48
Joined
Jan 14, 2010
Messages
5
Thanks
I have copied and pasted your code into my database but when i try and run it, it throws up a compile error, "Variable not Defined" and highlights the word Search. See highlighted and underlined below? I have obviously done something stupid but dont know what? I am begining to think i have bitten off more than i can chew. Please help.

Private Sub Form_Error(DataErr As Integer, Response As Integer)
' If an error occurs because of duplicate data in a required field
' Display own custom error message

Const conErrDuplicateKey = 3022
Const conErrNullKey = 3314
Const conErrZeroKey = 3058

Select Case DataErr
Case conErrDuplicateKey
MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
Response = acDataErrContinue
 

Guus2005

AWF VIP
Local time
Today, 20:48
Joined
Jun 26, 2007
Messages
2,641
Thanks
I have copied and pasted your code into my database but when i try and run it, it throws up a compile error, "Variable not Defined" and highlights the word Search. See highlighted and underlined below? I have obviously done something stupid but dont know what? I am begining to think i have bitten off more than i can chew. Please help.

Private Sub Form_Error(DataErr As Integer, Response As Integer)
' If an error occurs because of duplicate data in a required field
' Display own custom error message

Const conErrDuplicateKey = 3022
Const conErrNullKey = 3314
Const conErrZeroKey = 3058

Select Case DataErr
Case conErrDuplicateKey
MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
Response = acDataErrContinue
The code you refer to has nothing to do with the code provided by me. It's nothing more than an example as to how to use and write user defined error messages.

The error you mention is because you didn't declare the "Search" variable you use in your code.

"Search" must be a global or local variable that depends on the scope you need it for.

Local:
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
' If an error occurs because of duplicate data in a required field
' Display own custom error message

Const conErrDuplicateKey = 3022
Const conErrNullKey = 3314
Const conErrZeroKey = 3058

[COLOR="DarkRed"]dim Search as string 'scope is procedure
Search = me.txtSearchField[/COLOR]

Select Case DataErr
Case conErrDuplicateKey
MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
Response = acDataErrContinue
Global:
Code:
Option Compare Database
Option Explicit

[COLOR="darkred"]dim mSearch as string 'Scope is module[/COLOR]

Private Sub Form_Error(DataErr As Integer, Response As Integer)
' If an error occurs because of duplicate data in a required field
' Display own custom error message

Const conErrDuplicateKey = 3022
Const conErrNullKey = 3314
Const conErrZeroKey = 3058

[COLOR="darkred"]'You need to set the value of mSearch
mSearch = me.txtSearchField[/COLOR]
Select Case DataErr
Case conErrDuplicateKey
MsgBox "This Serial Number already exsists: " & mSearch, , "Warning Duplicate Record!"
Response = acDataErrContinue
Enjoy!
 

Ezz

New member
Local time
Today, 19:48
Joined
Jan 14, 2010
Messages
5
I have tried your solution and it now throws up another compile error saying "Method or data member not found" and highlights the (Me.txtSearchField) statement. See below.
Since adding the extra code's the origional "error code change" that worked with the navigation buttons now doesn't? and the command "Save" button still shows accesses default error message. I don't suppose there is a way of coding the command button to look at the forms error messages is there????
Regards
Ezz

Private Sub Form_Error(DataErr As Integer, Response As Integer)
' If an error occurs because of duplicate data in a required field
' Display own custom error message

Const conErrDuplicateKey = 3022
Const conErrNullKey = 3314
Const conErrZeroKey = 3058

'You need to set the value of mSearch
mSearch = Me.txtSearchField

Select Case DataErr
Case conErrDuplicateKey
MsgBox "This Serial Number already exsists: " & mSearch, , "Warning Duplicate Record!"
Response = acDataErrContinue
 

Guus2005

AWF VIP
Local time
Today, 20:48
Joined
Jun 26, 2007
Messages
2,641
What i am offering you are directions and guidelines. I now that the odds that you have a text box named txtSearchField are pretty slim. So that's the name you have to change to meet YOUR textbox name. "Me" is the object refering to your current form. Me.txtSearchField refers to the textbox named txtSearchField obviously you don't have a textbox control with that name. So you have to change it to whatever the name is that you used.

The subject of the topic title is too difficult for you at this moment.

Try to figure out how the northwind database works and work your way up from there. There is also the Orders and Solutions sample database.

Enjoy!
 

Attachments

  • Nwind.zip
    688.1 KB · Views: 169
  • Orders.zip
    320.3 KB · Views: 153
  • Solutions9.zip
    502.5 KB · Views: 163

Ezz

New member
Local time
Today, 19:48
Joined
Jan 14, 2010
Messages
5
Guus
Thanks for the info, I did say i was a novice.
I have now ammended the Me.txtSearchField to read what it should, and low and behold no more error messages. The initial problem of displaying my own error message still exsists, however i think this may be because i should be adapting your origional example, instead of just a straight copy and paste.
ie. Should i be altering the statement "YourModuleName" to something from my database? and should i be adding a procedure underneath the statement 'Body of procedure here? like the change of error message.
Further could you re-send the Orders and Solutions examples as they don't do anything but keep telling me "Cannot update. Database or object is Read Only"?
 

Guus2005

AWF VIP
Local time
Today, 20:48
Joined
Jun 26, 2007
Messages
2,641
Are you really a novice? With a black cap and white collar?
LOL. Just picturing you sitting behind your desktop. Trying not to swear.:D
Sorry, couldn't resist.

Yes, with YouModuleName i mean your module name.
The error handler construction should be the same in every procedure so whatever your procedure look like, this is the error handler. With "this" i mean the code in my first post in your thread.

Please try again to download the database samples. I checked them and they seem ok to me. They are zip files and you have to unpack them before you open them ofcourse. If you still get a message that they are read-only, try to change the file attributes. Look in the file properties.

For more help, please post a sample database.

HTH:D
 

Users who are viewing this thread

Top Bottom