Solved

Query asking for an Expr1 as a PArameter

Posted on 2009-04-15
12
698 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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
 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
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…

726 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