Solved

Duplicate Main/Sub Forms through MultiSelect List Box

Posted on 2012-03-28
9
300 Views
Last Modified: 2012-04-06
I need to duplicate a Mainform and Subform records, but instead of duplicating the current record  I need to duplicate the current record for a MultiSelect List box looping through each ID selected in the list box and duplicating into the main recordset and the subform recordset.  I was hoping someone could provide a sample of code I could work off of. Thanks.
0
Comment
Question by:skennelly
  • 5
  • 4
9 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37777934
Why?

In other words, this sounds a bit odd, and would seem to create redundant data.

So perhaps you could *first* take a step back and explain your ultimate goal here...?

JeffCoachman
0
 

Author Comment

by:skennelly
ID: 37778322
No problem. I am designing a fitness workout program. The program has a main form with (Name, Date, WorkoutType, etc.) linked to a subform which contains the workout data (Exercise, Sets, Repetitions, etc.) for the workout. Currently I have a duplicate button on the main form that duplicates the current record (main and subform) for the Client selected in the CmboBox. If I want to duplicate the record for another client, I need to select that client in the CmboBox.

So, I would like to add an unbound Multi-Select list box containing a list of all of the clients. I would then select (x) amount of clients from the list box. When I select the Duplicate button - the main and subform records would be duplicated for all of the clients selected in the list box.

To do this, I believe I would need to loop through the Client list box to build the criteria statement. Then I would need to select the underlying recordsets from the main and subforms, then connect it to the build criteria to add the workout to each of the clients' programs. The whole process would be done programmatically.

I hope this explanation is more helpful. Thanks.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37778647
Great,
Thanks for the explanation, makes more sense now...


The kicker here is that you will have to be good a VBA to pull this off.

Here is the popular link to copy main and subform records:
http://support.microsoft.com/kb/208824

Then join that with something like this (sample attached) to do this for multiple customers
In your case you listbox would be filled with customers instead on reports.
And your code on the Button click (near the bottom) would have a snippet *roughly* like this:

...
Dim lngSelected As Long
   
        For lngSelected = 0 To Me.lstReports.ListCount - 1
            'Run the code from the MS Site
        Next lngSelected
...


Goo Luck...

;-)

JeffCoachman
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37778685
0
 

Author Comment

by:skennelly
ID: 37779914
Thanks for your response. I have been working on the code for quite some time before asking the question. I will check out the samples and get back to you . Thanks again.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37783206
Yeah, basically if you can get the "Copy" code working for one Main Record ID, then the list box code simply loops the Main ID's that selected in the listbox.
(And does the "Copy" for each one...

;-)

Jeff
0
 

Accepted Solution

by:
skennelly earned 0 total points
ID: 37793359
After much trial and error, I was able to solve it with the following code. I have only pasted the part of the Procedure relevant to the question. Thanks.

    With Me.RecordsetClone

    For Each var In Me!cmboAthleteID.ItemsSelected
    Criteria = Me![cmboAthleteID].ItemData(var)


        'Duplicate the main record: add to form's clone.
        DoCmd.GoToRecord , , acNewRec
        Forms!frmWorkoutDetail!AthleteID = Criteria
        Forms!frmWorkoutDetail!WorkoutType = Forms!frmTemplateDetail!WorkoutType
        Forms!frmWorkoutDetail!WorkoutDate = Forms!frmTemplateDetail!WDate
        
        DoCmd.RunCommand acCmdSaveRecord
            lngID = Forms!frmWorkoutDetail!WorkoutID
            'Duplicate the related records for the Strength Detail Subform: append query.
            If Forms!frmTemplateDetail![frmTemplateDetailSub].Form.RecordsetClone.RecordCount > 0 Then
                strSQL = "INSERT INTO [qryStrengthDetail] (WorkoutID, ExerciseCategoryID, ExerciseID, ExerciseOrder, Reps, Weight, Cluster, ExerciseTime, Side, StrengthNotes, WorkoutCode) " & _
                    "SELECT " & lngID & " As NewID, ExerciseCategoryID, ExerciseID, ExerciseOrder, Reps, Weight, Cluster, Time, Side, Notes, Code " & _
                    "FROM [qryTemplateDetail] WHERE TemplateID = " & Forms!frmTemplateDetail.TemplateID & ";"
                DBEngine(0)(0).Execute strSQL, dbFailOnError
            Else
                MsgBox "There is no Strength Workout to duplicate."
            End If
                Forms!frmWorkoutDetail.frmStrengthDetailSub.Form.Requery
    
Next var

Open in new window

0
 

Author Comment

by:skennelly
ID: 37793385
The bulk of the question was solved by myself, but the answers provided led me to research coding of multi-select list boxes so I would like to give partial credit. Thanks.
0
 

Author Closing Comment

by:skennelly
ID: 37815434
I was able to solve it on my own.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question