473,499 Members | 1,610 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 2570

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

Similar topics

3
3281
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...
15
4353
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
6454
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...
15
4007
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...
9
2040
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...
6
1988
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...
2
3040
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...
5
2786
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...
28
7336
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). ...
0
954
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...
0
7009
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...
0
7178
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,...
0
7223
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...
1
6899
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
7390
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...
0
5475
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
4919
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
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...

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.