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:
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
‘ Append data from Excel worksheet
‘ Close the connection to the workbook
Set adoCn = Nothing
Set wsQS = Nothing