473,625 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Handle Run-Time Error 3022

124 New Member
On the OnClick event of a command button I have code that copies a parent record and all of its child records in the same table using recordsets:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCopy_Click()
  2. On Error GoTo Error_Handler
  3.  
  4.     Dim lngMatricIdFrom As Long
  5.     Dim lngMatricIdTo As Long
  6.     Dim lngDiscId As Long
  7.     Dim lngDiscClassId As Long
  8.     Dim lngNewDiscId As Long
  9.     Dim lngNewDiscClassId As Long
  10.     Dim strStatus As String
  11.     Dim rstDiscTo As DAO.Recordset
  12.     Dim rstDiscFrom As DAO.Recordset
  13.     Dim rstDiscClTo As DAO.Recordset
  14.     Dim rstDiscClFrom As DAO.Recordset
  15.     Dim rstDiscCrTo As DAO.Recordset
  16.     Dim rstDiscCrFrom As DAO.Recordset
  17.     Dim rstDiscCrExTo As DAO.Recordset
  18.     Dim rstDiscCrExFrom As DAO.Recordset
  19.  
  20.     DoCmd.Hourglass True
  21.  
  22.     strStatus = SysCmd(acSysCmdSetStatus, "Copying Disciplines...")
  23.  
  24.     lngMatricIdFrom = Me.cboMatricIdFrom.Column(0)
  25.     lngMatricIdTo = Me.cboMatricIdTo.Column(0)
  26.  
  27.     Set rstDiscTo = CurrentDb.OpenRecordset("Disc", dbOpenDynaset)
  28.     Set rstDiscFrom = CurrentDb.OpenRecordset("SELECT * FROM Disc WHERE MatricId = " & lngMatricIdFrom)
  29.  
  30.     If rstDiscFrom.BOF And rstDiscFrom.EOF Then
  31.         GoTo Exit_Procedure
  32.     Else: rstDiscFrom.MoveFirst
  33.     End If
  34.  
  35.     'Copy discipline with selected matricid
  36.     Do While Not rstDiscFrom.EOF
  37.         With rstDiscTo
  38.             .AddNew
  39.             !MatricId = lngMatricIdTo
  40.             !CodeDiscTypeId = rstDiscFrom!CodeDiscTypeId
  41.             !CodeDiscId = rstDiscFrom!CodeDiscId
  42.             !CreditMin = rstDiscFrom!CreditMin
  43.             !CourseMin = rstDiscFrom!CourseMin
  44.             !ClassMin = rstDiscFrom!ClassMin
  45.             !SortOrder = rstDiscFrom!SortOrder
  46.             .Update 'CODE STOPS
  47.             .Bookmark = rstDiscTo.LastModified
  48.             lngNewDiscId = rstDiscTo!DiscId
  49.         End With
  50.  
  51.         lngDiscId = rstDiscFrom!DiscId
  52.  
  53.         Set rstDiscClTo = CurrentDb.OpenRecordset("DiscClass", dbOpenDynaset)
  54.         Set rstDiscClFrom = CurrentDb.OpenRecordset("SELECT * FROM DiscClass WHERE DiscId = " & lngDiscId, dbOpenSnapshot)
  55.  
  56.         If rstDiscClFrom.BOF And rstDiscClFrom.EOF Then
  57.             Exit Do
  58.         Else: rstDiscClFrom.MoveFirst
  59.         End If
  60.  
  61.         'Loop through and copy discipline's classifications with new DiscId
  62.         Do While Not rstDiscClFrom.EOF
  63.             With rstDiscClTo
  64.                 .AddNew
  65.                 !DiscId = lngNewDiscId
  66.                 !CodeClassId = rstDiscClFrom!CodeClassId
  67.                 !CreditMin = rstDiscClFrom!CreditMin
  68.                 !CourseMin = rstDiscClFrom!CourseMin
  69.                 !SortOrder = rstDiscClFrom!SortOrder
  70.                 !ClassNote = rstDiscClFrom!ClassNote
  71.                 .Update
  72.                 .Bookmark = rstDiscClTo.LastModified
  73.                 lngNewDiscClassId = rstDiscClTo!DiscClassId
  74.             End With
  75.  
  76.             lngDiscClassId = rstDiscClFrom!DiscClassId
  77.  
  78.             Set rstDiscCrTo = CurrentDb.OpenRecordset("DiscCourse", dbOpenDynaset)
  79.             Set rstDiscCrFrom = CurrentDb.OpenRecordset("SELECT Cr.* FROM DiscCourse Cr INNER JOIN DiscClass Cl ON " _
  80.                 & "Cr.DiscClassId = Cl.DiscClassId WHERE Cl.DiscClassId = " & lngDiscClassId, dbOpenSnapshot)
  81.  
  82.             If rstDiscCrFrom.BOF And rstDiscCrFrom.EOF Then
  83.                 Exit Do
  84.             Else: rstDiscCrFrom.MoveFirst
  85.             End If
  86.  
  87.             'Loop through and copy discipline's classification's courses with new DiscClassId
  88.             Do While Not rstDiscCrFrom.EOF
  89.                 With rstDiscCrTo
  90.                     .AddNew
  91.                     !DiscClassId = lngNewDiscClassId
  92.                     !Course = rstDiscCrFrom!Course
  93.                     !AnyPassingGrade = rstDiscCrFrom!AnyPassingGrade
  94.                     !MinGradeId = rstDiscCrFrom!MinGradeId
  95.                     !IncludeMajorGPA = rstDiscCrFrom!IncludeMajorGPA
  96.                     !SortOrder = rstDiscCrFrom!SortOrder
  97.                     .Update
  98.                 End With
  99.                 rstDiscCrFrom.MoveNext
  100.             Loop
  101.  
  102.             rstDiscClFrom.MoveNext
  103.         Loop
  104. MoveNextDisc:
  105.         rstDiscFrom.MoveNext
  106.     Loop
  107.  
  108.     Forms!f_Main!sfrmDisc.Requery
  109.  
  110. Exit_Procedure:
  111.     On Error Resume Next
  112.     rstDiscTo.Close
  113.     Set rstDiscTo = Nothing
  114.     rstDiscFrom.Close
  115.     Set rstDiscFrom = Nothing
  116.     rstDiscClTo.Close
  117.     Set rstDiscClTo = Nothing
  118.     rstDiscClFrom.Close
  119.     Set rstDiscClFrom = Nothing
  120.     rstDiscCrTo.Close
  121.     Set rstDiscCrTo = Nothing
  122.     rstDiscCrFrom.Close
  123.     Set rstDiscCrFrom = Nothing
  124.     rstDiscCrExTo.Close
  125.     Set rstDiscCrExTo = Nothing
  126.     rstDiscCrExFrom.Close
  127.     Set rstDiscCrExFrom = Nothing
  128.     strStatus = SysCmd(acSysCmdClearStatus)
  129.     DoCmd.Hourglass False
  130.     DoCmd.SetWarnings True
  131.     Exit Sub
  132.  
  133. Error_Handler:
  134.     If Err.Number = 3022 Then
  135.         GoTo MoveNextDisc
  136.     Else
  137.         MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
  138.         Resume Exit_Procedure
  139.         Resume
  140.     End If
  141. End Sub
  142.  
This code works fine except when there's a duplicate record. I try to handle this in Error_Handler. If the user is trying to copy a record that already exists with the same MatricId, CodeDiscTypeId, and CodeDiscId, then I basically want to skip over that record and move on to the next record in the rstDiscFrom recordset. This appears to work for the first record in the recordset. However, come the second duplicate record, the code stops at the .Update line above where it tries to add a new record to rstDiscTo (and where I put a CODE STOPS comment for you) and it gives me a Run-Time Error 3022. I want it to jump to my Error_Handler so it knows to just skip that record and move on to the next. How do I do this?
Jun 8 '10 #1
0 2587

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

Similar topics

3
3288
by: B Moor | last post by:
SServer PC: Win SBS 2003 with 2.6 GHz processor and 1GB RAM SQL Server 2000 v 2000.8.00.76 (sp3) MS Office 2k3 MSJet ms04-014 (latest ost sp8) MDAC v2.8 RTM ADO 2.1 vb6.exe / ADO 2.0 I think this is a SQL Server/ADO problem as I have 2 applications with
15
4367
by: Tim Clacy | last post by:
Please illuminate; what operator of class 'Event' will get matched for these two cases : Event ev1; Event ev2; // Case 1 // if (ev1) ;
5
6459
by: Apollyon | last post by:
I'm trying to figure out if there's a function that takes a file pointer (FILE *) and will return a HANDLE type ... I'm trying to modify some code and I've looked everywhere to no avail. Thanks -- Apollyon
15
4034
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE CALLING FORM! This is very bad for me, because in my application, Form1 responds to an ENTER keypress by calling Form2. If the user closes Form2 via an ENTER, then Form1 just reopens it again, kind of trapping the user in Form2 (they can still close...
9
2047
by: lli | last post by:
Hi Guys, I am a new cgi programmer. Now I need to design a web application: 1. first, user login by their username and password in a form (login form). When users click submit button it connect to database and check user name and password in a table. If it match in a table, form1.py should show second form for users in their browser. I have finish this form. It works well. 2. second form (search form) which contain last name, and first...
6
2005
by: Joel | last post by:
2 Questions: (1) The documentation says application.run() creates a standard message loop on the current thread and "optionally" shows a form. This is really confusing because I was of the understanding that application.run() creates a message loop for the form and passes all messages to it. If showing the form is optional, and I want to to display 2 forms, which form will application.run() pass the windows messages to?
2
3052
by: Gary Wessle | last post by:
Hi I need help organizing this program in the right way. I included the code below which compiles and runs and gives the desired effect to a certain point, but I don't know what the next step is to finish this program. the program presents Main menu to the user and gives a prompt, the user types the number corresponding to an item from the menu, the program then
5
2803
by: Lin | last post by:
Hello, I am new to c#, please help. I am writing an application, which will be able to send email automatically, for a very large organisation. The application I am writing should be on the central server, anyone within the organisation should be able to download the application and run on his/her local machine without any configuration. The organisation may have quite a few email servers, so we cannot user server side email API to...
28
7361
by: | last post by:
I have a multi threaded windows form application that runs great after calling Application.Run(). Application.Run is required for a COM component I a using in the app (required for message loop). I have created a windows service from VStudio 2005 template. What is the windows service replacement for Application.Run()?
0
970
by: tochisan | last post by:
Hello all, I'm writing a c# console program that's going to be run as a service but I want it to have the ability to handle arguments and run in a certain way. The program is supposed to monitor several different types of servers and update a SQL database with vital information. Several of the servers are different and require special considerations within the program. The easiest way for me to accomplish this is with arguments against the...
0
8253
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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
8354
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
8497
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...
1
6116
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
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.