Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query asking for an Expr1 as a PArameter

Posted on 2009-04-15
12
Medium Priority
?
704 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 600 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
 
LVL 1

Accepted Solution

by:
NicholasSmith earned 600 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 600 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 600 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

598 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