Solved

Query asking for an Expr1 as a PArameter

Posted on 2009-04-15
12
691 Views
Last Modified: 2012-05-06
Hi Guys
this is the SQl query

SELECT PatientMaster.PatientID, PatientMaster.PatientCode, DiagnosisDetails.GISTDiagnosisDate,

Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date]))) AS LastContactEvent

,


 DateDiff("d",[DiagnosisDetails].[GISTDiagnosisDate],[LastContactEvent]) AS OPSND


FROM (PatientMaster LEFT JOIN TimeLine ON PatientMaster.PatientID = TimeLine.Patient) INNER JOIN DiagnosisDetails ON PatientMaster.PatientID = DiagnosisDetails.PatientID
GROUP BY PatientMaster.PatientID, PatientMaster.PatientCode, DiagnosisDetails.GISTDiagnosisDate, DateDiff("d",[DiagnosisDetails].[GISTDiagnosisDate],[LastContactEvent]), PatientMaster.Deceased
HAVING (((PatientMaster.Deceased)=No))
ORDER BY PatientMaster.PatientID, Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])));


When i run this query is asking me for [LastContactEvent] as a Parameter, i hit enter and runs fine, i do not know why
i am using [LastContactEvent] as the max of 3 days i call a function that i create and return the mas of 3 dates.
in OPSND i use [LastContactEvent] to get the difference in days between [lastcontactevent] and another datefield

Any idea


best regards
0
Comment
Question by:titorober23
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 7

Expert Comment

by:cmrobertson
ID: 24148843
have you declared lastcontactevent and set it's initial value
0
 

Author Comment

by:titorober23
ID: 24148881
LastContactEvent is not a variable is just the name of a field

Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date]))) AS LastContactEvent

and i use it in another field
DateDiff("d",[DiagnosisDetails].[GISTDiagnosisDate],[LastContactEvent]) AS OPSND

when i run the query i get the msgbox Enter Parameter Value
[LastContactEvent]
0
 
LVL 1

Expert Comment

by:NicholasSmith
ID: 24149069
you have LastContactEvent as a label for a field within your query. when your grouping it doesnt know the data field is part of the recordset. to get around this, use the full field formula and not the name of the field
0
 
LVL 7

Expert Comment

by:cmrobertson
ID: 24149095
then it needs to be fully qualified with the tablename as well
0
 
LVL 1

Assisted Solution

by:StuartMc77
StuartMc77 earned 150 total points
ID: 24149156
try changing the name of the LastContactEvent parameter in the datediff funtion to [LastContactEvent2] and see if the message box asks for lastcontactevent or lastcontactevent 2.

If it asks for lastcontactevent2 then the problem is that you need to set a variable called LastContactEvent to the result of the MAX(IIF(...)) statement before you can use it in the datediff function.
0
 

Author Comment

by:titorober23
ID: 24149550
how do i set that variable, should i set it global and assign a value in the function, or should i call the maxdate in the datediff function
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 1

Accepted Solution

by:
NicholasSmith earned 150 total points
ID: 24149731
your query needs to read:

SELECT PatientMaster.PatientID, PatientMaster.PatientCode, DiagnosisDetails.GISTDiagnosisDate, Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date]))) AS LastContactEvent, DateDiff("d",[DiagnosisDetails].[GISTDiagnosisDate],Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date]))) ) AS OPSND FROM (PatientMaster LEFT JOIN TimeLine ON PatientMaster.PatientID = TimeLine.Patient) INNER JOIN DiagnosisDetails ON PatientMaster.PatientID = DiagnosisDetails.PatientID
GROUP BY PatientMaster.PatientID, PatientMaster.PatientCode, DiagnosisDetails.GISTDiagnosisDate, DateDiff("d",[DiagnosisDetails].[GISTDiagnosisDate],[LastContactEvent]), PatientMaster.Deceased
HAVING (((PatientMaster.Deceased)=No))
ORDER BY PatientMaster.PatientID, Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])));
0
 
LVL 1

Expert Comment

by:StuartMc77
ID: 24151156
What Nicholas has said is correct and should work.  The problem here is that the refernce to column LastContactEvent in the datediff function is not able to see use the "on the fly" column that was just made.  What Nicholas has done is simply copy the same formlua into the datediff function.  This works but essentially is doubling up the processing time by doing the same calculation twice.

I believe if you added this before the SELECT part of the statement you could use it through out your query:

SET @LastContactEvent = Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])))

SELECT ... etc

but if that doesn't work, I'd be happy to find the correct way to do it or I'm sure Nicholas' version will work for you too.
0
 
LVL 1

Assisted Solution

by:StuartMc77
StuartMc77 earned 150 total points
ID: 24158440
Try this:

DECLARE LastContactEvent Datetime

SET @LastContactEvent = Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])))

SELECT      PatientMaster.PatientID,
            PatientMaster.PatientCode,
            DiagnosisDetails.GISTDiagnosisDate,
            @LastContactEvent AS LastContactEvent,
            DateDiff("d",[DiagnosisDetails].[GISTDiagnosisDate],@LastContactEvent) AS OPSND

FROM      (PatientMaster LEFT JOIN TimeLine ON PatientMaster.PatientID = TimeLine.Patient)
            INNER JOIN DiagnosisDetails ON PatientMaster.PatientID = DiagnosisDetails.PatientID

GROUP BY      PatientMaster.PatientID,
                  PatientMaster.PatientCode,
                  DiagnosisDetails.GISTDiagnosisDate,
                  DateDiff("d",[DiagnosisDetails].[GISTDiagnosisDate],@LastContactEvent),
                  PatientMaster.Deceased

HAVING (((PatientMaster.Deceased)=No))

ORDER BY      PatientMaster.PatientID,
                  Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])));
0
 
LVL 1

Expert Comment

by:StuartMc77
ID: 24158456
um... but put and @ before LastContactEvent on the DECLARE line lol.
0
 
LVL 1

Assisted Solution

by:NicholasSmith
NicholasSmith earned 150 total points
ID: 24158530
Instead of having:

 ORDER BY PatientMaster.PatientID,
                  Max(IIf(maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])=#1/1/1000#,"",maxdate([PatientMaster].[LastUpdateDate],[PatientMaster].[LastUpdateDate],[TimeLine].[Start Date])));

wouldnt this work if you wrote:

 ORDER BY PatientMaster.PatientID, @LastContactEvent
0
 
LVL 1

Expert Comment

by:StuartMc77
ID: 24166346
HA... lol.. yes I missed that... Thanks Nicholas
0

Featured Post

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.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

707 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

19 Experts available now in Live!

Get 1:1 Help Now