保存Excel数据的Access数据库

问题描述 投票:-2回答:1

我打算保存特定小区的参考价值和访问数据库中的表,其中包括学生姓名,学#,选择的宏文件。我已提取在Excel中的各种细胞以上PERFORMA项目。这必须被保存在访问DATABSE表。

谢谢。

database vba access
1个回答
0
投票

这里是一个方式做你想做的事,使用ACCDB格式。

Sub InsertIntoX2()

    Dim cn As ADODB.Connection, rs As ADODB.Recordset, row As Long
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
            "Data Source=C:\Users\Excel\Desktop\Test.accdb;"

    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "tblTrx", cn, adOpenKeyset, adLockOptimistic, adCmdTable

    row = 3    ' the start row in the worksheet
    Do While Not IsEmpty(Worksheets("Sheet1").Range("A" & row))

        With rs
            .AddNew    ' create a new record
            .Fields("ID") = Worksheets("Sheet1").Range("A" & row).Value
            .Fields("Product") = Worksheets("Sheet1").Range("B" & row).Value
            .Fields("ProdDate") = Worksheets("Sheet1").Range("C" & row).Value
            .Update
        End With
        row = row + 1
    Loop

    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

End Sub

只要设定一个基准,在Excel中,为“Microsoft ActiveX数据对象2.8库”。

© www.soinside.com 2019 - 2024. All rights reserved.