Send Excel Data to Access

Send Excel Data to Access

While working on client projects, I enter all my timesheet data in Excel. Why do I use Excel? Here are my top reasons (excuses):

  1. Excel is usually open on my computer, so it’s the easiest program for me to use
  2. That’s the way I’ve always done it
  3. Other important, and perfect valid, reasons that are long forgotten.

Invoices in Microsoft Access

Even though I enter the project working time data in Excel, I create my invoices in Microsoft Access. The reasons are similar to the list above!

As a result, 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 a look at how it works.

Filter Completed Items

On my Excel timesheet, there’s a Send to DB button.

That button runs a macro which filters the completed rows to a different worksheet, named CopyToDB.

On the CopyToDB sheet, the Advanced Filter extract range has:

  • only the data columns that I need for the export
  • columns n the order that I need them for the Access database.

For examples of similar code, go to the Excel Advanced Filter Macros page on my Contextures site.

Send Data to Access

Once the data’s on the CopyToDB sheet, I give it a quick glance, to make sure everything looks okay.

Next, I click the Send to Access button at the top of that sheet.

That button runs a macro that:

  • opens an ADO connection to the database
  • inserts the Excel data
  • closes the connection
  • clears the export range on CopyToDB sheet, to remove the data.

The macro code is further down the page.

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 entered 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, if needed.

For example, if the database moves to a different folder, I just type the new address in the Database cell.

connection string and command text string
connection string and command text string

Excel VBA Code – Send to Access

I’m not an ADO connection expert, so perhaps this Excel VBA code can be improved, but this macro does what I need:

'=======================
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
'========================

0 thoughts on “Send Excel Data to Access”

  1. “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 🙂

  2. 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

  3. 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.

  4. 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?

  5. This is great! I’m very new to Access and want to understand fully how this works. What type of query is qryProjDataFromExcel?

  6. Thanks for the lovely code. I am pretty new to this. I struggle to run the code as it stops at adoCn.execute sCommand. I think it’s to do with the “DataToExport” Range. How did you set up that range, so the sql string finds that data set?
    Thanks
    Joe

    1. @Joe, I use a macro to filter the data onto the export sheet, and name the range.
      Here’s the part that names the range:

      Sub SetExportRange()
      Dim wsCopy As Worksheet
      Set wsCopy = Worksheets("CopyToDB")
      Dim rngCopy As Range
        Set rngCopy = wsCopy.Range("C3").CurrentRegion
        ActiveWorkbook.Names.Add "DataToExport", _
            "=" & wsCopy.Name & "!" _
              & rngCopy.Address
      Set wsCopy = Nothing
      Set rngCopy = Nothing
      End Sub

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.