PSIUnit
asked on
Verify Date = First or Last day of month
I have 2 Formatted Short Date Fields. Start Date and End Date. The Users can pick any Year or any Month but the Start Day must be the first day of the Month and the End Day must be the Last day of the Month.
And the End Date has to be after the start date. (I can handle this)
The Tables are SQL but the Form that the Users edit is Access. I was thinking of adding code to the Close button to verify this data before allowing them to Close. If it is not correct I will display an error message. I can handle the error message but how do I do the validations for what day of the month they entered?
And the End Date has to be after the start date. (I can handle this)
The Tables are SQL but the Form that the Users edit is Access. I was thinking of adding code to the Close button to verify this data before allowing them to Close. If it is not correct I will display an error message. I can handle the error message but how do I do the validations for what day of the month they entered?
A better design would be to have the user select a starting year and month and end year and month. Then your front-end calculates the day-of-month for starting and ending dates.
I have attached 2 functions that return the start and end dates of the selected month.
so you could either just use those as the data posted to SQLS, or do a check in the front end to see if the input matches the required date and throw an alert if not.
so you could either just use those as the data posted to SQLS, or do a check in the front end to see if the input matches the required date and throw an alert if not.
Function MonthStart(datein As Date) As Date
MonthStart = DateSerial(Year(datein), Month(datein), 1)
End Function
Function MonthEnd(datein As Date) As Date
MonthEnd = DateSerial(Year(datein), Month(datein) + 1, 1) - 1
End Function
Sub button_Close()
Dim erx As Boolean
erx = False
If datepicker1.Date <> MonthStart(datepicker1.Date) Then
MsgBox "Start date needs to be first of the month"
erx = True
End If
If datepicker2.Date <> MonthEnd(datepicker2.Date) Then
MsgBox "End date must be the last day of the month"
erx = True
End If
If erx = False Then
'process the date
Else
'return to entry for,
End If
End Sub
You can ask user enter only month and year and generate first and last day with proposed functions
If you want to validate the start/end dates in your form, then it's probably best to use the Before Update event handlers of the corresponding controls, like this:
Option Explicit
Option Compare Database
Private Sub txtEndDate_BeforeUpdate(pintCancel As Integer)
If DatePart("d", txtEndDate + 1) <> 1 Then
MsgBox "End date must be last day of the month"
pintCancel = True
End If
End Sub
Private Sub txtStartDate_BeforeUpdate(pintCancel As Integer)
If DatePart("d", txtStartDate) <> 1 Then
MsgBox "Start date must be first day of the month"
pintCancel = True
End If
End Sub
And here's one I made earlier! :-)
Q26887751.mdb
Q26887751.mdb
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
WOW!!! That is a whole Lot of Information! Thank you! Let me sift thru it today and figure out which ones work best for this.. I had 2 separate fields originally for the date but the users did not like it that way they want to enter a date even though they have no control over the day ..(go figure).. so i went with them enterring the date and me fixing it... i will let you know how it works out.. Thank you very much ...
@cactus_data had never thought of using day=0 in a dateserial to get the last day of prev month but certainly works.
It does. DateSerial is a great function.
/gustav
/gustav
ASKER
That was Great! Thank all of you for your help.
You are welcome!
/gustav
/gustav
http://msdn.microsoft.com/en-us/library/system.datetime.daysinmonth.aspx
Or you can do the following in VBA
Function dhDaysInMonth(Optional dtmDate As Date = 0) As Integer
' Return the number of days in the specified month.
If dtmDate = 0 Then
' Did the caller pass in a date? If not, use the current date.
dtmDate = Date
End If
dhDaysInMonth = DateSerial(Year(dtmDate), _
Month(dtmDate) + 1, 1) - _
DateSerial(Year(dtmDate), Month(dtmDate), 1)
End Function