473,472 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error Handling while Inserting data

1 New Member
Hi,
I am trying to do error handling during insert in MS Access 2002 (OS: MS XP)

The problem is that when a duplicate record is added for the primary key field or a null value included in a unique filed, an error appears:

"Microsoft Access cannot append all the records in the append query!"

What I want is to suppress this message and display my own message, but both this message and mine one comes while I do the error handling.

Please suggest. I have included the code. Using "Docmd.Setwarnings false" suppresses the display of both the above message as well as my message -- it actually doesn't do the error handling.

Also, where can I find the list of errors in Access and their error numbers?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdsave_Click()
  2.  
  3. On Error GoTo errmsg    
  4.     Form.Requery
  5.  
  6.     DoCmd.RunSQL "INSERT INTO members ( IDNo, Name, MemberType, Designation, Address, " _
  7.     & "Citizenship, CitizenNo, FirmName ) " _
  8.     & "SELECT tmpmembers.IDNo, tmpmembers.Name, tmpmembers.MemberType, tmpmembers.Designation, " _
  9.     & "tmpmembers.Address, tmpmembers.Citizenship, tmpmembers.CitizenNo, tmpmembers.FirmName " _
  10.     & "FROM tmpmembers"
  11.  
  12. exit_cmdsave:
  13.     Exit Sub
  14.  
  15. errmsg:
  16.  
  17.     If Err = 3022 Then     
  18.         
  19.                  <-- this is not working
  20.  
  21.         MsgBox "The IDNO already exists. Please type another one!",  vbOKOnly, "Duplicate ID Error"
  22.  
  23.     ElseIf Err = 3058 Then
  24.  
  25.         MsgBox "IDNO cannot contain Blank!", vbOKOnly, "Blank ID!"
  26.  
  27.     Else
  28.         MsgBox Err.Number & ":" & " " & Err.Description, , "Error!"
  29.  
  30.     End If
  31.     Resume exit_cmdsave
  32.  
  33. End Sub
  34.  
Feb 17 '07 #1
4 9423
abouddan
42 New Member
Hi,
I am trying to do error handling during insert in MS Access 2002 (OS: MS XP)

The problem is that when a duplicate record is added for the primary key field or a null value included in a unique filed, an error appears:

"Microsoft Access cannot append all the records in the append query!"

What I want is to suppress this message and display my own message, but both this message and mine one comes while I do the error handling.

Please suggest. I have included the code. Using "Docmd.Setwarnings false" suppresses the display of both the above message as well as my message -- it actually doesn't do the error handling.

Also, where can I find the list of errors in Access and their error numbers?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdsave_Click()
  2.  
  3. On Error GoTo errmsg    
  4.     Form.Requery
  5.  
  6.     DoCmd.RunSQL "INSERT INTO members ( IDNo, Name, MemberType, Designation, Address, " _
  7.     & "Citizenship, CitizenNo, FirmName ) " _
  8.     & "SELECT tmpmembers.IDNo, tmpmembers.Name, tmpmembers.MemberType, tmpmembers.Designation, " _
  9.     & "tmpmembers.Address, tmpmembers.Citizenship, tmpmembers.CitizenNo, tmpmembers.FirmName " _
  10.     & "FROM tmpmembers"
  11.  
  12. exit_cmdsave:
  13.     Exit Sub
  14.  
  15. errmsg:
  16.  
  17.     If Err = 3022 Then     
  18.  
  19.         MsgBox "The IDNO already exists. Please type another one!",  vbOKOnly, "Duplicate ID Error"
  20.  
  21.     ElseIf Err = 3058 Then
  22.  
  23.         MsgBox "IDNO cannot contain Blank!", vbOKOnly, "Blank ID!"
  24.  
  25.     Else
  26.         MsgBox Err.Number & ":" & " " & Err.Description, , "Error!"
  27.  
  28.     End If
  29.     Resume exit_cmdsave
  30.  
  31. End Sub
  32.  




I think the error number for the duplicate IDNO is not 3022 it's 2501.
I tested it on my machine and it worked very well.
Note that when you are promted that an error occured and you have to choose between yes no and Help , you have to choose No.
Feb 17 '07 #2
nico5038
3,080 Recognized Expert Specialist
Error messages may differ depending on the installed Access/JetEngine version.
Best to do a check for an existing ID before issuing the INSERT.
You could use a DLOOKUP like:

IF IsNull(DLOOKUP("IDNo","members","IDNo=" & Me.IDNo)) then

This assumes that the IDNo is a field on your form.

Nic;o)
Feb 17 '07 #3
ADezii
8,834 Recognized Expert Expert
Hi,
I am trying to do error handling during insert in MS Access 2002 (OS: MS XP)

The problem is that when a duplicate record is added for the primary key field or a null value included in a unique filed, an error appears:

"Microsoft Access cannot append all the records in the append query!"

What I want is to suppress this message and display my own message, but both this message and mine one comes while I do the error handling.

Please suggest. I have included the code. Using "Docmd.Setwarnings false" suppresses the display of both the above message as well as my message -- it actually doesn't do the error handling.

Also, where can I find the list of errors in Access and their error numbers?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdsave_Click()
  2.  
  3. On Error GoTo errmsg    
  4.     Form.Requery
  5.  
  6.     DoCmd.RunSQL "INSERT INTO members ( IDNo, Name, MemberType, Designation, Address, " _
  7.     & "Citizenship, CitizenNo, FirmName ) " _
  8.     & "SELECT tmpmembers.IDNo, tmpmembers.Name, tmpmembers.MemberType, tmpmembers.Designation, " _
  9.     & "tmpmembers.Address, tmpmembers.Citizenship, tmpmembers.CitizenNo, tmpmembers.FirmName " _
  10.     & "FROM tmpmembers"
  11.  
  12. exit_cmdsave:
  13.     Exit Sub
  14.  
  15. errmsg:
  16.  
  17.     If Err = 3022 Then     
  18.  
  19.         MsgBox "The IDNO already exists. Please type another one!",  vbOKOnly, "Duplicate ID Error"
  20.  
  21.     ElseIf Err = 3058 Then
  22.  
  23.         MsgBox "IDNO cannot contain Blank!", vbOKOnly, "Blank ID!"
  24.  
  25.     Else
  26.         MsgBox Err.Number & ":" & " " & Err.Description, , "Error!"
  27.  
  28.     End If
  29.     Resume exit_cmdsave
  30.  
  31. End Sub
  32.  
There have been several Posts relating to this very Topic. To suppress the standard Access Error Message and display your own, custom Error Message, you need to write code in the Form's Error() Event, trap the specific Error (which Err.Number will return), and set the acDataErrContinue CONSTANT:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Error(DataErr As Integer, Response As Integer)
  2. If DataErr = XXXX Then      'Trap specific Error here
  3.   MsgBox "Your specific Error Message", vbExclamation, "Blah, Blah, Blah"
  4.   Response = acDataErrContionue  'do not display the Default Error Message
  5. End If
  6. End Sub
Feb 17 '07 #4
NeoPa
32,556 Recognized Expert Moderator MVP
That's good stuff ADezii, but I think a better solution is to check before adding the record (As per Nico's post #2).
Error handling is good, but relying on it in the logic of the code is not generally to be recommended IMHO. If for no other reason than the error handling settings on an individual PC may differ from those expected. That and simplicity of code.
Feb 18 '07 #5

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

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
1
by: Sean Abrahams | last post by:
The following is a reprint of a message I sent to the tutor list a long time ago, that I haven't gotten around to discussing with anyone else and failed to hear a reply on the tutor list. Hoping...
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
1
by: michaeltorus | last post by:
Hi I'm currently designing a new web application in .Net. I've pretty covered everything, apart from error handling. There seems to be a few different way to do this, but something I've read...
4
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
by: mbenedict | last post by:
I am rather new at this code and am attempting to modify existing code to use clob datatypes, which I have never used before. The database tables have been set up for clob data. When trying to use...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.