Solved

Compare Tables MS Access 2007

Posted on 2012-03-21
10
345 Views
Last Modified: 2012-04-02
Good Afternoon All,

I have two tables in an access database.  The first table is tblCAD and the second table is tblPCR.  I am trying to compare the field in tblCAD.IncNum to tblPCR eIncNum.  The data is coming from a website.  TblCAD represents CAD dispatches from our local 9-1-1 center.  TblPCR is coming from a second website for patient care reports.  Each is stored in a seperate table in access.  The common field is IncNum.  The CAD data comes down with an incident number attached.  As the EMT completes the report he also enters the incident number as eIncNum in the tblPCR table.  I want to compare the two incident numbers.  If the incident number is in tblPCR matches the incident number in tblCAD then I know the report was completed by the EMS provider.  

I do not know if it is possible but I would also like to see the Incident Numbers in tblCAD that have no match in tblPCR.  I want to see both completed reports and uncompleted reports.  

the CAD incident number comes down as 2012-85.  The employee may or may not use the 2012.  There report may show an incident number of 85 or 2012-85.  So I am looking for this to pick up either match.  

Any help would be greatly appreciated,

Lisa
0
Comment
Question by:AnneYourPointIs
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:colonytire
ID: 37749045
You should be able to use Right(tablefield, 3) in your query so it uses the last 3 characters ie your example "-85" would be returned.  Info on the query can be found here:
http://office.microsoft.com/en-us/access-help/compare-two-tables-and-find-records-without-matches-HA010205132.aspx
0
 

Author Comment

by:AnneYourPointIs
ID: 37749159
I have tried this and it is not working for me.  It produces a query full of bad data.  The information in tblCAD does not match the information in tblPCR.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37749506
AnneYourPointIs,
<see the Incident Numbers in tblCAD that have no match in tblPCR.>

Try the "unmatched" query wizard in Access:
Create-->Query Wizard-->Find Unmatched....
0
 
LVL 5

Expert Comment

by:DoveTails
ID: 37749827
I obtained a query similar to the following from the EE site in the past.
I modified it to match your table names (and included an ID field):
Data in tblCAD that have no records in tblPCR:
 
SELECT tblCAD.ID, tblCAD.incnum
FROM tblCAD
LEFT JOIN tblPCR ON tblCAD.incnum = tblPCR.eIncNum
WHERE (tblPCR.eIncNum) Is Null;
================================
adding the NOT keyword shows data in tblCAD with matching records in tblPCR

SELECT tblCAD.ID, tblCAD.incnum
FROM tblCAD
LEFT JOIN tblPCR ON tblCAD.incnum = tblPCR.eIncNum
WHERE (tblPCR.eIncNum) Is Not Null;
===================================

To remove the year from your Inc number, you may have to build a query to Trim that value away.  Here's an example:

WithoutYear: Trim(Right([incNum],Len([IncNum])-InStr([IncNum],"-")))
0
 

Author Comment

by:AnneYourPointIs
ID: 37752262
I created two queries and applied your suggestion of Trim(right(.  I applied the two new queries to your suggestion above replacing the queries for the tables.  I get a data type mismatch.  It works fine for the tables but trimming the incident numbers down so they match on both sides and then running the query is where I get the error.  Here is what I have:

SELECT qryCADTrimIncNum.cadID, qryCADTrimIncNum.NewCADIncNum, qryCADTrimIncNum.Unit, qryPCRTrimIncNum.eCompany, qryCADTrimIncNum.IncDate
FROM qryCADTrimIncNum LEFT JOIN qryPCRTrimIncNum ON qryCADTrimIncNum.NewCADIncNum = qryPCRTrimIncNum.NewPCRincNum
WHERE (((qryPCRTrimIncNum.NewPCRincNum) Is Null));
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 5

Accepted Solution

by:
DoveTails earned 500 total points
ID: 37753315
AnneYourPointIs,
I see what you're talking about.  I usually try to use the JOIN syntax as opposed to the NOT IN syntax because it's more efficienct. However, in this case, it may be worth considering using the NOT IN.

Here's an example (directly on the tables and by-passing the underlying queries)

SELECT * FROM tblCAD
WHERE Trim(Right([incNum],Len([IncNum])-InStr([IncNum],"-"))) NOT IN (SELECT Trim(Right([eIncNum],Len([eIncNum])-InStr([eIncNum],"-"))) FROM tblPCR);
0
 
LVL 5

Expert Comment

by:DoveTails
ID: 37753403
An additional point ....   I'm assuming your 'mismatch' error message is due to data existing in one table and not in the other, yet that's what you hoping to accomplish.  So using different syntax seems to be the way to go.

 Someone else here on EE with a greater understanding may be able to clarify, but that's the only reason I can see for the error.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37753850
^
correct.

AnneYourPointIs,

Perhaps if you posted a sample of the database that exhibits this issue, it would be easier...
0
 

Author Comment

by:AnneYourPointIs
ID: 37797849
Ok Dove,

You have put me on the right track, and it appears that the below query is offering the data that I was looking for.  Thanks for your help!

SELECT tblCAD1.CADID, tblCAD1.IncDate, tblCAD1.CADIncNum, tblPCR1.ePCRNum, tblCAD1.IncAddress, tblCAD1.Unit, tblPCR1.eCompany, tblCAD1.Response, tblCAD1.PSAP, tblCAD1.Disp, tblCAD1.Enr, tblCAD1.Arr, tblCAD1.EnrHosp, tblCAD1.ArrHosp, tblCAD1.Clear
FROM tblCAD1 LEFT JOIN tblPCR1 ON tblCAD1.CADIncNum = tblPCR1.PCRIncNum
WHERE (((tblCAD1.IncDate)>=#2/1/2012#) AND (Not (tblCAD1.ArrHosp) Is Null));
0
 
LVL 5

Expert Comment

by:DoveTails
ID: 37797939
Excellent,

Cheers
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now