Results 1 to 3 of 3

Thread: [RESOLVED] Date format/calculation issues with Word 2003 userform

  1. #1

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Resolved [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:
    1. 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:
    1. Dim strValidity As Date
    2.         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.
    Last edited by New2vba; Dec 18th, 2005 at 09:12 AM.

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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)

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Unhappy 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:
    1. txtDate.Value = Format(CDate(Date), "dd/mm/yy")

    After change event (incase the user wishes to enter future/past date):

    VB Code:
    1. txtDate.Value = Format(CDate(txtDate.Text), "dd/mm/yy")

    Then to produce my derived value:

    VB Code:
    1. 'CALCULATE VALID TO DATE
    2.     Dim strValidity As String
    3.         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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width