tags:

views:

101

answers:

2

I'm having trouble creating a query.

I need to create a SQL query where ProductID = prodSelect (user choice) and EndTime = Null and StartTime < 3 hours from the current time.

This is what I have so far. Could someone help me figure it out?

Set rs = CurrentDb.OpenRecordset("SELECT TimeID " & _
    "FROM tblLunchTime " & _
    "WHERE ProductionID = prodSelect 
       AND EndTime = NULL 
       AND StartTime < (Now - 0/0/0000 3: 00: 00 AM")
+3  A: 

The condition AND EndTime = NULL always evaluates to unknown. Instead, use:

AND EndTime is NULL 

Then, you probably should tell the database how to parse the datetime string. This would work on MS Access (to require StartTime to be more than 3 hours in the past):

AND StartTime < dateadd('h',-3, now);

Per your comment, here is a full query:

SELECT TimeID 
FROM tblLunchTime 
WHERE ProductionID = 1 
AND EndTime IS NULL 
AND StartTime < DATEADD('h',-3,now)

The problem might be your prodId; I'm not sure what it's supposed to mean. You might try to use it as a variable, like

"WHERE ProductionID = " & prodId & " " & _
Andomar
Why is it negative 3?
BioXhazard
@BioXhazard: What happens when you add -3 (hours) to the current time? Do you get an answer that's 3 hours in the future or an answer that's 3 hours in the past? I suppose if there was a `datesubstract` function you could call `datesubtract(hour,3,getdate())`...
FrustratedWithFormsDesigner
There's a [McFly joke](http://www.imdb.com/title/tt0088763/) in here somewhere...
OMG Ponies
That doesn't make sense. How would a negative number increase time?
BioXhazard
@OMG Ponies: go for it!
FrustratedWithFormsDesigner
@BioXhazard: It doesn't increase time. Adding -3 hours to the current time gives you a time that was 3 hours *prior* to the current time.
FrustratedWithFormsDesigner
It still doesn't work. I just get invalid argument.
BioXhazard
Well that's probably because the solution was posted *before* you said you were using MS-Access and Andomar also specified his solution was for *SQL Server*, which does work a bit differently than Access. Now that I've retagged this as "ms-access" someone with Access knowledge might post a working solution.
FrustratedWithFormsDesigner
@BioXhazard, FrustratedWithFormsDesigner: Edited for MS Access. I thought everyone had a copy of Access on his pc anyway ;)
Andomar
Could you post the whole expression into one line for MSAccess? Mine just gives an invalid argument error.
BioXhazard
A: 

As I said in the other version of this question, I think you should calculate the date/time criterion before you concatenate. Otherwise, you're passing it off to the server to do. I would do the same even if the data store was Jet/ACE.

David-W-Fenton