[Help] Mass duplicate main records and related subform records (1 Viewer)

cnstarz

Registered User.
Local time
Yesterday, 20:13
Joined
Mar 7, 2013
Messages
89
In my simple database (attached), I need to mass duplicate Tasks and their Notes.

I have three tables: tbTasks (PK: Task_ID), tbNotes (PK: Note_ID), jtbTaskNotes (FKs: Task_ID and Note_ID). jtbTaskNotes is my many-to-many junction table that ties Tasks to Notes.

The main form (fmTasks), bound to tbTasks, has a subform (sbfm_TaskNotes) that displays notes associated with each Task. On themain form,you select which Tasks you want duplicated via a checkbox. The append query (quCopyTasks) will duplicate all tasks that have the checkbox checked. All good there. However, I can't figure out how to also duplicate each task's Notes.

I found Allen Browne's solution (http://allenbrowne.com/ser-57.html), but that only handles duplication of one record at a time, whereas I need to duplicate many records at a time (sometimes 10+ records). How do I go about duplicating multiple Tasks and their associated Notes?

Before you ask "why are you duplicating records?": There are times when tasks need to be re-accomplished and therefore need to have a new record. It's easier to duplicate records than it is to hand-jam everything again. Thanks for your help!
 

Attachments

  • TaskNotesdb.zip
    31.2 KB · Views: 94

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:13
Joined
Aug 30, 2003
Messages
36,125
The problem is that in order to maintain referential integrity, you need each new main primary key to add the related records. I'm not sure how you can do that in bulk. I'd probably use Allen's method within a loop of the main records to be duplicated.
 

cnstarz

Registered User.
Local time
Yesterday, 20:13
Joined
Mar 7, 2013
Messages
89
Yeah, looping Allen's method and having it duplicate each record one at a time is what I was thinking, but I'm not VBA proficient and don't know how to tell it which records to duplicate nor how to loop it. :(
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:13
Joined
Aug 30, 2003
Messages
36,125
Here's my template code for looping a recordset. In your case you'd use the task table with the same criteria as the append query. Within the loop you can refer to the field like:

rs!FieldName

Code:
  Dim strSQL  As String
  Dim db      As DAO.Database
  Dim rs      As DAO.Recordset

  Set db = CurrentDb()
  
  strSQL = "SELECT ..."
  Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

  Do While Not rs.EOF

    rs.MoveNext
  Loop

  set rs = nothing
  set db = nothing
 

cnstarz

Registered User.
Local time
Yesterday, 20:13
Joined
Mar 7, 2013
Messages
89
Sweet deal! After playing with it yesterday, I have it working the way I need it to! Thanks for pointing me in the right direction. I uploaded a zip that has an .mdb and .accdb version for anyone else that needs to do this too. The mdb version cant be compacted for some reason, that's why it's freaking 7mb in size. O_O Hopefully this helps somebody.
 

Attachments

  • tasknotesdb2.zip
    786.8 KB · Views: 100

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:13
Joined
Aug 30, 2003
Messages
36,125
Happy to help! The size may be due to images or attachments. I'm on an iPad right now so can't look at the file.
 

Users who are viewing this thread

Top Bottom