Dynamic Imports to Access

Ergo1291

New Member
Joined
Feb 5, 2014
Messages
2
Hi fellow XLers,

I have a question about importing excel data into a database (Access) via VBA:

Current Situation:

- 1 Excel Sheet containing:
- 98 Columns (A-CT) with Row 1 being the header ¦ includes a Unique ID (UID)
- 6000 Rows (changing all the time)

- 1 Access Table containing:
- 98 Fields (same names as column headers (Row 1) in Excel) ¦ UID is the Primary Key
- 11389 records

What I need to do:

I want to check if an entry exists in the DB, and if so update a number of fields according to the entries in the Excel file.
If it doesnt exist i want to write the entire row into the corresponding fields.

My Problem/Question:

I am new to Access, but i managed to write specific cells from Excel into their corresponding fields in Access by using such a code:

IN EXCEL Module:
Code:
With rs
            ' create a new record
            .AddNew 

            ' add values to each field in the record
            .Fields("FieldName1") = Range("A" & r).Value
            .Fields("FieldName2") = Range("B" & r).Value
            .Fields("FieldName3") = Range("C" & r).Value
            ' ......
            .Fields("FieldNameN") = Range("CL" & r).Value
            
            ' stores the new record
           .Update 
End With
r = r + 1 ' next row

as you might see, i would have to write 98 lines of code to add all the columns. I am sure there is a much cleaner solution to this problem, but I have no idea where to start.

Any help is very welcome!!!
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,216,099
Messages
6,128,820
Members
449,469
Latest member
Kingwi11y

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