use of isdate

G

Guest

I would like to Check to see that ending month is later than beginning month.
How do I do that. because what I have is Checking to see if the ending
date is later than beginning date as follows: (where begindate and enddate
are unbound textboxes on a form now my form has 2 unbound textboxes with
beginMonth and EndMonth. Months are formated as "mmmm yyyy" )

If IsDate(BeginDate) And IsDate(EndDate) Then
If EndDate < BeginDate Then
MsgBox "The ending date must be later than the beginning date."
Exit Sub
End If
Else
MsgBox "Please use a valid date for the beginning date and the
ending date values."
End If
 
A

Allen Browne

Try using CDate() to convert the entries to actual date values:

If IsDate(BeginDate) And IsDate(EndDate) Then
If CDate(EndDate) < CDate(BeginDate) Then
MsgBox "The ending date must be later than the beginning date."
Exit Sub
End If
Else
MsgBox "Please use a valid date"
End If
 
R

Rick Brandt

JOM said:
I would like to Check to see that ending month is later than
beginning month. How do I do that. because what I have is Checking
to see if the ending
date is later than beginning date as follows: (where begindate and
enddate are unbound textboxes on a form now my form has 2 unbound
textboxes with beginMonth and EndMonth. Months are formated as "mmmm
yyyy" )

If IsDate(BeginDate) And IsDate(EndDate) Then
If EndDate < BeginDate Then
MsgBox "The ending date must be later than the beginning
date." Exit Sub
End If
Else
MsgBox "Please use a valid date for the beginning date and the
ending date values."
End If

While a text string like "March 2005" will return True when used as the
argument for IsDate() it will not be evaluated as a date in your If EndDate
< BeginDate line. That will evaluate them as text strings and the "less
than" will be alphbetical, not chronological.

Try wrapping each in CDate()...

If IsDate(BeginDate) And IsDate(EndDate) Then
If CDate(EndDate) < CDate(BeginDate) Then
MsgBox "The ending date must be later than the beginning date."
Exit Sub
End If
Else
MsgBox "Please use a valid date for the beginning date and the ending
date values."
End If
 
G

Guest

Thanks for your response, When I tried to open the report based on those
months (from July to September) it opened the report for may june july
september and october...

This is how my report is based..
my table has the following: Rcvddate e.g., 10/10/2005

so my report is grouped as follows:
RcvdMonths:Format$([RcvdDate],"mmmm yyyy",0,0) e.g., October 2005

so what could have I done wrong
 
G

Guest

Thanks for your response, When I tried to open the report based on those
months (from July to September) it opened the report for may june july
september and october...

This is how my report is based..
my table has the following: Rcvddate e.g., 10/10/2005

so my report is grouped as follows:
RcvdMonths:Format$([RcvdDate],"mmmm yyyy",0,0) e.g., October 2005

so what could have I done wrong
 
R

Rick Brandt

JOM said:
Thanks for your response, When I tried to open the report based on
those months (from July to September) it opened the report for may
june july september and october...

This is how my report is based..
my table has the following: Rcvddate e.g., 10/10/2005

so my report is grouped as follows:
RcvdMonths:Format$([RcvdDate],"mmmm yyyy",0,0) e.g., October 2005

so what could have I done wrong

How are you passing the criteria on your form to the report?
 
G

Guest

through a query, the query's sql is as follows

SELECT Tasks, Format$([RcvdDate],"mmmm yyyy",0,0) AS RcvdMonths
FROM tblEmployee
GROUP BY Tasks, Format$([RcvdDate],"mmmm yyyy",0,0)
HAVING (((Tasks) Is Not Null) AND ((Format$([RcvdDate],"mmmm yyyy",0,0))
Between [Forms]![rptmonthlyYield]![BeginMonth] And
[Forms]![rptmonthlyYield]![EndMonth]))
ORDER BY Tasks;


Rick Brandt said:
JOM said:
Thanks for your response, When I tried to open the report based on
those months (from July to September) it opened the report for may
june july september and october...

This is how my report is based..
my table has the following: Rcvddate e.g., 10/10/2005

so my report is grouped as follows:
RcvdMonths:Format$([RcvdDate],"mmmm yyyy",0,0) e.g., October 2005

so what could have I done wrong

How are you passing the criteria on your form to the report?
 
R

Rick Brandt

JOM said:
through a query, the query's sql is as follows

SELECT Tasks, Format$([RcvdDate],"mmmm yyyy",0,0) AS RcvdMonths
FROM tblEmployee
GROUP BY Tasks, Format$([RcvdDate],"mmmm yyyy",0,0)
HAVING (((Tasks) Is Not Null) AND ((Format$([RcvdDate],"mmmm
yyyy",0,0)) Between [Forms]![rptmonthlyYield]![BeginMonth] And
[Forms]![rptmonthlyYield]![EndMonth]))
ORDER BY Tasks;

Okay, there you are wrapping your dates in the format function and the output of
that is no longer a date, it is a string. If you want chronological comparisons
you need to keep the date DataType.
 
A

Allen Browne

Yes, I thought you might be doing somehing like that.

The simplest workaround would be to let the user specify full dates, e.g.
July 1 2005 to October 31 2005. You could then use the code in this link:
http://allenbrowne.com/casu-08.html

If you don't want to do that, and you want to include October, you will need
to ask for less than November so all the October dates are included. The
most efficient approach would be to convert those text boxes into true
dates, so that Access can use any index on your date field and perform a
numeric date comparison. You will then need to format the date into the
correctly delimited format in the WhereCondition string:

Dim strWhere As String
Dim bCancel As Boolean
Const conJetDate = "\#mm\/dd\/yyyy\#"

If IsDate(BeginDate) And IsDate(EndDate) Then
If CDate(EndDate) < CDate(BeginDate) Then
MsgBox "The ending date must be later than the beginning date."
bCancel = True
End If
Else
MsgBox "Please use a valid date"
bCancel = True
End If
If Not bCancel Then
strWhere = "([MyDateField] >= " & _
Format(CDate(Me.BeginDate, conJetDate) & _
") AND ([MyDateField] < " & _
Format(CDate(Me.BeginDate, conJetDate) & ")"
'Debug.Print strWhere
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

JOM said:
Thanks for your response, When I tried to open the report based on those
months (from July to September) it opened the report for may june july
september and october...

This is how my report is based..
my table has the following: Rcvddate e.g., 10/10/2005

so my report is grouped as follows:
RcvdMonths:Format$([RcvdDate],"mmmm yyyy",0,0) e.g., October 2005

so what could have I done wrong

Allen Browne said:
Try using CDate() to convert the entries to actual date values:

If IsDate(BeginDate) And IsDate(EndDate) Then
If CDate(EndDate) < CDate(BeginDate) Then
MsgBox "The ending date must be later than the beginning
date."
Exit Sub
End If
Else
MsgBox "Please use a valid date"
End If
 
G

Guest

Thanks for the advice....

Rick Brandt said:
JOM said:
through a query, the query's sql is as follows

SELECT Tasks, Format$([RcvdDate],"mmmm yyyy",0,0) AS RcvdMonths
FROM tblEmployee
GROUP BY Tasks, Format$([RcvdDate],"mmmm yyyy",0,0)
HAVING (((Tasks) Is Not Null) AND ((Format$([RcvdDate],"mmmm
yyyy",0,0)) Between [Forms]![rptmonthlyYield]![BeginMonth] And
[Forms]![rptmonthlyYield]![EndMonth]))
ORDER BY Tasks;

Okay, there you are wrapping your dates in the format function and the output of
that is no longer a date, it is a string. If you want chronological comparisons
you need to keep the date DataType.
 
G

Guest

Thanks for the advice!

Allen Browne said:
Yes, I thought you might be doing somehing like that.

The simplest workaround would be to let the user specify full dates, e.g.
July 1 2005 to October 31 2005. You could then use the code in this link:
http://allenbrowne.com/casu-08.html

If you don't want to do that, and you want to include October, you will need
to ask for less than November so all the October dates are included. The
most efficient approach would be to convert those text boxes into true
dates, so that Access can use any index on your date field and perform a
numeric date comparison. You will then need to format the date into the
correctly delimited format in the WhereCondition string:

Dim strWhere As String
Dim bCancel As Boolean
Const conJetDate = "\#mm\/dd\/yyyy\#"

If IsDate(BeginDate) And IsDate(EndDate) Then
If CDate(EndDate) < CDate(BeginDate) Then
MsgBox "The ending date must be later than the beginning date."
bCancel = True
End If
Else
MsgBox "Please use a valid date"
bCancel = True
End If
If Not bCancel Then
strWhere = "([MyDateField] >= " & _
Format(CDate(Me.BeginDate, conJetDate) & _
") AND ([MyDateField] < " & _
Format(CDate(Me.BeginDate, conJetDate) & ")"
'Debug.Print strWhere
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

JOM said:
Thanks for your response, When I tried to open the report based on those
months (from July to September) it opened the report for may june july
september and october...

This is how my report is based..
my table has the following: Rcvddate e.g., 10/10/2005

so my report is grouped as follows:
RcvdMonths:Format$([RcvdDate],"mmmm yyyy",0,0) e.g., October 2005

so what could have I done wrong

Allen Browne said:
Try using CDate() to convert the entries to actual date values:

If IsDate(BeginDate) And IsDate(EndDate) Then
If CDate(EndDate) < CDate(BeginDate) Then
MsgBox "The ending date must be later than the beginning
date."
Exit Sub
End If
Else
MsgBox "Please use a valid date"
End If

I would like to Check to see that ending month is later than beginning
month.
How do I do that. because what I have is Checking to see if the
ending
date is later than beginning date as follows: (where begindate and
enddate
are unbound textboxes on a form now my form has 2 unbound textboxes
with
beginMonth and EndMonth. Months are formated as "mmmm yyyy" )

If IsDate(BeginDate) And IsDate(EndDate) Then
If EndDate < BeginDate Then
MsgBox "The ending date must be later than the beginning
date."
Exit Sub
End If
Else
MsgBox "Please use a valid date for the beginning date and the
ending date values."
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top