Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a text field in a dialog box. I would like user to enter a date in it but to only use the "-" and not "/" When user tabs out of the field, what code is needed to alert them to use the hypen? The reason for this that other code adds this date as part of the document name. Because "/" isn't a valid character for a file name, I want them to use the hyphen.

    Previously I tried the following code to replace slashes with hyphens and validate the date field, but users at another location received Compile errors "Can't find project or library." This works great on my computer but not theirs. Using Word 2003.

    ' Validates that a correct date was entered. Converts 1/1/2010 to 1-1-2010
    ' in order for the document's file name to be set (can't contain "/")
    Public Sub txtCycleDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With txtCycleDate
    If IsDate(.Text) Then
    '- format it
    .Text = Format$(.Text, "mm-dd-yyyy")
    Else
    '/ error
    msgbox "The value you entered is not a date."
    .SelStart = 0
    .SelLength = Len(.Text)
    Cancel = True
    End If
    End With
    End Sub

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi charlie6067,

    You can ensure the user inputs only numbers, with a '/' or '-' with code like:
    Code:
    Private Sub txtCycleDate_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not (KeyAscii > 47 And KeyAscii < 59) Then
      If KeyAscii = 45 Then Exit Sub
      If KeyAscii = 47 Then
        Beep
        KeyAscii = 45
      Else
        Beep
        KeyAscii = 0
      End If
    End If
    End Sub
    If the user inputs a '/', the system gives a beep/ding sound (to alert them to the invalid separator) and converts it to a '-'. For any other disallowed character, the system gives a beep/ding sound, discards the input and waits for the user to input a valid character.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, Paul, for the reply and code!
    charlie
    charlie6067

Posting Permissions

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