views:

526

answers:

5

I have an Excel spreadsheet provided as a report, when really its more of a database, with the row number acting as primary key. I need to compare some columns in this set with a different set of records from an Access 2007 database. To make matters more complicated, this needs to be done automatically, without user input, on a regular schedule.

What is the best way to get the data out of the spreadsheet, and what is the best way to run the comparison. As for getting data out of the spreadsheet, ACEDAO seems the best option, since this is Excel 2007, but MSDN has no information I could find on how to query Excel using DAO/ACEDAO, only to export to Excel using DAO/ACEDAO. As for running the comparison, beyond comparing each column of each entry, I can't figure out a better way to do it.

+3  A: 
Robert Harvey
Thanks for the idea of Office Automation. I was planning on running the Macro powering the updates at startup, and launching that DB in Access with a batch file.
A. Scagnelli
I've created this structure in a query (JOIN LEFT from spreadhseet to table, where ID Is NULL); however, it fails the Is Null test and returns all records, not ones absent from the table.
A. Scagnelli
In order for a row to be omitted from the output set, each of the joins must match values on both sides. Is it possible one of the joins points to the wrong field on one side? Start with a single join, observe the empty set, and add additional joins one at a time, testing as you go.
Robert Harvey
The above structure works, the error came from a different source (the test data rather than the SQL query). However, how would it be possible to go in the other direction (return all the records in a table that are not in a linked table) while there are additional columns in the table that are not in the linked table.
A. Scagnelli
Nevermind that last comment, I was able to figure it out.
A. Scagnelli
+1  A: 

If the Excel worksheets are structured as lists then ADO might be the way to go - more info here


edit: there are several different possible approaches here

  • use ADO in Excel to read from the Access database and use VBA to compare the returned values to the Excel worksheet. Probably only going to be useful for simple comparisons on relatively small recordsets

  • as described in Robert Harvey's answer, make the Excel worksheet a linked table in the Access database and query with SQL using Access itself

  • using ADO and ADOX make the Excel worksheet a linked table in the Access database and query with SQL using ADO in Excel

I didn't realise that DAO was back in use with Access 2007. As I use Excel to connect to databases, DAO has been deprecated for years (with ADO being the preferred connection method). I now know better

barrowc
In a previous question, I asked whether ADO or DAO would be better, and DAO was the response, since ACEDAO is the standard for ACEDB in Access 2k7: http://stackoverflow.com/questions/1039224/is-it-better-to-use-ado-or-dao-in-access-2007/1041717
A. Scagnelli
In answer to *that* question I answered (paraphrasing) that you shouldn't ACEDAO exclusively or ADO exclusively but use whichever one makes most sense in any given case. In better than 99% of cases there is no significant difference between the two. This is software engineering, not philosophy :)
onedaywhen
A. Scagnelli
'compatible'? The answer is no but why do you think that would matter?
onedaywhen
If they are compatible (ie: mixing them lets an ADO object read an ACEDAO recordset), then the distinction doesn't matter; but for the current project, it does matter since one needs to be used.
A. Scagnelli
I've edited some more thoughts into my answer
barrowc
An ACEDAO recordset cannot read an ACEDAO recordset. Why do you think it does i.e. what are you doing here?
onedaywhen
Because you can fabricate an ADO recordset then you can read into it data in memory data and that in-memory data can be an ACEDAO recordset, ADO recordset, whatever. So an ADO recordset is more 'compatible', for what that's worth. But I'm not sure what you end goal is.
onedaywhen
+1  A: 

Using ACE you can query the workbook directly e.g.

SELECT S1.seq AS seq_ACE, 
       S2.seq AS seq_Excel
  FROM Sequence AS S1
       LEFT OUTER JOIN [Excel 12.0;Database=C:\Test.xlsx;].Sequence AS S2
          ON S1.seq = S2.seq;
onedaywhen
A: 

You can create an ODBC data source for the Excel data and another for the database data and then use an ODBC data comparison tool such as CompareData to compare the data

Farid Z
A: 

I know I am gonna get slammed for this because it isn't very "programmer-y", but if this is a one-off and the resulting Access table is just a temporary working copy of the data for a few queries you can just copy/paste the data directly into a table in datasheet view.

It is a quick, dirty, no-frills way to do it, but it works and requires very little extra work.

Note: If the table is REALLY large or is actively in use by others, you might not want to do this. It can be slow and introduce a lot of locking.

JohnFx
It can also have unpredicatable results in regard to target data types. But you're right to add it into the mix for one-offs.
David-W-Fenton
I've used it a million times for requests like "Can you delete all of the items from TableX with one of the product codes listed in the attached spreadsheet?"
JohnFx