0

i am trying to copy a collection database in vba to access 2010 database, below is a code that works. But what i want to know is if there is some easier or quicker way of doing this especially when i am going to have large no or fields and records.

Dim plist As New partlist 'plist is a class
Dim plcol As New Collection
Sub DBInsert1()
' this function will add add 2 records from a collection to access database

Dim DB As DAO.Database
Dim RS As DAO.Recordset

plist.itemno = "1"
plist.itemname = "one"
plcol.Add plist
Set plist = Nothing
plist.itemno = "2"
plist.itemname = "two"
plcol.Add plist
'above plcol collection has a set of info


    ' open database
    Set DB = DAO.OpenDatabase("D:\tblImport.accdb")

    ' open table as a recordset
    Set RS = DB.OpenRecordset("Table1")


    For Each plist In plcol
        ' add a record to the recordset
        RS.AddNew

        RS.Fields("itemno") = plist.itemno
        RS.Fields("itemname") = plist.itemname

        ' write back recordset to database
        RS.Update
        Set plist = Nothing
    Next
    ' important! cleanup
    RS.Close

    ' forget to close the DB will leave the LDB lock file on the disk
    DB.Close

    Set RS = Nothing
    Set DB = Nothing
End Sub

1 Answer 1

0

you could try an ADO connection to the DB www.connectionstrings.com and https://support.microsoft.com/en-us/kb/168336

CONNECTION.execute "INSERT INTO Table1 (itemno,itemname) values('" & plist.itemno & "','" & plist.itemname & ')"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.