VBA集合到Access 2010数据库

时间:2016-03-11 11:27:21

标签: vba collections ms-access-2010

我正在尝试复制vba中的集合数据库以访问2010数据库,下面是一个有效的代码。但我想知道的是,如果有更简单或更快捷的方法,特别是当我要有大量的没有或字段和记录时。

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 个答案:

答案 0 :(得分:0)

您可以尝试ADO连接到DB www.connectionstrings.com和https://support.microsoft.com/en-us/kb/168336

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