473,789 Members | 3,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Customising multiple error messages for a command button

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

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

Const conErrDuplicate Key = 3022
Const conErrZeroKey = 3058

Select Case DataErr
Case conErrDuplicate Key
MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
Response = acDataErrContin ue

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

Case Else

'Display a standard error message
Response = acDataErrDispla y
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_C lick

DoCmd.DoMenuIte m acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Me.[Serial Number].SetFocus
Me.Command26.Vi sible = False

Exit_Command26_ Click:
Exit Sub

Err_Command26_C lick:

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 3468
ChipR
1,287 Recognized Expert Top Contributor
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 New Member
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_C lick


DoCmd.DoMenuIte m acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Me.[Serial Number].SetFocus
Me.Command26.Vi sible = False

Exit_Command26_ Click:
Exit Sub

Err_Command26_C lick:

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 Recognized Expert Moderator Top Contributor
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 New Member
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
1490
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 check_error(typ, data): if (typ != 'OK'): print typ print data
1
282
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
2319
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. The error messages appear when runnign the apend query manually or when closing the form using X, but not when using command buttons. Does anyone know how can the messages be displayed when using command
6
539
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 surfin it in multiple browsers simultaneously the site generates a generic runtime error after awhile. I'm thinking this has something to do with my access database and multiple connections. I'm using forms authentication with a login page. Is...
6
5000
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 for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
8
9581
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> Uninstalled old version, installed new version, and am now trying to use the CLP. <<<<<<<<<<<< For more detailed help, refer to the Online Reference Manual.
1
1697
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(). So far, so good. But I use these core classes from a bunch of different places. Sometimes from within a CherryPy server, sometimes batch jobs run from a command line, sometimes from Windows services (also written in
1
2043
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 tried to edit the code, as below (the original code is on his website) but get a load of error messages. There are only a few bits that need editing (about 6 or 7 little bits) but I still get loads of error messages and if anyone can help, I would...
2
19494
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
3
2977
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 This is a text-only newsgroup (you could have captured the error message with Ctrl+C), and if you can't be bothered to check back for messages, why would we make the effort to answer in the first place? RunCode is designed to call a public...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.