Fast update of Access data with Excel data using Excel VBA

前端 未结 1 580
礼貌的吻别
礼貌的吻别 2020-12-22 03:43

By \"fast\" I mean using the UPDATE SQL query as opposed to looping through every recordset.

Here I found this nice query:

\'\'Batch upd         


        
相关标签:
1条回答
  • 2020-12-22 04:00

    I think you are looking for code like this:

    Dim db As Object
    Dim engine As Object
    Set engine = CreateObject("DAO.DBEngine.120")
    Set db = engine.OpenDatabase("C:\your\database.accdb")
    
    Dim sql As String
    sql = "UPDATE AccTable  AS acc " & _
        " INNER JOIN (SELECT * FROM [NamedRange] IN ""C:\your\excel\file.xlsx"" ""Excel 12.0 xml;"" ) AS xls " & _
        " ON acc.ID = xls.ID " & _
        " Set acc.SomeField = xls.SomeField "
    
    db.Execute sql
    

    Unfortunately with all current versions of Access/DAO.DBEngine this will raise the error message You cannot edit this field because it resides in a linked Excel spreadsheet. The ability to edit data in a linked Excel spreadsheet has been disabled in this Access release. because Microsoft has deliberately disabled this feature for security reasons.

    And, yes, this is nonsense, because you are not even trying to update the data in Excel, but still it does not work anymore. And as far as I know, it applies to all possible approaches to link an Excel-Sheet to an Access-Table in a single SQL statement.

    As a workaroaund you could either try to import the Excel-data to an Access database table (I do not know if this still works!) and then link the two Access tables for an update, or you'll have to resort to the looping and updating single records.

    0 讨论(0)
提交回复
热议问题