Importing Excel worksheet range to Ms Access Table

后端 未结 3 1892
你的背包
你的背包 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:00

    You can use a query instead of iterating through a recordset:

    Sub AccessCode()
        Application.ScreenUpdating = False
        Dim db As Database
        Dim rs As DAO.Recordset
    
        Set db = OpenDatabase("C:\Users\user\Desktop\Test Copy.accdb")
        db.Execute "INSERT INTO [Fact Table] ([GUID], [StageID], etc) " & _
        "SELECT * FROM [SheetName$G:M] " & _
        "IN """ & ActiveWorkbook.FullName & """'Excel 12.0 Macro;HDR=No;'"
    End Sub
    

    This has numerous advantages, such as often being faster because you don't have to iterate through all the fields.

    If you would trigger the import from Access instead of Excel, you wouldn't even need VBA to execute the query.

提交回复
热议问题