When inserting data from a query to a table, does the query run for each record inserted?

前端 未结 3 1389
一整个雨季
一整个雨季 2021-01-13 20:16

I\'m inserting data problematically into tables. When I do this from another table, it\'s swift, only slowed very slightly if there are a lot of records. Even then, it\'s a

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-13 20:55

    I have written a pretty basic module that should accomplish this for you very quickly compared to your current process. Note you will need to re-name your project to something other than "Database" on the project navigation pane for this to work

    I have assumed that table1 and table2 are the same as you have above table3 is simply a list of all records in table 1 with a blank "FieldValues" field to add the required "value1, value2" etc. This should result in Table3 being populated with your desired result

    IMPORANT: For anyone using recordset .edit and .update functions make sure you remove record level locking in the access options menu, it can be found under the "client settings" section of Access options, failing to do so will cause extreme bloating of your file as access will not drop record locks until you compact and repair the database. This may cause your database to become un-recoverable once it hits the 2gb limit for windows.

    Function addValueField()
    
    'Declarations
    Dim db As Database
    Dim rs1 As DAO.Recordset
    Dim rs2 As DAO.Recordset
    Dim qry As String
    Dim value As String
    Dim recordcount as Long
    
    
    Set db = CurrentDb()
    
    'Open a select query that is a join of table 1 and table 2
    'I have made Contact ID a foreign key in the second table
    qry = "SELECT Table1.[Contact ID], Table1.Name, Table2.FieldValue FROM Table1 INNER     JOIN Table2 ON Table1.[Contact ID] = Table2.[Contact ID(FK)] ORDER BY [Contact ID];"
    
    Set rs1 = db.OpenRecordset(qry, dbOpenDynaset)
    
    
    'Table 3 was filled with each record from table1, with a 3rd "Field Value" field to
    'be filled with your Value 1, Value 2 etc.
    qry = "SELECT * FROM Table3 ORDER BY [Contact ID]"
    
    Set rs2 = db.OpenRecordset(qry, dbOpenDynaset)
    
    'Ensure you have enough file locks to process records
    recordcount = rs1.recordcount
    DAO.DBEngine.SetOption DAO.dbMaxLocksPerFile, recordcount + 1000
    
    rs1.MoveFirst
    rs2.MoveFirst
    
    'Here we test to see if "Name" is the same in both recordsets, if it is, add the       FieldValue
    'to the FieldValue in Table3, otherwise move to the next record in table 3 and compare    again
    
    
    Do While Not rs1.EOF
        If IsNull(rs2![FieldValue]) = True Then
            If rs2![FieldValue] = "" Then
                rs2.Edit
                rs2![FieldValue] = rs1![FieldValue]
                rs2.Update
                rs1.MoveNext
            Else
                rs2.Edit
                rs2![FieldValue] = rs2![FieldValue] & "; " & rs1![FieldValue]
                rs2.Update
                rs1.MoveNext
            End If
        Else
            rs2.MoveNext
        End If
        Loop
    rs1.close
    rs2.close
    db.close
    set db = nothing
    set rs1 = nothing
    set rs2 = nothing
    
    End Function
    

提交回复
热议问题