Question : Access 2003 Insert Query with data from form to multiple records

Folks I have a form that has a few fields that I am entering. The information that I am entering also needs to be entered into 15, 19, 25 or 30 more records (Random amounts). Not different data but the exact data.

Below my form I have a query that helps in my entry and I have added a toggle button. I would like to be able to select the "Other" records that require the same information and after entering hit a command button that takes my form information and inserts the same text into the same corresponding fields.

Please see the attached database as reference. The Copy Multiple button is the button I want to use to insert my data into multiple records.

Answer : Access 2003 Insert Query with data from form to multiple records

Ok. Here's a working solution..
The code loops through the subform records. If the record has 'Copy' selected, it sets an object reference to an update query that I've added to your database and populates the parameters from the details on the screen.

The only other modification i made to your database was to create a unique index on TagNumber in SKPIinvestigation. They did _happen_ to be unique values, but best to enforce it.

Let us know how you get on with it.

Simon
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
Sub CopyDetailsToSKPIs_SelectByTagNumber()
Dim rs As DAO.Recordset
Dim qd As QueryDef
Dim recCount As Long
Dim copyCount As Long
Dim msgString As String

Set rs = Me.qSkpiInvestigationTMMIsubform.Form.RecordsetClone
If Not ((rs.BOF) And (rs.EOF)) Then
    rs.MoveLast
    recCount = rs.RecordCount
    Debug.Print recCount & " records in subform"
    rs.MoveFirst
    
    For ctr = 1 To rs.RecordCount
        If rs.Fields("Copy") = True And rs("tagNumber") <> Me.TagNumber Then
            copyCount = copyCount + 1
            Debug.Print "*** Copying details to SKPI with Tagnumber " & rs.Fields("tagNumber")
            Set qd = CurrentDb.QueryDefs("qryUpdateSKPI_TagNumber")
            qd.Parameters("strTagNumber") = rs.Fields("tagNumber")
            'Fill in the form-based params
            qd.Parameters(1) = Me.DateInvestigationIssued
            qd.Parameters(2) = Me.RCOccurence
            qd.Parameters(3) = Me.RCDetection
            qd.Parameters(4) = Me.RCCategory
            qd.Parameters(5) = Me.Responsible
            qd.Parameters(6) = Me.dispute
            qd.Parameters(7) = Me.CMOccurence
            qd.Parameters(8) = Me.CMDetection
            qd.Parameters(9) = Me.CMCategory
            qd.Parameters(10) = Me.DateCMImplemented
            qd.Parameters(11) = Me.Status
            qd.Parameters(12) = Me.WeeklyUpdate
            qd.Parameters(13) = Me.WeeklyUpdateDate
'            For x = 0 To qd.Parameters.Count - 1
'                Debug.Print x; qd.Parameters(x).Name & vbTab & vbTab & qd.Parameters(x).Value
'            Next
            qd.Execute
            Set qd = Nothing
        ElseIf rs("tagNumber") = Me.TagNumber Then
            Debug.Print "Skipping TagNumber" & rs("tagNumber") & " because it is the record being edited in the main form."
        End If
        rs.MoveNext
    Next
    msgString = "Details were copied to " & copyCount & " records."
Else
    msgString = "No records in subform!"
End If
    MsgBox msgString, vbOKOnly + vbInformation, "Copy to multiple SKPIs"
Set qd = Nothing
Set rs = Nothing
End Sub
 
modified database with working code behind "copy to multiple" button and one extra update query.
 
Random Solutions  
 
programming4us programming4us