excel - 使用 VBA 将数据从 Excel 导出到 Access

标签 excel ms-access vba export

我的 Excel 文件中有一个表,其中包含一些数据,我想使用 VBA 代码将这些数据导出到 Access 上的数据库(在名为 Water Quality 的数据库的具体表中),以避免每次打开我的数据库是时候我想介绍更多相关数据了。

目前我有这段代码,但它不起作用......

Sub Button14_Click()

' Macro purpose: To add record to Access database using ADO and SQL
' NOTE:  Reference to Microsoft ActiveX Data Objects Libary required

' Exports data from the active worksheet to a table in an Access database

'Dim cnt As New ADODB.Connection
'Dim rst As New ADODB.Recordset
Dim cnt As DAO.Database
Dim rst As Recordset
Dim dbPath As String
Dim tblName As String
Dim rngColHeads As Range
Dim rngTblRcds As Range
Dim colHead As String
Dim rcdDetail As String
Dim ch As Integer
Dim cl As Integer
Dim notNull As Boolean
Dim sConnect As String

'Set the string to the path of your database as defined on the worksheet
dbPath = "C:\Documents and Settings\Administrador\Mis documentos\MonEAU\modelEAU Database V.2.accdb"
tblName = "Water Quality"
Set rngColHeads = ActiveSheet.Range("tblHeadings")
Set rngTblRcds = ActiveSheet.Range("tblRecords")

'Set the database connection string here
sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & dbPath & "';"     'For use with *.accdb files
' sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";"     'For use with *.mdb files

'Concatenate a string with the names of the column headings
colHead = " ("
For ch = 1 To rngColHeads.Count
    colHead = colHead & rngColHeads.Columns(ch).Value
    Select Case ch
        Case Is = rngColHeads.Count
            colHead = colHead & ")"
        Case Else
            colHead = colHead & ","
    End Select
Next ch

'Open connection to the database
cnt.Open sConnect
'Begin transaction processing
On Error GoTo EndUpdate
cnt.BeginTrans

'Insert records into database from worksheet table
For cl = 1 To rngTblRcds.Rows.Count
    'Assume record is completely Null, and open record string for concatenation
    notNull = False
    rcdDetail = "('"

    'Evaluate field in the record
    For ch = 1 To rngColHeads.Count
        Select Case rngTblRcds.Rows(cl).Columns(ch).Value

                'if empty, append value of null to string
            Case Is = Empty
                Select Case ch
                    Case Is = rngColHeads.Count
                        rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL)"
                    Case Else
                        rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL,'"
                End Select

                'if not empty, set notNull to true, and append value to string
            Case Else
                notNull = True
                Select Case ch
                    Case Is = rngColHeads.Count
                        rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "')"
                    Case Else
                        rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "','"
                End Select
        End Select
    Next ch

    'If record consists of only Null values, do not insert it to table, otherwise
    'insert the record
    Select Case notNull
        Case Is = True
            rst.Open "INSERT INTO " & tblName & colHead & " VALUES " & rcdDetail, cnt
        Case Is = False
            'do not insert record
    End Select
Next cl

EndUpdate:
'Check if error was encounted
If Err.Number <> 0 Then
    'Error encountered.  Rollback transaction and inform user
    On Error Resume Next
    cnt.RollbackTrans
    MsgBox "There was an error.  Update was not succesful!", vbCritical, "Error!"
Else
    On Error Resume Next
    cnt.CommitTrans
End If

'Close the ADO objects
cnt.Close
Set rst = Nothing
Set cnt = Nothing
On Error GoTo 0

End Sub

目前的问题是,当我调试代码时,在函数“cnt.Open sConnect”上出现编译错误:“找不到方法或数据成员”。

如果可能的话,我们将不胜感激任何帮助。

注意:我使用的是 Office 2010。

最佳答案

您的编译错误是由于以下两行造成的:

Dim cnt As DAO.Database
cnt.Open sConnect

DAO.Database 对象没有 .Open 方法,这解释了“未找到方法或数据成员”。很多时候,错误消息可能有些模糊,而且没有多大帮助。然而,在这种情况下,我想不出错误消息如何能更清晰。

还有一些我不明白的事情。 sConnect 看起来像一个 ADO 连接字符串。但cnt是一个DAO(数据库)对象。您无法像在一条语句中那样混搭两个对象模型。

在事件变量声明之前有这个:

'Dim cnt As New ADODB.Connection

然后在您的程序中,您将:

'Close the ADO objects
cnt.Close

因此,也许您最初打算将 cnt 用作 ADO.Connection 对象,并且在将其切换为 DAO 后并未调整其余代码.Database 对象。

我建议您修改代码以理清 DAO 与 ADO 的混淆,然后向我们展示新代码(如果您还有任何剩余问题)。请仅向我们展示重现您希望解决的问题所需的最少测试代码。 TIA 供您考虑。

关于excel - 使用 VBA 将数据从 Excel 导出到 Access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232619/

相关文章:

java - 使用 DataNucleus 从 MySQL 到 XLS (java)

c# - 在 C# 中计算过滤的 excel 范围的行数

ms-access - Access 数据类型转换为 bool 值

vba - 将行复制到新工作表 VBA

vb.net - VB.NET 中需要类似 Excel 的网格

ms-access - MS Access - 更改表单中的值后立即写入表

sql - Access sql内连接并统计2个表

sql - 如何通过sql向access数据库插入超链接?

javascript - JavaScript 中的 VBA 等效项,用于 Google 表格的 Google Apps 脚本

excel - 对于每列突出显示最大值(Excel)