Popup Calendar bypasses Validation

matthewnsarah07

Registered User.
Local time
Yesterday, 21:18
Joined
Feb 19, 2008
Messages
192
Until now I have used this piece of validation code on my staff holiday from to validate the [Date From] field

Code:
Private Sub Date_From_BeforeUpdate(Cancel As Integer)
If DateDiff("d", Date, Date_From) > 400 Then
  MsgBox "'Date From' Cannot Be Greater Than One Year In The Future"
  Cancel = True
End If
End Sub

I have now added a popup form type calendar to fill in this field, this uses an On Click event:

Code:
=CalendarFor([Date From],"Select Date From")

The problem I have is that the validation code appears to be bypassed by selecting a date from the calendar - how can I get the validation to work regardless of whether a date is typed or entered by calendar?

Thanks for your help
 
Tony's calendar hack is probably a better choice than one of the ActiveX calendars you're probably using, as ActiveX controls tend to have problems when moving between various versions of Access, but for future reference, here's the reason that your validation code stopped running.

The BeforeUpdate and AfterUpdate of a control such as a textbox are executed after data is physically entered, i.e. typed into the control. If, on the other hand, data is entered into the control thru code, such as when you assign a value to Date_From based on the popup calendar, these events do not fire! So if you enter data thru code, you have to explicitly call the event, placing the code immediately after assigning the value to the textbox:

=CalendarFor([Date From],"Select Date From")
Call Date_From_BeforeUpdate(0)

Since the BeforeUpdate event requires an argument, in this case Cancel as Integer, you add the (0). If the event being called (such as an AfterUpdate event) doesn't require an argument, you would omit this part.
 
Never heard a word to that effect, Tony, and can't imagine, after working for all these years, how it could be a problem! You know how rumors get started/spread on the internet! Being disabled and pretty much stuck behind this keyboard 16 hours a day, I troll on 4 or 5 Access forums regularly, and I see so much absolute garbage posted! And all the people posting this stuff sound so reasonable and knowledgeable! Then somebody else repeats its it then somebody else!
 
Thanks all for your assistance with this one.

I have taken Missinglinq advice and adding the BeforeUpdate event into the code has solved the issue

Thanks again
 

Users who are viewing this thread

Back
Top Bottom