Consulting

Results 1 to 13 of 13

Thread: Texbox - Numbers ONLY - Date

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    The keypress will work. I think I have it in a few of my older workbooks. Xld gives you an example even. But I am still partial to the comboboxes.

  2. #2
    VBAX Tutor Philcjr's Avatar
    Joined
    Jul 2005
    Location
    Bedminster, NJ
    Posts
    208
    Location
    XLD,

    Thanks for your input, huge help.

    All,

    I am attaching my version of a TextBox that will capture a date that a user inputs.

    ANY suggestions by anyone would greatly be appreciated.

    Oh ya, here is the code incase you dont want to download the file...
    Option Explicit
     
    Private Sub CommandButton1_Click()
        If IsDate(TextBox1.Value) = False Or Left(TextBox1.Value, 2) > 12 Or _
        Mid(TextBox1.Value, 4, 2) > 31 Then MsgBox ("This is not a date, Please try again") _
        Else MsgBox ("This is a date, Good for you")
    End Sub
     
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Dim Temp As String
        Select Case KeyAscii
            Case Asc("0") To Asc("9")
            Case Else: KeyAscii = 0
        End Select
        If Len(TextBox1) = 2 Or Len(TextBox1) = 5 Then _
            TextBox1.Value = TextBox1.Value & "/"
         End if
    End Sub
     
    Private Sub UserForm_Initialize()
        TextBox1.Value = Format(Date, "MM/DD/YY")
        CommandButton1.SetFocus
    End Sub
    Last edited by Aussiebear; 03-23-2025 at 08:58 AM.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    This is overkill as the IsDate function will not allow an invalid number of days or months, so all you need is

    Private Sub CommandButton1_Click()
        If Not IsDate(TextBox1.Value) Then
            MsgBox ("This is not a date, Please try again")
        Else
            MsgBox ("This is a date, Good for you")
        End If
    End Sub
    In addition, your way only works for US dates, as we continentals use a dd/mm/yy format, where the first two digits can be greater than 12. Mine works for any.

    .
    Last edited by Aussiebear; 03-23-2025 at 08:59 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Tutor Philcjr's Avatar
    Joined
    Jul 2005
    Location
    Bedminster, NJ
    Posts
    208
    Location
    XLD,

    Thanks for the heads-up... However, if I wanted to ensure that a US date only was entered correctly, would my way surfice?

    The rare times I get to create files that capture date(s) would only be that of US Dates. I will, nevertheless, be mindful of US dates and Continental dates.

    Thanks for all your input/help

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Quote Originally Posted by Philcjr
    However, if I wanted to ensure that a US date only was entered correctly, would my way surfice?
    Your way works for US only dates if it will only be used in the US, but over here it fails because it will not let me input a day of 13, and if I revert the format, the IsDate fails.

    My suggestion works for US dates in the US, continental dates in the continent.

    .
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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