Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date verification using calendar selector 2

Status
Not open for further replies.

BabyPowder2u

Programmer
May 4, 2005
87
0
0
US
I am using ocxCalendar (MSCAL.Calendar.7) to pick dates. I would like to verify the date selected is >= current date.

Can someone help me accomplish this?

Thanks,

T
 
Morning BabyPowder2U,

I think all calendar controls default to the current date, but putting something like YourCalendarName.Value = Date() in your Form_Load sub will also do it. Of course this assumes you computer's system is set with the correct date.

Hope this helps!

The Missinglinq

There's ALWAYS more than one way to skin a cat!
 
Hi missingling,

The calendar selector does start at the current date, but it doesn't prevent someone from choosing a day < current date. This is what I want to prevent.

Thanks,
T
 
Sorry, haven't had my morning coffee! In the After_Update of your calendar compare the date picked and popup a message box if it before today:

Private Sub YourCalendarName_AfterUpdate()
Dim Msg As String
Dim Response As String

If YourCalendarName.Value < Date Then

Msg = "YOU MUST PICK A TODAYS DATE OR A DATE IN THE FUTURE "

MsgBox Msg, vbRetryCancel + vbExclamation, "INCORRECT DATE!"

End If

End Sub

The Missinglinq

There's ALWAYS more than one way to skin a cat!
 
Thanks Missingling,

I will check this out. Btw, you "Dim Response As String".
I don't see "Response" anywhere else. Can you tell me what that is for?

Thanks,
T
 
The calendar doesn't have an afterUpdate event, so I tried:

Private Sub ocxCalendar_Updated(Code As Integer)
Dim Msg As String
Dim Response As String
Dim ChkDate As Date
Dim NowDate As Date

'ChkDate = Date

ChkDate = Now
Debug.Print "ChkDate = " & ChkDate
Debug.Print "ocxCalendar ='" & ocxCalendar & "'"

If ocxCalendar.Value < ChkDate Then
Debug.Print "ocxCalendar.Value ='" & ocxCalendar.Value & "'"
Debug.Print "Now ='" & Now & "'"
Msg = "YOU MUST PICK A TODAYS DATE OR A DATE IN THE FUTURE "

MsgBox Msg, vbRetryCancel + vbExclamation, "INCORRECT DATE!"

End If
End Sub

This "one time" gave me the prints with for ocxcalendar.value & Now
now included the time string, so I changed it to "date"

since then, I have gotten "NO" print statements at all, and it has never prompted with the msgbox.

I don't understand why...
 
I have tried the following code in these fields:
cboStartDate1_Change, cboStartDate1_AfterUpdate,
ocxCalendar_Updated

Dim Msg As String
Dim Response As String
Debug.Print "In cboStartHour1_GotFocus"
Debug.Print "cboStartDate1 ='" & cboStartDate1 & "'"

If cboStartDate1 < Date Then
Debug.Print "cboStartDate1 ='" & cboStartDate1 & "'"
Debug.Print "Now ='" & Now & "'"
Msg = "YOU MUST PICK A TODAYS DATE OR A DATE IN THE FUTURE "

MsgBox Msg, vbRetryCancel + vbExclamation, "INCORRECT DATE!"

End If

none of them even displayed the debug.print lines. The only way the code works is if I place it in the "GotFocus" event of the next field. This doesn't make sense to me.
 
How about this modifcation to your code?:
Code:
Private Sub ocxCalendar_Click()
Dim Msg As String
Dim Response As String
Debug.Print "In ocxCalendar_Click"
Debug.Print "ocxCalendar ='" & ocxCalendar.Value & "'"

If ocxCalendar.Value < Date Then
    Debug.Print "ocxCalendar ='" & ocxCalendar.Value & "'"
    Debug.Print "Now ='" & Date & "'"
    Msg = "YOU MUST PICK A TODAYS DATE OR A DATE IN THE FUTURE "
              
    MsgBox Msg, vbRetryCancel + vbExclamation, "INCORRECT DATE!"

End If
End Sub
The click event fires on the Calendar.

Hope this helps

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
Thanks for the reply HarleyQuinn,

The ocxCalendar doesn't have a "on Click" event. It has:
On Updated (tried the code here w/ref to ocx - didn't work)
On Enter
On Exit
On Got Focus
On Lost Focus
Custom (don't have any idea what that is)

Still trying...

T
 
I assure you that it does.

Go into your code. At the top left drop down select the name of your calendar control (ocxCalendar). In the dropdown to the right is a list of the available events.

You will see Click in there.

There are several events in the your won't find on the events tab in the controls property box.

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
You will also find the After_Update event mentioned by Missinglinq in there too.

Hope this helps

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
I apologize, I was looking at the properties sheet and there are no "on click" or "after_Update" events there. I am very unfamiliar with using the editor pulling down events like you mentioned. I will try that and get back.

T
 
It helps to know what your doing. Placing the code in the afterUpdate (once I found it) does produce the desired message.

One problem, if the user presses re-try and returns to the calendar, if they press the same date again, it no longer fails the test and allows the field to be updated with an incorrect date.

Any ideas?

T
 
It will, because the calendar isn't updating therefore the After_Update event won't fire. Could you have a go on the Click event instead??

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
A safe habit is to enforce ALL validation rules in the BeforeUpdate event procedure of the Form too.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I tried with it in just the click event, that didn't take me back to the calendar after pressing retry, it returned to the field and put the invalid date in the field.

Something similar happened with the code in both the afterUpdate and click events. It still resulted with an invalid date in the field.
 
The BeforeUpdate of the form might be an option for this form which is only doing adds. However I'm going to have an "update" form in which the old dates would be valid, but any "new entry" or change of that field to an old date would be invalid. I haven't figured out how to do this since "canceling" the calendar (after an invalid date) did not allow me to return to the original value in the field.

T
 
Each bound control has an OldValue property.
To know if the current record is a new one you may test the NewRecord property of the Form object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
That could be part of the problem right now, the cboStartDate(1-10) is an unbound field. Is there an OldValue property for an unbound field>
 
an "update" form in which the old dates would be valid
(...) the cboStartDate(1-10) is an unbound field

So, from where are the old dates coming ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top