Inserting Date into Userform Testbox

raschwab

New Member
Joined
Mar 26, 2011
Messages
15
I am endeavouring to insert a date into a textbox on a userform, using the following code:

Code:
Private Sub tbDate_Change()
    Me.tbDate.Value = Format(Me.tbDate.Value, "dd mmm yy")
End Sub

I initialized the testbox to reflect the current date; however, when I attempt to alter the date, a revised date appears as soon as I enter the first character of the date that I want.

Any help/advice would be most appreciated.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try using the AfterUpdate event instead...

Code:
[font=Verdana][color=darkblue]Private[/color] [color=darkblue]Sub[/color] tbDate_AfterUpdate()
    Me.tbDate.Value = Format(Me.tbDate.Value, "dd mmm yy")
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
Thanks for responding, Domenic. The "AfterUpdate" event does resolve the main problem. Now the format comes in as "4/29/11" as opposed to the "dd mmm yy" format that I would like to see. Any ideas as to how how I could overcome this issue?

Incidentally, I have designed the userform such that the user can call up Calendar Control 12.0. This works perfectly.
 
Upvote 0
I was incorrect when I stated that the output format was "4/29/11". This was the format resulting from the initialization code. That said, when I type "11apr11" the output does not change to "11 Apr 11" per the "dd mmm yy" format specified. Would appreciate any insights as to how I could circumvent this issue.

Rick Schwab
 
Upvote 0
When entering a value that represents a date, it needs to be entered in a format that can be recognized by Excel, such as 4/15, 4/15/11, 4/15/2011, Apr 15, 2011, April 15, 2011, 15 Apr 11, and 15 Apr 2011.
 
Upvote 0
Thanks, Domenic. It does work if I follow the format. I was hoping that it would work the way I had tried it, which I was able to do when I insert the data directly into the spreadsheet cell. Thanks again.
 
Upvote 0
Since you have Calendar Control 12.0, why not use it to allow the user to select the date, instead of having the user enter the date in a textbox?
 
Upvote 0
Why restrict the user to the format they enter the date in?

If you need the date entered converted to a particular format for further use it's pretty easy to check they have entered a valid date with the IsDate function.

If they have you can convert it to the format you need, if they haven't you can ask them to re-enter the date.
 
Upvote 0
Why restrict the user to the format they enter the date in?

If you need the date entered converted to a particular format for further use it's pretty easy to check they have entered a valid date with the IsDate function.

If they have you can convert it to the format you need, if they haven't you can ask them to re-enter the date.

Hi Norie,

I had thought about using the IsDate function to validate a date entry. However, it returns True if a date is mistakingly entered in the following format...

"30-Feb-11"

Hence, my suggestion to use the Calendar control. I'd appreciate any comments.
 
Upvote 0
Domenic

Forgot about that.

I suppose you could use IsDate as the 1st 'line of defence' and then validate further.

The Calendar control might be the best bet, but it isn't necessarily going to be available to everyone.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,876
Members
452,949
Latest member
Dupuhini

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