Date Diff in Forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm working on a Word form and cannot get my text form field to calculate. I know how to use the datediff function but I don't know which language to use in my macro. I have a text field named "Date of Birth" and a text form field named "Date of Note". I want the "Day of Life" form field to give me the difference between the date of birth and the date of note but I don't know how. I also want a text form field named "Day of Note" to automatically insert the "day" of the week depending on the date of the note which is not always today. I may need to type a note from yesterday or the day before. Help!!!!
 
Hi Maryanne,

For a Word document containing fields coded to do this
kind of thing, and much more with dates, go to:
http://www.wopr.com/cgi-bin/w3t/showflat.pl?
Cat=&Board=wrd&Number=249902
(url all one line)
where you can download a Word file with examples.

Cheers
-----Original Message-----
I'm working on a Word form and cannot get my text form
field to calculate. I know how to use the datediff
function but I don't know which language to use in my
macro. I have a text field named "Date of Birth" and a
text form field named "Date of Note". I want the "Day of
Life" form field to give me the difference between the
date of birth and the date of note but I don't know how. I
also want a text form field named "Day of Note" to
automatically insert the "day" of the week depending on
the date of the note which is not always today. I may
need to type a note from yesterday or the day before.
Help!!!!
 
Hi =?Utf-8?B?TWFyaWFubmU=?=,
I'm working on a Word form and cannot get my text form field to calculate. I
know how to use the datediff function but I don't know which language to use in my
macro. I have a text field named "Date of Birth" and a text form field named "Date
of Note". I want the "Day of Life" form field to give me the difference between
the date of birth and the date of note but I don't know how. I also want a text
form field named "Day of Note" to automatically insert the "day" of the week
depending on the date of the note which is not always today.In the form fields' OPTIONS for these two dates you'd want to select the macro
that does the calculation from the "On Exit" list.

Word macros use VBA language, and DateDiff would be the same here as in other
Office apps or VB. You'd want to put the macro in a project, either that of the
template you use to create multiple form documents, or that of a single form
document you'll be re-using.

To get the information from the form fields for the calculation, you'd do
something like this:

Sub CalcDateDiff()
Dim doc As Word.Document, dt1 As Date, dt2 As Date

Set doc = ActiveDocument
If IsDate(doc.FormFields("Text1").Result) Then
dt1 = CDate(doc.FormFields("Text1").Result)
Else
MsgBox ("Enter a valid date in the first box")
End If
If IsDate(doc.FormFields("Text2").Result) Then
dt2 = CDate(doc.FormFields("Text2").Result)
Else
MsgBox ("Enter a valid date in the second box")
End If
doc.FormFields("Text3").Result = _
DateDiff("d", dt2, dt1)
End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or reply in
the newsgroup and not by e-mail :-)
 
Thank you. I got it to work and calculate the difference, but now how do I get my "day" form field to put in the day of the week according to the date of my note field which is not always going to be today?
 
Hi =?Utf-8?B?TWFyaWFubmU=?=,
how do I get my "day" form field to put in the day of the week according to the
date of my note field which is not always going to be today?Here, again, you need a macro.

The Format() function can give you the day of the week for a specific date. If,
for example, another form field contains a date you can get the day of the week
using this formula:
Format(CDate(ActiveDocument.Formfields("Datefield").Result), "dddd")

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :-)
 
I couldn't get the Format function to work. Do I need to start a new macro or can I add it at the bottom of the macro that calculates the difference in the dates. Help.
 
Hi =?Utf-8?B?TWFyaWFubmU=?=,
I couldn't get the Format function to work. Do I need to start a new macro or can I add it at the bottom of the macro that calculates the difference in the dates.
It can be in the same macro, if it should be executed at the same time.

So, HOW is it not working? Are you getting an error message, or what?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
 
I get an error message saying Compile error: Expected:=. I put it in the same macro as the last line.
 
Hi =?Utf-8?B?TWFyaWFubmU=?=,
I get an error message saying Compile error: Expected:=. I put it in the same macro as the last line.
When I look back at my post, I see there's a missing ) at the very end of the
code line. Have you added that?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
 
I did enter an extra parentheses after "dddd" but I still get an error message: Compile error: Syntax error. Can you tell me the exact codes I need to use in the order I need to use them to get the dates to calculate and add the day of my note?
 
Hi =?Utf-8?B?TWFyaWFubmU=?=,
I did enter an extra parentheses after "dddd" but I still get an error message: Compile error: Syntax error. Can you tell me the exact codes I need to use in the order I need to use them to get the dates to calculate and add the day of my note?
could you copy/paste the entire macro as you currently have it into a reply, please?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
 
This is my macro as requested.Sub CalcDateDiff(
Dim doc As Word.Document, BegDate As Date, EndDate As Dat
Set doc = ActiveDocumen
If IsDate(doc.FormFields("BegDate").Result) The
BegDate = CDate(doc.FormFields("BegDate").Result
End I
If IsDate(doc.FormFields("EndDate").Result) The
EndDate = CDate(doc.FormFields("EndDate").Result
End I
doc.FormFields("WholeWeeks").Result =
DateDiff("d", BegDate, EndDate
Format(CDate(ActiveDocument.FormFields("DateField").Result), "dddd")
End Su
 
Hi =?Utf-8?B?TWFyaWFubmU=?=,

Thank you, I understand the error message, now. There was a
mis-communication how you need to use the Format() function I gave you.
You're not *assigning* it to anything. It would need, for example:

ActiveDocument.FormFields("DateField").Result = Format(etc...)

Note that I don't know if your form contains a form field named
DateField, if not, you do need to change that on both sides of the
equation. Or, if it's picking up the date from one field and writing it
to a different one, you need to use those field names.
This is my macro as requested.Sub CalcDateDiff()
Dim doc As Word.Document, BegDate As Date, EndDate As Date
Set doc = ActiveDocument
If IsDate(doc.FormFields("BegDate").Result) Then
BegDate = CDate(doc.FormFields("BegDate").Result)
End If
If IsDate(doc.FormFields("EndDate").Result) Then
EndDate = CDate(doc.FormFields("EndDate").Result)
End If
doc.FormFields("WholeWeeks").Result = _
DateDiff("d", BegDate, EndDate)
Format(CDate(ActiveDocument.FormFields("DateField").Result), "dddd"))
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :-)
 
Back
Top