Formfield Date Validation

V

VBA WannaBe (101)

Hi All,

Hopefully you are able to help me! I am setting up a template in Word 2002
(using XP Professional) as a protected form.

I have numbers form fields (checkboxes, dropdowns & text fields) in my
template. I have set one of my text formfields as a date, and specified
format (properties - "dd-mmm-yyyy") and also loaded an "Entry" macro into the
next formfield to resend the user back to the date formfield if it is empty.
Brilliant so far (achieved through your posts on this site and MVP). I am
struggling with setting up a validation date macro, so when users enter in an
incorrect date it gives them an error message and puts focus back to the date
field.

When I test the date, if I enter in the "nd", "th" after the day ... it
spits the dummy and kicks out a strange date ..

I want to either:
a) prevent the user from entering in a date with the "th" "nd" etc OR
b) when a date is entered in this format a macro removes the non-numeric

is anybody able to assist me? Appreciate your help.

Regards
 
D

DA

Check your help on the isDate() function. It will most
likely do the trick for what you want.

Hope that helps,
Dennis
 
V

VBA WannaBe (101)

Hi Dennis,
I went and checked out the IsDate Function, and upon reading it, it
certainly sounds like what I want - I grabbed the code from the hlep and
dumped it into my form .. however when I run it, my month is represented by
0000 ... Im sure Im doing something too blonde for words, can you have a look
at my macro and spot the error? I changed the properties on my date textform
field so it would only display numeric (eg. dd-mm-yy) .. but that did not do
the trick either.
Thanx, Tania :)

Sub ValidateDate2()
Dim MyDate, YourDate, NoDate, MyCheck
With ActiveDocument.FormFields("bkStartDate").Range.Fields(1).Result
MyDate = "12 February 1969": YourDate = #2/12/1969#: NoDate = "Hello"
MyCheck = IsDate(MyDate) ' Returns True.
MyCheck = IsDate(YourDate) ' Returns True.
MyCheck = IsDate(NoDate) ' Returns False.
If MyDate = False Then
Application.OnTime When:=Now + TimeValue("00:00:01"),
Name:="BacktobkStartDate"
MsgBox "You need to enter a valid date", vbInformation
End If
End With
End Sub
 
D

DA

Hi Tania

For a start, lets concentrate on your code that you
posted.

At no stage are you checking the value of the actual form
field that's named "bkStartDate". If you want to do field
validation, you must set up your field that follows
bkStarDate with an Entry Macro event (under field
properties). This macro should look something similar to
this:

'-----------------
Sub ValidateDate2()
If IsDate(ActiveDocument.FormFields _
("bkStartDate").Result) = False Then
MsgBox "Invalid date"
ActiveDocument.FormFields("bkStartDate").Select
End If
End Sub
'----------------

You've also set up your dateTime format incorrectly. By
stating "dd-mm-yy" you are actually putting a minute
value into the middle of the date. Your format should
be "dd-MM-yy".

Apart from all of the above, by setting the properties of
your form field to a date field, Word will automatically
do a validity check on the value, so none of the above
would be needed.

Hope that helps,
Dennis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top