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
4 3448
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. - Private Sub cmdSave_Click()
-
On Error GoTo ErrorHandler
-
-
RunCommand acCmdSaveRecord
-
-
If CurrentProject.AllForms("frmViewProjects").IsLoaded Then
-
Forms![frmViewProjects].Refresh
-
Forms![frmViewProjects].Form_Current
-
Forms![frmViewProjects].Update
-
End If
-
-
DoCmd.Close acForm, "frmEditActivity"
-
-
ExitCode:
-
Exit Sub
-
ErrorHandler:
-
If Err.number = 3022 Then
-
MsgBox "That activity already exists.", , "Duplicate Activity"
-
Else
-
HandleError Err.number, Err.Description, Me.Name & "::cmdSave_Click"
-
End If
-
Resume ExitCode
-
End Sub
-
-
-
Private Sub Form_BeforeUpdate(Cancel As Integer)
-
On Error GoTo ErrorHandler
-
Dim valid As Boolean
-
Dim strCriteria As String
-
-
valid = True
-
-
If IsNull(txtActivityNumber) Or txtActivityNumber = "" Then
-
txtActivityNumber.BorderColor = 255
-
valid = False
-
Else
-
txtActivityNumber.BorderColor = 11050647
-
End If
-
If IsNull(txtActivityTitle) Or txtActivityTitle = "" Then
-
txtActivityTitle.BorderColor = 255
-
valid = False
-
ElseIf InStr(txtActivityTitle, """") > 0 Or InStr(txtActivityTitle, ";") Then
-
MsgBox "Please avoid "" or ; characters for database compatability.", , "Input Error"
-
txtActivityTitle.BorderColor = 255
-
valid = False
-
Else
-
txtActivityTitle.BorderColor = 11050647
-
End If
-
If IsNull(cboProjectLeader) Or cboProjectLeader = "" Then
-
cboProjectLeader.BorderColor = 255
-
valid = False
-
Else
-
cboProjectLeader.BorderColor = 11050647
-
End If
-
-
If valid = False Then
-
lblError.Visible = True
-
Cancel = True
-
End If
-
-
ExitCode:
-
Exit Sub
-
ErrorHandler:
-
HandleError Err.number, Err.Description, Me.Name & "::Form_Before_Update"
-
Resume ExitCode
-
End Sub
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
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: - Select Case Err.Number
-
Case 3022
-
MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
-
-
Case 3058
-
MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"
-
-
Case Else
-
'Display the error number and the generic Access message.
-
MsgBox "Error #: " & Err.Number & " " & Err.Description
-
-
End Select
Another option is to use the elseif statement - If Err.Number= 3022 Then
-
MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
-
-
ElseIf Err.Number = 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
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.
Thats it the ElseIf statement works a treat many thanks.
Ezzz.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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....
|
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...
|
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...
|
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>
...
|
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()....
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |