[RESOLVED] Date format/calculation issues with Word 2003 userform
Don't mean to take advantage of the free and much appreciated help, but I'm stuck again! :(
Can anybody help?
I have a Word 2003 userform that includes some text boxes. When the form is initialised, I insert the date into one of these boxes using the following:
VB Code:
txtDate.Value = Format(Date, "dd/mm/yy")
When the form is completed and a command button is pressed, the forms values and some derived values are inserted into the Word document using bookmarks. One of these derived values is the date + 30 days, which is what I am having a problem with.
I have used several sample pieces of code, but can't seem to get it to work - the form generates an error everytime. The latest code I have (which doesn't work) is:
VB Code:
Dim strValidity As Date
strValidity = Format$((txtDate.Value + 30), "dd-mm-yy")
I also want to set the original text box (txtDate) format to dd/mm/yy similar to that used in Excel so, for example, if the user enters a new (shorthand) date, such as "15-12", it will be interpretted as "15/12/05"
Any help would be appreciated.
Re: Date format and calculation issues
Code:
txtDate.text=format(cdate(txtDate.Text)+30,"dd mmm yyyy")
The value you are pulling from the text box is text. Not a date. CDate converts it to a date. You may need to confirm that the text in the text field is actually a date otherwise it will error. (IsDate function)
Re: Date format/calculation issues with Word 2003 userform
Thanks that certainly helped me to get it working, although I had to change my derived value to a string to prevent it from appearing in the format dd/mm/yyyy. Here's what I have:
Upon form initialisation:
VB Code:
txtDate.Value = Format(CDate(Date), "dd/mm/yy")
After change event (incase the user wishes to enter future/past date):
VB Code:
txtDate.Value = Format(CDate(txtDate.Text), "dd/mm/yy")
Then to produce my derived value:
VB Code:
'CALCULATE VALID TO DATE
Dim strValidity As String
strValidity = Format(CDate(txtDate.Text) + 30, "dd/mm/yy")
I have tested using various dates and it seems to work fine - entries such as 1-2 are interpretted as 01/02/05 and it calculates the strValidity correctly.
However, I don't know if this method is the best way to achieve my objective. Incidentally, once strValidity and txtDate are inserted into the Word document they only need to be plain text.
Thanks again :)