• Home
  • About

Entries RSS | Comments RSS
  • Beginning PivotTables in Excel 2007 will introduce you to the exciting new pivot table features in Excel 2007. Create quick summaries and pivot charts, add impact with traffic light icons, design calculated fields, group dates and numbers.

  • Categories

  • Archives

  • Recent Posts

    • Create Single Click Desktop Icons
    • Open a Second Window in Outlook 2007
    • Excel Twitters 20090103
    • Excel Twitters 2008127
    • Have an Excellent Christmas!
  • Pages

    • About
  • Subscribe in a reader

Send Excel Data to Access

Posted on November 4th, 2008 by Debra Dalgleish

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

‘========================

Share/Save/Bookmark

Filed under: Access, Data Entry, Excel, Time Management

« Are You a Star Maker? Procrastination Revisited »

8 Responses to “Send Excel Data to Access”


  1. Comment from John McTigue
    Time: November 4, 2008, 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 :)


  2. Comment from Debra Dalgleish
    Time: November 4, 2008, 1:01 am

    Oh, I hate when that happens. ;-)
    Thanks John!


  3. Comment from Jan Karel Pieterse
    Time: November 4, 2008, 5:36 am

    Interesting post Debrah. So what would you do with *existing* records?


  4. Comment from JP
    Time: November 4, 2008, 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


  5. Comment from Debra Dalgleish
    Time: November 4, 2008, 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.


  6. Comment from Jan Karel Pieterse
    Time: November 5, 2008, 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?


  7. Comment from Debra Dalgleish
    Time: November 7, 2008, 9:02 am

    Jan Karel, that sounds right.


  8. Pingback from Excel Links of the Week - All Downloads at One Place Edition | Pointy Haired Dilbert - Chandoo.org
    Time: November 10, 2008, 2:59 pm

    [...] Sending Excel Data to MS Access [...]

Leave a Reply

  • Subscribe

    Subscribe in a reader
  • Email Updates

    Subscribe by Email
  • Calendar

    November 2008
    S M T W T F S
    « Oct   Dec »
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30  
  • Contextures

    • Contextures Excel Pages
    • My Video Tutorials
  • Excel

    • Daily Dose of Excel
    • Excel Team Blog
    • Jan Karel Pieterse
    • JP’s Excel/Outlook
    • PTS Blog
    • Spreadsheet Page Blog
    • TVMCalcs Excel Blog
  • General

    • Hodge Blog
    • J-Walk Blog
    • SuiteMinute