Send Excel Data to Access
While working on client projects, I enter all my timesheet data in Excel because:
- Excel’s usually open, so it’s the easiest program for me to use
- that’s the way I’ve always done it
- other important reasons that are long forgotten.
However, I create my invoices in Access, so I have to move the data from Excel to Access, usually at the end of the workday. Last week, JP asked about the code that I use, so here’s how it works.
Filter the Completed Items
On the Excel timesheet there’s a Send to DB button that runs a macro to filter the completed rows to a different worksheet. The Advanced Filter extract range has just the columns that I need for the export, in the order that I want them.
Send Data to Access
Once the data’s on the export sheet, I give it a quick glance, to make sure everything looks okay. Then I click the Send to Access button at the top of that sheet. It runs a macro that opens an ADO connection to the database, inserts the Excel data, and closes the connection. Finally, it clears the export range, to remove the data.
How It Works
For the export code, the connection string and command text string are on the QueryStrings worksheet in the the Excel workbook. I enter the info in the green cells, and the strings for the macro are calculated in the white cells.
This makes it easy to modify the connection strings. For example, if the database moves to a different folder, I just type the new address in the Database cell.
I’m not a connection expert, so perhaps this can be improved, but here’s my code:
‘=======================
Sub SendDataToAccess()
Dim wsQS As Worksheet
Dim sConnect As String
Dim sCommand As String
Dim adoCn As ADODB.Connection
Set wsQS = Worksheets(”QueryStrings”)
Set adoCn = New ADODB.Connection
sConnect = wsQS.Range(”rngConnect”).Value
sCommand = wsQS.Range(”rngCommand”).Value
‘ Get ADO connection to the workbook
adoCn.Open sConnect
‘ Append data from Excel worksheet
adoCn.Execute sCommand
‘ Close the connection to the workbook
adoCn.Close
Set adoCn = Nothing
Worksheets(”CopyToDB”).Range(”DataToExport”).Offset(1, 0).ClearContents
Worksheets(”Proj DB”).Activate
Set wsQS = Nothing
End Sub
‘========================


November 4th, 2008 at 12:55 am
“What kind of Virgo posts a typo?” (Nov 01)
“… the strings for the macro are are calculated in the white cells.” (Nov 04)
That kind of Virgo
November 4th, 2008 at 1:01 am
Oh, I hate when that happens.
Thanks John!
November 4th, 2008 at 5:36 am
Interesting post Debrah. So what would you do with *existing* records?
November 4th, 2008 at 7:09 am
Nice! Do you find that the data is appended to the bottom of the table? I usually instantiate Access and use the DoCmd.TransferSpreadsheet to import worksheets. How many rows do you usually import and how long does it take?
–JP
November 4th, 2008 at 9:08 am
Jan Karel, I sometimes manually delete the completed records in the main table, but usually just remove the end time, and change the date to the next day, so I can record more work on that project.
JP, the Access table has an AutoNumber field, so the appended records end up at the bottom of the table. There are only 5-6 records most days, and it takes about a second to export them.
November 5th, 2008 at 12:47 am
Debra,
I was hoping for something as simple as the INSERT INTO myTable IN SELECT * FROM …
I pull data from an Access table into Excel, update and add records and write back. The table has an autonumber field which is empty for new records in Excel so I know when to use INSERT or UPDATE.
I expect the INSERT SQL you wrote above does not work on a filtered sheet so I’d have to split the table into two sheets (new records and existing records) to make that work, right?
November 7th, 2008 at 9:02 am
Jan Karel, that sounds right.
November 10th, 2008 at 2:59 pm
[...] Sending Excel Data to MS Access [...]