By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,488 Members | 2,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,488 IT Pros & Developers. It's quick & easy.

Customising multiple error messages for a command button

P: 28
I have customised a form to display my own error messages using:-

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

In order to get my "Save" command button (not changed from Command26) to do the same i used:-
Private Sub Command26_Click()
On Error GoTo Err_Command26_Click

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Me.[Serial Number].SetFocus
Me.Command26.Visible = False

Exit_Command26_Click:
Exit Sub

Err_Command26_Click:

If Err = 3022 Then
MsgBox "This Serial Number already exsists: " & mSearch, , "Warning Duplicate Record!"

Else
' Display the error number and the generic Access message.
MsgBox "Error #: " & Err.Number & " " & Err.Description

End If
Resume Exit_Command26_Click

End Sub

But this only works for the one error code. Is there a way to get my "Save" command button to work with multiple error codes like my form?
PS. my programming skills are very basic at best, the above has been copied and adapted from info found on the net.
Regards
Ezzz
Jan 26 '10 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,287
When you save the record, the Form's Before_Update event is automatically called, and you can use it to check for other errors, give a message, and cancel if necessary. If you set Cancel to True, you should get an error from the save record event. Here is an example straight from one of my projects.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSave_Click()
  2. On Error GoTo ErrorHandler
  3.  
  4.     RunCommand acCmdSaveRecord
  5.  
  6.     If CurrentProject.AllForms("frmViewProjects").IsLoaded Then
  7.         Forms![frmViewProjects].Refresh
  8.         Forms![frmViewProjects].Form_Current
  9.         Forms![frmViewProjects].Update
  10.     End If
  11.  
  12.     DoCmd.Close acForm, "frmEditActivity"
  13.  
  14. ExitCode:
  15.     Exit Sub
  16. ErrorHandler:
  17.     If Err.number = 3022 Then
  18.         MsgBox "That activity already exists.", , "Duplicate Activity"
  19.     Else
  20.         HandleError Err.number, Err.Description, Me.Name & "::cmdSave_Click"
  21.     End If
  22.     Resume ExitCode
  23. End Sub
  24.  
  25.  
  26. Private Sub Form_BeforeUpdate(Cancel As Integer)
  27. On Error GoTo ErrorHandler
  28.     Dim valid As Boolean
  29.     Dim strCriteria As String
  30.  
  31.     valid = True
  32.  
  33.     If IsNull(txtActivityNumber) Or txtActivityNumber = "" Then
  34.         txtActivityNumber.BorderColor = 255
  35.         valid = False
  36.     Else
  37.         txtActivityNumber.BorderColor = 11050647
  38.     End If
  39.     If IsNull(txtActivityTitle) Or txtActivityTitle = "" Then
  40.         txtActivityTitle.BorderColor = 255
  41.         valid = False
  42.     ElseIf InStr(txtActivityTitle, """") > 0 Or InStr(txtActivityTitle, ";") Then
  43.         MsgBox "Please avoid "" or ; characters for database compatability.", , "Input Error"
  44.         txtActivityTitle.BorderColor = 255
  45.         valid = False
  46.     Else
  47.         txtActivityTitle.BorderColor = 11050647
  48.     End If
  49.     If IsNull(cboProjectLeader) Or cboProjectLeader = "" Then
  50.         cboProjectLeader.BorderColor = 255
  51.         valid = False
  52.     Else
  53.         cboProjectLeader.BorderColor = 11050647
  54.     End If
  55.  
  56.     If valid = False Then
  57.         lblError.Visible = True
  58.         Cancel = True
  59.     End If
  60.  
  61. ExitCode:
  62.     Exit Sub
  63. ErrorHandler:
  64.     HandleError Err.number, Err.Description, Me.Name & "::Form_Before_Update"
  65.     Resume ExitCode
  66. End Sub
Jan 26 '10 #2

P: 28
ChipR
Thanks for the the above, but addapting it to my form is a bit beyond me?
It also looks like you are coding different entries to your form and i want to code the one entry box [Serial Number] to display multiple error messages.

I did however adapt part of it by adding an End If and then a new If Err statement see highlighted below:-
Private Sub Command26_Click()
On Error GoTo Err_Command26_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Me.[Serial Number].SetFocus
Me.Command26.Visible = False

Exit_Command26_Click:
Exit Sub

Err_Command26_Click:

If Err = 3022 Then
MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"

End If

If Err = 3058 Then
MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"


Else
' Display the error number and the generic Access message.
MsgBox "Error #: " & Err.Number & " " & Err.Description

End If
Resume Exit_Command26_Click

End Sub

This almost solves my problem in that it now displayes the error messages i want it to but it also displayes the default error message for 3022 as well. Any ideas??
Regards
Ezzz
Jan 27 '10 #3

TheSmileyCoder
Expert Mod 100+
P: 2,321
Please remember to use the code tags [code][/code] around your code. It will make it much easier to read.


What you need is to use either a Select statement:
Expand|Select|Wrap|Line Numbers
  1. Select Case Err.Number
  2.   Case 3022
  3.     MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
  4.  
  5.   Case 3058 
  6.     MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"
  7.  
  8.   Case Else
  9.      'Display the error number and the generic Access message.
  10.      MsgBox "Error #: " & Err.Number & " " & Err.Description
  11.  
  12. End Select
Another option is to use the elseif statement
Expand|Select|Wrap|Line Numbers
  1. If Err.Number= 3022 Then
  2.     MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
  3.  
  4. ElseIf Err.Number = 3058 Then
  5.     MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"
  6.  
  7. Else
  8.      'Display the error number and the generic Access message.
  9.      MsgBox "Error #: " & Err.Number & " " & Err.Description
  10. End If

A more general suggestion to help yourself, is to give your buttons a sound name.
Some people like to use for command buttons the prefix cmd (example cmd_Save or cmdSave), I myself prefix all my buttons with btn, btn_Save for example.
Jan 27 '10 #4

P: 28
Thats it the ElseIf statement works a treat many thanks.
Ezzz.
Jan 27 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.