Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / September 2005

Tip: Looking for answers? Try searching our database.

Compare beginning & ending date

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex - 09 Sep 2005 21:00 GMT
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.
William Benson - 10 Sep 2005 04:05 GMT
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

>I have cmdbegin and cmdend controls in my form.  In a button on the same
>form
[quoted text clipped - 19 lines]
>
> Thanks for your help.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.