Jay Paredes
Jay Paredes

Reputation: 1

Adding Records in a Table using ACCESS VBA

My Code is:

Public Sub ImportCommonFields()

    Set rs1 = CurrentDb.OpenRecordset("Imported Table" & " " & TableCtr)
    Set cf = CurrentDb.OpenRecordset("CommonFields")
    
    cf.AddNew
    cf("FieldNames") = rs1.Fields

    cf.Update
    Set fld = Nothing

End Sub

I am currently getting the column names (Fields) from the table in rs1 and would like to import them to an existing table "CommonFields" under the column "FieldNames".

Upvotes: 0

Views: 52

Answers (1)

Gustav
Gustav

Reputation: 55806

Loop the Fields collection:

Public Sub ImportCommonFields()

    Dim fld     As DAO.Field

    Set rs1 = CurrentDb.OpenRecordset("Imported Table" & " " & TableCtr)
    Set cf = CurrentDb.OpenRecordset("CommonFields")
    
    For Each fld In rs1.Fields
        cf.AddNew
        cf("FieldNames").Value = fld.Name
        cf.Update
    Next

    cf.Close
    rs1.Close

    Set fld = Nothing

End Sub

Upvotes: 1

Related Questions