Compare beginning & ending date

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have cmdbegin and cmdend controls in my form. In a button on the same form
I need to write code that will check to make sure that the difference between
cmdbegin and cmdend is not greater then 6 months. The code below seems to be
working, but I'm not sure why >5 works instead of >6. I think it probably
has something to do with the fact that my dates will always be the first day
of the month, like 12/1/04 & 5/1/05. Should I be writing this DateDiff
function different?

Code that works:
'check to make sure no more than 6 months of data is requested.
If DateDiff("m", cmdbegin, cmdend) > 5 Then
MsgBox "This report can only show 6 months of data...."
Exit Sub
End If

Also, I'm trying to check to make sure cmdbegin is not a date after cmdend
but I'm having no luck with that at all.

Thanks for your help.
 
Hi, Are you referring to captions on command buttons? I cannot get DateDiff
to accept the commandbuttons as arguments unless I actually include the
..Caption property in the arguments: MsgBox DateDiff("m", CmdBegin.Caption,
CmdEnd.Caption). I have set up a form with three buttons, one named
CmdBegin, another with CmdEnd, and a third which calculates the DateDiff in
two different ways. I enter dates as the captions for CmdBegin and CmdEnd.
This is the click event of that third button: The results are the same every
which way I test it!

Private Sub Command2_Click()
Dim dtBegin As Date , dtEnd As Date, MyDtDiff As Long
dtBegin = CDate(CmdBegin.Caption)
dtEnd = CDate(CmdEnd.Caption)
MyDtDiff = 12 * (Year(dtEnd) - Year(dtBegin)) + Month(dtEnd) -
Month(dtBegin)
MsgBox "Using MY Calculation: " & MyDtDiff & vbcrlf & "Using VBA DateDiff: "
& _
DateDiff("m", CmdBegin.Caption, CmdEnd.Caption)
End Sub

Try using the caption property directly, or even better,
CDate(CmdBegin.Caption)
and
CDate(CmdEnd.Caption)

Bill
 
Back
Top