Results 1 to 5 of 5

Thread: Restrict User To Enter Permissible Date Range Values Only In TextBox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    40
    Rep Power
    0

    Restrict User To Enter Permissible Date Range Values Only In TextBox

    Hi, I have a form that has a specific textbox named DateBox and I want to restrict the user from entering any date later than 25 November 2013.
    Any quick ideas on how to do this?

    I have searched quickly but did not manage to find something suitable.
    Thanks in advance for any reply.

    I have tried something like this but it is not working...

    Code:
    Private Sub Datebox_Change()
    Dim maxdate As String
    maxdate = "25/11/2013"
    If Datebox.Value > maxdate Then
    MsgBox "You have entered an invalid date"
    Else
    Exit Sub
    End If
    End Sub
    Last edited by MrBlackd; 11-15-2013 at 02:33 PM.

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    Quote Originally Posted by MrBlackd View Post
    Hi, I have a form that has a specific textbox named DateBox and I want to restrict the user from entering any date later than 25 November 2013.
    Any quick ideas on how to do this?

    I have searched quickly but did not manage to find something suitable.
    Thanks in advance for any reply.

    I have tried something like this but it is not working...

    Code:
    Private Sub Datebox_Change()
    Dim maxdate As String
    maxdate = "25/11/2013"
    If Datebox.Value > maxdate Then
    MsgBox "You have entered an invalid date"
    Else
    Exit Sub
    End If
    End Sub
    I don't think you will want to use the Change event as it is fired for every character you type or delete... you won't have time to form your dates before you test the entry for being a date. I think you will need to use the Exit event... if the date is not proper, you can cancel the exit. Code like this should work for you...

    Code:
    Private Sub DateBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
      If Len(DateBox.Value) Then
        If IsDate(DateBox.Value) Then
          If CDate(DateBox.Value) > DateSerial(2013, 11, 25) Then
            MsgBox "You have entered an invalid date"
            Cancel = True
          End If
        Else
          MsgBox "You have entered an invalid date"
          Cancel = True
        End If
      End If
    End Sub

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    40
    Rep Power
    0
    Thank you for your reply....

    I have actually decided to include the restriction on the save button of the form and not as a private sub.
    Last edited by MrBlackd; 11-15-2013 at 03:29 PM.

  4. #4
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    Quote Originally Posted by MrBlackd View Post
    Thank you for your reply.... one more question...

    what exactly does the line
    Code:
    Cancel=True
    The Exit event fires when the focus leaves the TextBox... setting Cancel=True literally cancels the exiting of the TextBox and the cursor remains in the TextBox until the user enters a proper date. Since I did not want to trap the user in there forever (assuming they did not know what a proper date was), I allow the TextBox to be exited if it is empty. If you did not want to do that (in other words, allow the program to go on with bad data assuming the user does not heed the MessageBox's advice by correcting the data), then remove the Cance=True statements.

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    40
    Rep Power
    0
    I understand, no need though to remove the statements since as mentioned in my previous (edited) post I have decided to include the restrictions in the save button of the form.

    But thanks for letting me know for future use, since I would not want the program to go on with bad data either.

    Your help was valuable!

Similar Threads

  1. Replies: 10
    Last Post: 10-17-2013, 07:36 PM
  2. Combobox with Dynamic Date Range
    By paul_pearson in forum Excel Help
    Replies: 5
    Last Post: 07-21-2013, 06:14 PM
  3. Replies: 5
    Last Post: 06-15-2013, 12:40 PM
  4. Replies: 3
    Last Post: 05-17-2013, 01:22 PM
  5. Replies: 2
    Last Post: 03-31-2012, 10:46 PM

Posting Permissions

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