473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Customising multiple error messages for a command button

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
4 3448
ChipR
1,287 Expert 1GB
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
Ezzz
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
2,322 Expert Mod 2GB
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
Ezzz
28
Thats it the ElseIf statement works a treat many thanks.
Ezzz.
Jan 27 '10 #5

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

Similar topics

0
by: Mr. Magoo | last post by:
I'm working with imaplib. I'm trying to flag (or move or copy - anything that takes a message_set as an argument) a bunch of messages with one command and am having trouble. === def...
1
by: dav | last post by:
Hi All, I'm a bit new to VBA and wanted to know is there any way you can format the command button? i.e make it a slightly different shape (rounded edges) and things like that. cheers Dav
1
by: bjbounce2002 | last post by:
Hello, I am using forms with command buttons to close form or run action queries. The error messages such as "Null value in required field" or "duplicate value in primary key" are suppressed....
6
by: mark | last post by:
I have an asp.net ecommerce web application on a remote web server. I'm using an Access database on the back end. I've notice a few strange things. When I mimic an multiple user environment by...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
8
by: Brian Tkatch | last post by:
Server: DB2/SUN 8.1.6 Client: DB2 Connect Personal Edition (No 11) <URL:ftp://ftp.software.ibm.com/ps/products/db2/fixes2/english-us/db2winIA32v8/fixpak/FP11_WR21365/FP11_WR21365_CONPE.exe> ...
1
by: Chris Curvey | last post by:
Hi all, I've apparently tied myself up a bit using the logging package. In my project, I have a core set of model and controller classes that set up their logging using logging.fileConfig()....
1
by: xian2 | last post by:
Hi All, I have been using this guide http://www.compasscomputing.co.uk/code/AcceesReportsToPDFandEmail.htm to create a command button that automatically saves and e-mails your report and have...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
3
by: Tom van Stiphout | last post by:
On Thu, 30 Oct 2008 11:04:44 GMT, "JOHNNY OUTING" <outingjr@att.net> wrote: You managed to violate several netiquette rules in one post. Please review http://www.mvps.org/access/netiquette.htm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.