472,802 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Error Handling while Inserting data

1
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 9310
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 Expert 2GB
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 Expert 8TB
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,534 Expert Mod 16PB
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.