MS Access Forum / General 1 / January 2006
Now()
|
|
Thread rating:  |
Bob Wickham - 10 Jan 2006 23:35 GMT Hi,
I have some sql in a sub that finds dates in a table but only if the date is less than today
WHERE [MonthYear] < Now()
I want it to do that but to not include the current month.
For instance, a list of dates in dd/mm/yy format 1/01/05 01/02/05 etc etc 01/11/05 01/12/05 01/01/06 01/02/06
If I run this today (11/01/06) 01/01/06 is included in the result because it is less than today. But I dont want the current month included.
Any ideas.
Bob
Arno R - 10 Jan 2006 23:50 GMT > Hi, > [quoted text clipped - 22 lines] > > Bob So you want to select all dates less than the first day of the current month?
WHERE DateField < DateSerial(Year(Now), Month(Now), 1)
Arno R
Anthony England - 10 Jan 2006 23:54 GMT "Bob Wickham" <wickham_bob@yahoo.com.au> schreef in bericht news:43c444d2@dnews.tpgi.com.au...
> Hi, > [quoted text clipped - 22 lines] > > Bob So you want to select all dates less than the first day of the current month?
WHERE DateField < DateSerial(Year(Now), Month(Now), 1)
Arno R
Hey! Are you copying my answers?
Randy Harris - 11 Jan 2006 00:22 GMT > "Bob Wickham" <wickham_bob@yahoo.com.au> schreef in bericht > news:43c444d2@dnews.tpgi.com.au... [quoted text clipped - 33 lines] > > Hey! Are you copying my answers? Based on the time stamps on your posts, it was a dead heat. :-{)
Arno R - 11 Jan 2006 07:55 GMT > Hey! Are you copying my answers? Funny this is: Your Timestamp: Date: Tue, 10 Jan 2006 23:50:50 +0000 (UTC) My Timestamp: Date: Wed, 11 Jan 2006 00:50:50 +0100 So there is not even a second difference here ... both almost identical answers ...
Are you *my* evil twin ?? I am looking for one ;-)
Btw: According to Google this means for both of us: Local: Wed, Jan 11 2006 12:50 am ??
Arno R
Bob Wickham - 11 Jan 2006 00:01 GMT >>Hi, >> [quoted text clipped - 28 lines] > > Arno R Thats it, perfect.
Thankyou to you both.
Bob
Anthony England - 10 Jan 2006 23:50 GMT > Hi, > [quoted text clipped - 22 lines] > > Bob So you want to include dates less than the first of the current month?
You can use the DateSerial function:
...WHERE MyDate<DateSerial(Year(Date),Month(Date),1)
I'm assuming that your field [MonthYear] is an actual DateTime field
Wayne Morgan - 11 Jan 2006 05:14 GMT Bob,
Just a quick FYI.
I see that you got your answer and it will take care of the problem I'm about to mention also. The problem with the original code is that not only would it have included dates from earlier in the month, it would also have included dates from the current date. You were comparing to Now() which is both date and time. If the values you were comparing to this were from the current day but with an earlier time, they would have been included also. A date with no time component (i.e. time component is zero) will be treated as midnight. So any date without a time component that was from the current date would have been less than Now() unless you happened to catch it just a midnight.
The reason this happens is because VBA treats dates as a floating point number. The integer part is the date and the decimal part is the time of day. The integer portion gives the number of days since 30 Dec 1899 and the decimal portion give the time as fractions of a day (.0 = midnight, .25 = 6am, .5 = noon, etc). So, a date with no time component would have a decimal portion of zero (i.e. midnight). If all you want is the date component, I recommend using the Date() function instead of the Now() function.
 Signature Wayne Morgan MS Access MVP
> Hi, > [quoted text clipped - 22 lines] > > Bob Bob Wickham - 11 Jan 2006 21:26 GMT > Bob, > [quoted text clipped - 18 lines] > portion of zero (i.e. midnight). If all you want is the date component, I > recommend using the Date() function instead of the Now() function. Thanks Wayne,
I understand your advice but, in practise, I can't get it to work.
Should I be able to simply change Now to Date in the solution offered by Arno and Anthony
WHERE DateField < DateSerial(Year(Now), Month(Now), 1)
like
WHERE DateField < DateSerial(Year(Date), Month(Date), 1)
because doing that results in a "Too few parameters error"
Bob
Jana - 11 Jan 2006 21:55 GMT Bob:
If you're using a European version, I think you need to use ; instead of , as the parameter separator. (Not 100% sure, though!) Try WHERE DateField < DateSerial(Year(Date); Month(Date); 1)
HTH, Jana
Red - 11 Jan 2006 22:08 GMT I just wanted to note, if you are doing this in a VBA (like in a form, or in a module), then you need to play with it a bit more....
This is an example of a recent thing I did with dates in a dynamic SQL statement in a module... it has been modified so I can legally use it (Darn proprietary agreements...), and so it better fits your purposes....
'Put this under "Option Explicit" Global Const JetDateFmt = "\#mm\/dd\/yyyy\#;;;\N\u\l\l"
'This is the 'where' part of your sql WHERE MyTable.MyDate < Format$(DateSerial(Year(Date()), Month(Date), 1), JetDateFmt)
Bob Wickham - 11 Jan 2006 22:20 GMT > I just wanted to note, if you are doing this in a VBA (like in a form, > or in a module), then you need to play with it a bit more.... [quoted text clipped - 10 lines] > WHERE MyTable.MyDate < Format$(DateSerial(Year(Date()), Month(Date), > 1), JetDateFmt) Thanks Red,
Yes, it is part of a much larger VBA routine. I'll give your suggestion a try and see what happens.
Bob
David W. Fenton - 11 Jan 2006 22:36 GMT > I just wanted to note, if you are doing this in a VBA (like in a > form, or in a module), then you need to play with it a bit [quoted text clipped - 11 lines] > WHERE MyTable.MyDate < Format$(DateSerial(Year(Date()), > Month(Date), 1), JetDateFmt) That's idiotic.
Format() returns a string, so you're using implicit coercion to compare the result to a date filed.
Secondly, the US format date will give incorrect results if run on a machine with different OS-supplied default date formats (e.g., DD/MM/YYYY).
DateSerial() returns a value of type variant, but it's always going to be of a type that compares universally correctly to all stored data values, independent of local date format settings.
Thus, formatting the output of DateSerial() is undoing the good that comes from using DateSerial() in the first place.
 Signature David W. Fenton http://www.dfenton.com/ usenet at dfenton dot com http://www.dfenton.com/DFA/
Randy Harris - 11 Jan 2006 22:45 GMT > I just wanted to note, if you are doing this in a VBA (like in a form, > or in a module), then you need to play with it a bit more.... [quoted text clipped - 10 lines] > WHERE MyTable.MyDate < Format$(DateSerial(Year(Date()), Month(Date), > 1), JetDateFmt) This doesn't make much sense. He's simply comparing 2 dates, there is no reason to format it. The "correct" code is that suggested by Arno and Anthony, but using Date rather than Now, as suggested by Wayne.
 Signature Randy Harris tech at promail dot com I'm pretty sure I know everything that I can remember.
Bob Wickham - 11 Jan 2006 22:11 GMT > Bob: > [quoted text clipped - 4 lines] > HTH, > Jana Thanks Jana, but that results in a Syntax error.
The Now() function works OK but I do like to understand the alternatives.
Bob
David W. Fenton - 11 Jan 2006 22:41 GMT > I understand your advice but, in practise, I can't get it to work. > [quoted text clipped - 9 lines] > > because doing that results in a "Too few parameters error" Are you sure you're typing:
WHERE DateField < DateSerial(Year(Date()), Month(Date()), 1)
Both Date() and Now() are functions (hence the trailing parentheses). In VBA code, the () will disappear when you leave the line, but in SQL, they are necessary.
 Signature David W. Fenton http://www.dfenton.com/ usenet at dfenton dot com http://www.dfenton.com/DFA/
Bob Wickham - 12 Jan 2006 00:03 GMT > Are you sure you're typing: > [quoted text clipped - 3 lines] > parentheses). In VBA code, the () will disappear when you leave the > line, but in SQL, they are necessary. Hi David,
Well No, I wasn't typing:
" WHERE [MonthYear] < DateSerial(Year(Date()), Month(Date()), 1)" & _
because it was working with
" WHERE [MonthYear] < DateSerial(Year(Now), Month(Now), 1)" & _
so I just replaced the word Now with the word Date.
I have, just now, changed it to read Date() and it works.
Both options work and, with my limited set of data, both options give the same result. It IS part of a larger piece of VBA code but the () after Date does not disappear.
Bob
Anthony England - 12 Jan 2006 00:32 GMT >> Are you sure you're typing: >> [quoted text clipped - 23 lines] > > Bob David has correctly pointed out an oversight on my part: You do need the brackets if this is part of an sql string. It is a bit of quirk that when you use the vba editor the brackets are removed but this does not obviously apply when they are part of a string.
In other words, you write:
strSQL="SELECT Orders.* FROM Orders WHERE Orders.OrderDate<Date()"
and you must use the brackets. However, if you try to write the following:
Private Sub cmdTest_Click() Dim dte As Date Dim str As String dte = Date() str = Format(dte, "dd-mmm-yy") MsgBox str End Sub
You will find that the line dte = Date() is changed to dte = Date
Hope that is clear.
As to the difference between Date() and Now() you need to realise that DateTime fields store both a time and date portion in one field. So if I was writing this at 8 am on 12 Jan, <Now() means any date/time less than 8 am on 12 Jan <Date() means date/time less than when the date changes (at midnight) In other words <Now() would include a record dated 12-Jan @ 5:30 am whereas <Date() would not.
However, in your example, we are using theDateSerial function so both functions: DateSerial(Year(Now()), Month(Now()), 1) DateSerial(Year(Date()), Month(Date()), 1) return exactly the same result: 1 Jan 2006 @ 00:00
Anthony England - 12 Jan 2006 00:39 GMT >> Are you sure you're typing: >> [quoted text clipped - 23 lines] > > Bob David has correctly pointed out an oversight on my part: You do need the brackets if this is part of an sql string. It is a bit of quirk that when you use the vba editor the brackets are removed but this does not obviously apply when they are part of a string.
In other words, you write:
strSQL="SELECT Orders.* FROM Orders WHERE Orders.OrderDate<Date()"
and you must use the brackets. However, if you try to write the following:
Private Sub cmdTest_Click() Dim dte As Date Dim str As String dte = Date() str = Format(dte, "dd-mmm-yy") MsgBox str End Sub
You will find that the line dte = Date() is changed to dte = Date
Hope that is clear.
As to the difference between Date() and Now() you need to realise that DateTime fields store both a time and date portion in one field. So if I was writing this at 8 am on 12 Jan, <Now() means any date/time less than 8 am on 12 Jan <Date() means date/time less than when the date changes (at midnight) In other words <Now() would include a record dated 12-Jan @ 5:30 am whereas <Date() would not.
However, in your example, we are using theDateSerial function so both functions: DateSerial(Year(Now()), Month(Now()), 1) DateSerial(Year(Date()), Month(Date()), 1) return exactly the same result: 1 Jan 2006 @ 00:00
Bob Wickham - 12 Jan 2006 01:19 GMT Thanks Anthony, Brilliant explanation which has certainly made things a lot clearer for me.
Thankyou
Bob Wickham
Wayne Morgan - 12 Jan 2006 14:57 GMT Bob,
If VBA gets rid of the () after Now but not after Date (it should after both) then I would suspect that you're using the word Date for something else also, possibly a field name. Since this is a function name, it is a reserved word and can cause you problems if you use it for other things. By keeping the () after Date, VBA is distinguishing this as the function as opposed to the other use of the word Date.
 Signature Wayne Morgan MS Access MVP
>> Are you sure you're typing: >> [quoted text clipped - 23 lines] > > Bob Bob Wickham - 12 Jan 2006 21:00 GMT > Bob, > [quoted text clipped - 4 lines] > keeping the () after Date, VBA is distinguishing this as the function as > opposed to the other use of the word Date. Hello Wayne,
Yes, I agree, its strange behaviour, but I have checked all my tables and code and can't find the word Date anywhere. I've used words like TheDate or PaymentDate or tblDate.
The code with the offending Date() is reproduced below.
Private Sub Command2_Click()
Dim dbs As Database Dim rstLoans As DAO.Recordset Dim rstMissed As DAO.Recordset Dim strSql As String
Set dbs = CurrentDb
' call the function DeleteAll()
Call DeleteAll("tblMissingCommissionReport")
Set rstLoans = dbs.OpenRecordset("SELECT DISTINCT LoanNo FROM tblCommission")
Do Until rstLoans.EOF Set rstMissed = dbs.OpenRecordset("SELECT [MonthYear]" & _ " FROM tblDate" & _ " WHERE [MonthYear] < DateSerial(Year(Date()), Month(Date()), 1)" & _ " AND Format([MonthYear],'mmmm,yyyy')" & _ " Not In (SELECT Format ([PaymentDate],'mmmm,yyyy')" & _ " FROM tblCommission" & _ " WHERE LoanNo =" & rstLoans!LoanNo & ")")
Do Until rstMissed.EOF strSql = "INSERT INTO tblMissingCommissionReport ( LoanNumber, TheDate )" & _ " VALUES ( " & rstLoans!LoanNo & ", " & CLng(rstMissed!MonthYear) & " )" dbs.Execute strSql, dbFailOnError rstMissed.MoveNext Loop rstMissed.Close rstLoans.MoveNext Loop rstLoans.Close Set rstMissed = Nothing Set rstLoans = Nothing Set dbs = Nothing
' call the sub RunMcrMissingCommissionReportPrintPreview()
Call RunMcrMissingCommissionReportPrintPreview
End Sub
Lyle Fairfield - 12 Jan 2006 22:21 GMT This is a long thread. I have not read it so I may be saying something that has already been said. If so, I apologise. When there is confusion about the VBA function Date() and some other Date, one can clear up the confusion by using VBA.Date(). TTBOMK this is not shortened to VBA.Date, nor is it confused with the little blonde number in 1104 on the next floor. When I remember I use VBA.Date() instead of Date, just to be sure there is no confusion.
David W. Fenton - 12 Jan 2006 22:56 GMT > The code with the offending Date() is reproduced below. You're confusing a string with code. In your example, Date() occurs only in a string, so it's really quite irrelevant.
The real question is who "Now" without () ever works in SQL.
 Signature David W. Fenton http://www.dfenton.com/ usenet at dfenton dot com http://www.dfenton.com/DFA/
Wayne Morgan - 12 Jan 2006 23:35 GMT You have Date() within the SQL string you are using to open the recordset. A string will be taken "as is" by VBA. It doesn't do any editing on it except for doubled up delimiters (quotes) being changes to single delimiters after the assignment of the string. SQL usually wants the () on the function, so what you have is now correct and explains why it didn't work without the (). VBA is NOT processing the Date() function. However, VBA would process the Date() function in a statement such as
dteMyDate = Date
In this case, it would drop the () if you typed them in.
 Signature Wayne Morgan MS Access MVP
>> Bob, >> [quoted text clipped - 61 lines] > > End Sub
|
|
|