【问题标题】:Open Excel table as DAO RecordSet for Appending打开 Excel 表作为 DAO 记录集以进行追加
【发布时间】:2020-04-13 23:16:47
【问题描述】:

我正在尝试从 DAO 更新 Excel 文件中的表,就好像它是数据库表一样。听起来应该是可能的,但我找不到任何有关使用 DAO 打开 Excel 的文档。

我已经打开文件并获得了记录集句柄,但我看到错误 3027“无法更新。数据库或对象是只读的。”当我尝试添加到记录集时。问题的一部分是我可以将工作表视为 tabledef,但我无法在该工作表中找到作为对象的 excel 表。也许我不知道将表作为记录集打开的正确语法。

我使用的代码指定了 dbOpenDynaset,就像您要使 Access 表可写一样。我正在尝试的可能吗?

错误发生在“.AddNew”上:

Const dbOpenDynaset As Long = 2
Const dbAppendOnly As Long = 8
Const dbOptimistic As Long = 3

Public Sub OpenExcelAsDB(ByVal excelFile As String) 
    Dim fileExtension As String
    fileExtension = Right$(excelFile, Len(excelFile) - InStrRev(excelFile, "."))

    Dim connectionString As String
    Select Case fileExtension
    Case "xls"
        connectionString = "Excel 8.0;HDR=YES;IMEX=1"
    Case "xlsx"
        connectionString = "Excel 12.0 Xml;HDR=YES;IMEX=1"
    Case "xlsb"
        connectionString = "Excel 12.0;HDR=YES;IMEX=1"
    Case "xlsm"
        connectionString = "Excel 12.0 Macro;HDR=YES;IMEX=1"
    Case Else
        connectionString = "Excel 8.0;HDR=Yes;IMEX=1"
    End Select

    With CreateObject("DAO.DBEngine.120")
        With .OpenDatabase(excelFile, False, False, connectionString)
            With .OpenRecordset("LogSheet$", dbOpenDynaset, dbAppendOnly, dbOptimistic) 
                .AddNew
                With .Fields()
                    .Item("errorNumber").Value = errorNumber
                    .Item("errorDescription").Value = errorDescription
                    .Item("customNote").Value = customNote
                    .Item("errorDate").Value = Now()
                    .Item("Username").Value = UserLogon
                    .Item("Computer").Value = ComputerName
                End With

                .Update
                .Close
            End With

            .Close
        End With
    End With
End Sub

【问题讨论】:

    标签: excel vba dao


    【解决方案1】:

    在这个时间点上,这似乎是一个相当古老的技术堆栈。我认为它大约在 1990 年代初,而且它肯定仍然存在,但支持似乎正在减少。所以,有了这个警告,你可以尝试这样的事情。

    Sub DAOFromExcelToAccess()
    ' exports data from the active worksheet to a table in an Access database
    ' this procedure must be edited before use
    Dim db As Database, rs As Recordset, r As Long
        Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb") 
        ' open the database
        Set rs = db.OpenRecordset("TableName", dbOpenTable) 
        ' get all records in a table
        r = 3 ' the start row in the worksheet
        Do While Len(Range("A" & r).Formula) > 0 
        ' repeat until first empty cell in column A
            With rs
                .AddNew ' create a new record
                ' add values to each field in the record
                .Fields("FieldName1") = Range("A" & r).Value
                .Fields("FieldName2") = Range("B" & r).Value
                .Fields("FieldNameN") = Range("C" & r).Value
                ' add more fields if necessary...
                .Update ' stores the new record
            End With
            r = r + 1 ' next row
        Loop
        rs.Close
        Set rs = Nothing
        db.Close
        Set db = Nothing
    End Sub
    

    就我个人而言,我会避开这类事情,而专注于某种云技术堆栈。

    【讨论】:

    • 两个问题:您正在打开一个旧的 MDB 文件。问题是关于使用 DAO 修改 Excel 文件。我可以通过这种方式读取 Excel 文件,但无法写入记录集。我对将我的 Excel 文件放在云中没有兴趣。同样,SQL express 在 Azure SQL 成本高昂的前提下是免费的。云并不适合所有人,您不应该像 90 年代那样推动 IBM 的大铁。
    猜你喜欢
    • 2022-01-20
    • 2017-08-31
    • 2011-04-25
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多