MS Access: Inserting Records with Visual Basic and DAO

This example shows you how to add records with VBA and DAO instead of with SQL queries. Sometimes, it’s easier to do it this way.b (The original intent was to simultaneously create a relation between the new record and another table, but this didn’t happen.)


Public Sub importFolks()
    Dim dbs As Database
    Dim rstFrom As Recordset
    Dim rstTo As DAO.Recordset
    
    Set dbs = CurrentDb()
    Set rstFrom = dbs.OpenRecordset("tmp match up list to db")
    
    Set rstTo = dbs.OpenRecordset("tblActivists", dbOpenDynaset, dbAppendOnly)
    
    rstFrom.MoveFirst
    a = 0
    Do Until (rstFrom.EOF)
        rstTo.AddNew
        rstTo.Fields("Fname") = rstFrom.Fields("Field11")
        rstTo.Fields("Lname") = rstFrom.Fields("Field12")
        rstTo.Fields("Email") = Nz(rstFrom.Fields("email"))
        parts = ParsePhoneNumbers(Nz(rstFrom.Fields("Phone")), 1)
        rstTo.Fields("WCode") = parts(1)
        rstTo.Fields("WPhone") = parts(2)
        parts = ParsePhoneNumbers(Nz(rstFrom.Fields("FAX")), 1)
        rstTo.Fields("FCode") = parts(1)
        rstTo.Fields("Fax") = parts(2)
        rstTo.Fields("Cell") = rstFrom.Fields("cellNumber")
        
        rstTo.Update
        
        rstFrom.MoveNext
        a = a + 1
    Loop
End Sub