How To Automatically Import Excel File To Access

simoran2

Board Regular
Joined
Jan 20, 2009
Messages
62
So I will have a weekly set of files dumped into a folder. These then need to be appended to three separate tables in access. Is there a way of automating this procedure?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Sure. If you create a Macro in Access and name it AutoExec, it will automatically run upon opening the database. So you can create a macro to import/append your files. You can use Windows Scheduler to open your database at the designated time/day, hence automating the process.
 
Upvote 0
Sure. If you create a Macro in Access and name it AutoExec, it will automatically run upon opening the database. So you can create a macro to import/append your files. You can use Windows Scheduler to open your database at the designated time/day, hence automating the process.

Ok thanks. How do i create a macro to append the files?
 
Upvote 0
Try this if it can help:
Code:
Private Sub FromExcelToAccess()
' 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:\IW6_Upload_Files\Working_Directory\Personal Files\LogTimeDatabase.mdb")
    ' open the database
    Set rs = db.OpenRecordset("tableLogTimeSheet", dbOpenTable)
    
    'While rs.RecordCount > 0
    'MsgBox rs.RecordCount
    '    rs.Delete
    '    rs.MoveNext
    'Wend
    ' get all records in a table
    R = 2 ' the start row in the worksheet
    Do While Len(Range("A" & R).Formula) > 0
    ' repeat until first empty cell in column A
    If rs.RecordCount = 0 Then GoTo jump:
        'MsgBox rs.Fields("LoginName")
        For i = 1 To rs.RecordCount
        On Error GoTo jump
        If Range("A" & R) = rs.Fields("LoginName") Then
            'MsgBox "Existing Name"
            With rs
                .Edit
                .Fields("Date") = Range("B" & R).Value
                .Fields("Login") = Range("C" & R).Value
                .Fields("Logout") = Range("D" & R).Value
                ' add more fields if necessary...
                .update ' stores the new record
            End With
            R = R + 1
        Else
jump:
            With rs
                .AddNew ' create a new record
                ' add values to each field in the record
                .Fields("LoginName") = Range("A" & R).Value
                .Fields("Date") = Range("B" & R).Value
                .Fields("Login") = Range("C" & R).Value
                .Fields("Logout") = Range("D" & R).Value
                ' add more fields if necessary...
                .update ' stores the new record
            End With
            R = R + 1 ' next row
            Exit For
        End If
        rs.MoveNext
        Next
        On Error GoTo 0
    Loop
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing
End Sub
Just change the ranges to suit your requirements
 
Upvote 0
Ok thanks. How do i create a macro to append the files?


You need to create a macro to use TransferSpreadsheet.

Select the Macro section in the database window, Create New Macro, then in the first action select TransferSpreadsheet follow the rest of the steps at the bottom. Add the other actions if doing this multiple times for the other workbooks.


Save the Macro as AutoExec.
 
Upvote 0
Ahh think i got it - build a macro to import then build the macro to append... ill try that before the VBA option but thanks to you both
You got it. The good thing about Macros is you do not need to know the first thing about VBA to use them. The one drawback is that you really cannot make them dynamic (i.e. if the file names change). However, another good tool is the "Convert Macros To Visual Basic" functionality. This will often give you much of the VBA code you need in the event that you need to convert your Macros to Visual Basic to make things more dynamic. It gives you a great starting point.
 
Upvote 0

Forum statistics

Threads
1,223,365
Messages
6,171,653
Members
452,415
Latest member
mansoorali

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top