Solved

Compare Tables MS Access 2007

Posted on 2012-03-21
10
342 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

757 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

25 Experts available now in Live!

Get 1:1 Help Now