Input mask in a text box for date

brendalpzm

Board Regular
Joined
Oct 3, 2022
Messages
58
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
I have a text box from activeX controls, let's name it DateBox.

This text box is meant to be used for input a date, so I want it to show DD/MM/YYYY as example when it's not being filled, and when the user clicks on the field to fill it that it changes to __/___/____ and when they start typing the _ start disappearing but the / remain.

Also to add a warningmessage whenever they type a weird date, here in my country we usually usea MM/DD/YYYY format but we need to stick to english format, so, to avoid error I'd like to add the warning message.

If you could help me I could be greatful :(((
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi Brenda,

I have done a couple of different things with dates, including the input mask. I ultimately move on from that because it doesn't allow someone to paste in a date if they want to. (Actually it might have been a phone number now that I think about it, but the issue is the same either way.)

My solution was to allow them to enter a valid date in whatever format the want, and then convert it programmatically when they leave the text box. I don't know how you feel about that idea, but I thought I'd share it. Here's the code I use:

VBA Code:
Private Sub ConvertDate_Click()
   
    ' Get the value from the Date_Txtbox
    Dim strDate As String
    strDate = Date_Txtbox.Value
   
    ' Convert the value to a date format
    Dim d As Date
    If IsDate(strDate) Then
        d = CDate(strDate)
    Else
        MsgBox "Invalid date format"
        Exit Sub
    End If
   
    ' Convert the date to the format "yyyy-mm-dd"
    Dim strNewDate As String
    strNewDate = Format(d, "yyyy-mm-dd")
   
    ' Update the value in the Date_Txtbox
    Date_Txtbox.Value = strNewDate
   
End Sub

You'll want to change the date format in the code to dd-mm-yyyy, change the name of your form control, and place the code in the exit event for your text box.

Let us know if this works for you.
 
Upvote 1
Solution
Hi Brenda,

I have done a couple of different things with dates, including the input mask. I ultimately move on from that because it doesn't allow someone to paste in a date if they want to. (Actually it might have been a phone number now that I think about it, but the issue is the same either way.)

My solution was to allow them to enter a valid date in whatever format the want, and then convert it programmatically when they leave the text box. I don't know how you feel about that idea, but I thought I'd share it. Here's the code I use:

VBA Code:
Private Sub ConvertDate_Click()
  
    ' Get the value from the Date_Txtbox
    Dim strDate As String
    strDate = Date_Txtbox.Value
  
    ' Convert the value to a date format
    Dim d As Date
    If IsDate(strDate) Then
        d = CDate(strDate)
    Else
        MsgBox "Invalid date format"
        Exit Sub
    End If
  
    ' Convert the date to the format "yyyy-mm-dd"
    Dim strNewDate As String
    strNewDate = Format(d, "yyyy-mm-dd")
  
    ' Update the value in the Date_Txtbox
    Date_Txtbox.Value = strNewDate
  
End Sub

You'll want to change the date format in the code to dd-mm-yyyy, change the name of your form control, and place the code in the exit event for your text box.

Let us know if this works for you.
thank you so muchhhh!!
 
Upvote 0
I have a text box from activeX controls, let's name it DateBox.

This text box is meant to be used for input a date, so I want it to show DD/MM/YYYY as example when it's not being filled, and when the user clicks on the field to fill it that it changes to __/___/____ and when they start typing the _ start disappearing but the / remain.

Also to add a warningmessage whenever they type a weird date, here in my country we usually usea MM/DD/YYYY format but we need to stick to english format, so, to avoid error I'd like to add the warning message.

If you could help me I could be greatful :(((
I know you already have resolved this but just wanted to let you know I implemented your data entry mask idea here

Regards.
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,731
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top