Importing Excel worksheet range to Ms Access Table

后端 未结 3 1990
你的背包
你的背包 2021-01-22 19:13

Good Afternoon,

I have created a Macro that uploads data to a access database ( both on my desktop). The problem is it I keep getting errors when I try to expand the ran

3条回答
  •  独厮守ぢ
    2021-01-22 20:10

    Change the rs section to this one:

    With rs
        .addnew
        !GUID = Range("g2").Value
        !StageID = Range("h2").Value
        '...etc
        .Update
    End With
    

    MSDN source

    Use the AddNew method to create and add a new record in the Recordset object named by recordset. This method sets the fields to default values, and if no default values are specified, it sets the fields to Null (the default values specified for a table-type Recordset).

    After you modify the new record, use the Update method to save the changes and add the record to the Recordset. No changes occur in the database until you use the Update method.

    Edit: This is how your code should look like, when you change the rs section with the code above:

    Sub AccessCode()
    
        Application.ScreenUpdating = False
    
        Dim db As Database
        Dim rs As DAO.Recordset
    
        Set db = OpenDatabase("C:\Users\user\Desktop\Test Copy.accdb")
        Set rs = db.OpenRecordset("Fact Table", dbOpenTable)
        
        With rs
            .addnew
            !GUID = Range("g2").Value
            !StageID = Range("h2").Value
            '...etc
            .Update
            .Close
        End With
    
        Application.ScreenUpdating = True
        MsgBox " Upload To PMO Database Successful."
    
    End Sub
    

提交回复
热议问题