打开Excel表作为DAO RecordSet进行追加[重复]

esyap4oy  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(115)

此问题已在此处有答案

Insert a row in an Excel Recordset through Excel VBA(1个答案)
2天前关闭。
我试图从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
wkftcu5l

wkftcu5l1#

数据访问对象(DAO)已经过时。ActiveX数据对象(ADO)。代码需要使用ADODB.Connection对象重写,如本问题所示:Insert a row in an Excel Recordset through Excel VBA

g6ll5ycj

g6ll5ycj2#

这似乎是一个相当古老的技术堆栈,在这个时候。我认为这是在20世纪90年代初,它肯定仍然存在,但支持似乎正在减少。所以,有了这个警告,你可以尝试这样的东西。

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

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

相关问题