Solved

Query asking for an Expr1 as a PArameter

Posted on 2009-04-15
12
693 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

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

16 Experts available now in Live!

Get 1:1 Help Now