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

前端 未结 3 1366
一整个雨季
一整个雨季 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:37

    Building on pegicity's answer, my eventual code was:

    Option Compare Database
    
    Sub Concatenate(strTableToConcatenate As String, strFieldToConcatenate As String, strIDField As String)
    
    Dim rsSource As DAO.Recordset
    Dim rsDestination As DAO.Recordset
    Dim qry As String
    Dim strSourceTable As String
    Dim i As Integer
    Dim strFieldName As String
    Dim strValue As String
    Dim intConcatenateID As Integer
    Dim intSortID As Integer
    
    strSourceTable = strTableToConcatenate & " (Concatenate)" 'Creates a duplicate copy of the table to be concatenated and empties the original table'
    DeleteTable (strSourceTable)
    DoCmd.CopyObject , strSourceTable, acTable, strTableToConcatenate
    qry = "DELETE FROM [" & strTableToConcatenate & "]"
    CurrentDb.Execute (qry)
    
    qry = "ALTER TABLE [" & strTableToConcatenate & "] ALTER COLUMN [" & strFieldToConcatenate & "] memo" 'Changes the DataType of the field to be concatenated to Memo, as the result may be considerably longer than the original data'
    CurrentDb.Execute (qry)
    
    i = 0
    intCurrentID = 0
    
    qry = "SELECT * FROM [" & strSourceTable & "] ORDER BY [" & strIDField & "], [" & strFieldToConcatenate & "]"
    Set rsSource = CurrentDb.OpenRecordset(qry, dbOpenDynaset)
    qry = "SELECT * FROM [" & strTableToConcatenate & "]"
    Set rsDestination = CurrentDb.OpenRecordset(qry, dbOpenDynaset)
    
    For Each fld In rsSource.Fields 'Finds the column number of the fields you are sorting by and concatenating from your source table.'
        strFieldName = rsSource.Fields(i).Name
        If strFieldName = strFieldToConcatenate Then
            intConcatenateID = i
        ElseIf strFieldName = strIDField Then
            intSortID = i
        End If
        i = i + 1
    Next
    
    If rsSource.recordcount <> 0 Then
    
        rsSource.MoveFirst
        intCurrentID = rsSource.Fields(intSortID).Value
        strConcatenateValue = ""
    
        While Not rsSource.EOF 'The source recordset is sorted by your designated sort field, so any duplicates of that field will be next to each other. If the row below has the same id as the row above, the sub continues to build the concatenated value. If the row changes, it adds the concatenated value to the destination record set.'
    
           If intCurrentID = rsSource.Fields(intSortID).Value Then
    
                strConcatenateValue = strConcatenateValue & "," & rsSource.Fields(intConcatenateID).Value
                rsSource.MoveNext
    
           Else
                rsDestination.AddNew
    
                i = 0
    
                If Len(strConcatenateValue) > 0 Then
                    strConcatenateValue = Right(strConcatenateValue, Len(strConcatenateValue) - 1)
                End If
    
                For Each fld In rsSource.Fields
                    strFieldName = rsSource.Fields(i).Name
                    If strFieldName = strFieldToConcatenate Then
                         strValue = strConcatenateValue
                    ElseIf strFieldName = strIDField Then
                        strValue = intCurrentID
                    Else
                        strValue = rsSource.Fields(i).Value
                    End If
                    rsDestination.Fields(strFieldName) = "" & strValue & ""
                    i = i + 1
                Next
    
                rsDestination.Update
                intCurrentID = rsSource.Fields(intSortID).Value
                strConcatenateValue = ""
    
           End If
    
         Wend
    
    End If
    
    rsSource.Close
    rsDestination.Close
    Set rsSource = Nothing
    Set rsDestination = Nothing
    
    End Sub
    

提交回复
热议问题