Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 18th, 2018, 06:39 PM
Authorized User
Points: 308, Level: 6
Points: 308, Level: 6 Points: 308, Level: 6 Points: 308, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2010
Posts: 59
Thanks: 12
Thanked 0 Times in 0 Posts
Default Compare Rows From Two Tables MS Access 2007

Hello.

I have a form with two buttons.
When the user clicks on the first button, I import two text files into two separate MS Access 2007 tables. They both have the same structure and have a Primary Key "ID" that is auto generated.

The text files fields are delimited by an "*" and there are a maximum of 19 fields per row, although most rows have less fields than that. The field names are Field1, Field2....Field19.

When the user then clicks on the second button, I need to compare these two tables, "Table Old" and "Table New". I need to know if "Table Old" has unmatched records in "Table New" and visa versa - rows in "Table New" that aren't in "Table Old".

I also need to know if any specific field in a matching row is different in "Table New" when compared to "Table" Old field.

My BIG problem is that there is no unique key that I can match on.

Here's an example of some rows in "Table Old" (ANSI X12 837 format).
ISA*00* *00* *ZZ*981234567 *ZZ*921234567 *180109*1512*^*00501*180109151*1*P*:~
GS*HC*982345678*924567890*20180109*1512*180109151* X*005010I20~
ST*837*18*005010I20~
BHT*0080*00*2018010915122527*20180109*1512*RP~
NM1*41*2*112233445*****46*983456701~
NM1*40*2*556677889*****46*923456702~
HL*1**20*1~
DTP*582*RD8*20180104-20180109~
NM1*CA*2*MARKETING BROS ENTERTAINMENT IN*****EI*112233445~

Here's an example of some rows in "Table New" (ANSI X12 837 format).
ISA*00* *00* *ZZ*981234567 *ZZ*921234567 *180110*1033*^*00501*180110103*1*P*:~
GS*HC*982345678*924567890*20180110*1033*180110103* X*005010I20~
ST*837*18*005010I20~
BHT*0080*00*2018011010330717*20180110*1033*RP~
NM1*42*2*112233445*****46*983456701~
N3*1234 RECONSIDERATION LANE~
N4*TUSTIN*CA*92688~
NM1*40*2*556677889*****46*923456702~
HL*1**20*1~
DTP*582*RD8*20180104-20180110~
NM1*CA*2*TRUMP BROS ENTERTAINMENT IN*****EI*112233445~

"Table New" has a couple of new rows.
I created a composite key on both tables using Field2 (e.g. "ISA") and Field3 (e.g. "00").

I first thought to create two record sets, one for each table, start comparing
row one to row one, etc., and when find a row that doesn't match, in either table, write that row to a new table "New Rows" or "Deleted Rows", depending on which record set it occurred in.

Definitely not very efficient!!

I then found some code on the Internet using the Union statement but have not been able to figure out how to get it to work.
(Here's a link to this code:
http://weblogs.sqlteam.com/jeffs/arc...1/10/2737.aspx)

SELECT TableName, ID, Field1, Field2, Field3
FROM
(
SELECT 'BillsOld' as TableName, O.ID, O.Field1, O.Field2, O.Field3
FROM BillsOld O
UNION ALL
SELECT 'BillsNew' as TableName, N.ID, N.Field1, N.Field2, N.Field3
FROM BillsNew N
) tmp
GROUP BY O.TableName, O.ID, O.Field1, O.Field2, O.Field3
HAVING COUNT(*) = 1
ORDER BY O.ID;

The problem is that at some point the Primary Key ID will not match because there may be less rows in "Table Old" or more rows in "Table New".
However, I'd like to have the ID in the results as it will help the user to identify the row where there are changes.
The "GROUP BY" requires me to use all the fields in the SELECT statement.

BTW, I just tried this code to see if the UNION is working per the article, but it returned all the rows from "Table New" and "Table Old" - which I used the same input file to populate.
SELECT tmp.TableName, tmp.ID
FROM
(SELECT 'BillsOld' as TableName, O.ID
FROM BillsOld O
UNION ALL
SELECT 'BillsNew' as TableName, N.ID
FROM BillsNew N ) AS tmp
GROUP BY tmp.TableName, tmp.ID
HAVING (Count(*)=1)
ORDER BY tmp.ID;

I just realized that all the rows are returned because the 'TableName' value is different!

How else could I know which table the row was coming from?!

Any suggestions will be greatly appreciated!

Thanks,
Rita

Last edited by ritagr; January 18th, 2018 at 08:12 PM..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting VB 2005 with MS Access 2007 Bazilin BOOK: Beginning Spring Framework 2 ISBN: 978-0-470-10161-2 0 July 21st, 2013 05:32 PM
left join on multiple tables in MS access database 2007 sql code mukta Access 2 April 11th, 2012 07:27 AM
Ms access 2007 Attachments Carl All Other Wrox Books 0 July 27th, 2009 06:55 PM
Connecting MS Access 2007 with C++ curiaquita Visual C++ 0 February 3rd, 2009 01:59 PM
Compare Two MS access db file and find missing rec lawsoncobol Reporting Services 0 August 3rd, 2006 04:14 AM



All times are GMT -4. The time now is 02:04 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.