Default to Current Date in Excel Form Textbox

GeminiG

New Member
Joined
Feb 22, 2016
Messages
13
Hi All,
I have a worksheet with a command button that opens an Excel form named frmMain.
On that form I have a frame named fraMain that has a textbox named txtDate. (The first field on the form)
I'm trying to get the current Date to populate the textbox automatically while also allowing the user to enter in a different date if needed. This is what I've tried all the following in the form VBA but no date shows up on the form load:<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">

Code:
[COLOR=#101094][FONT=Verdana][FONT=Consolas]Private Sub frmMain_Activate()[/FONT][/FONT]
    txtDate.text = Format(Now(),"MM/DD/YY")
End Sub
[/COLOR][COLOR=#101094][FONT=Verdana][FONT=Consolas]
Private Sub frmMain_Initialize()[/FONT][/FONT][/COLOR]
[COLOR=#101094]    me.txtDate.text = Format(Now(),"MM/DD/YY")[/COLOR]
[COLOR=#101094]End Sub[/COLOR]
[COLOR=#101094]
Private Sub fraMain_txtDate_Activate()
    If Me.txtDate.Value = "" Then
        Me.txtDate.Value = Format(Date, "dd/mm/yyyy")
    End If
End Sub

Private Sub fraMain_Initialize()
    If Me.ActiveControl.ActiveControl.Name = "txtDate" Then
        Me.txtDate.Value = Format(Date, "dd/mm/yyyy")
    End If
[/COLOR]


</code>Can somebody point out what I am doing wrong? I realize I only need on function but I can't get any to work. Thanks!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I've tried:

Private Sub frmMain_Initialize()
Me.txtDate.Text = Format(Now(), "MM/DD/YY")
End Sub

and it worked with me, when i launch the userform date shows up..no problem..
try to comment all other code regarding this userform and live just the code above..
 
Upvote 0
I tried inserting your code and deleting all of my entries still doesn't work.
It sets the focus to the txtDate of that form but doesn't enter a date, nothing. Is it because the textbox is inside of a frame on the form?
 
Upvote 0

Forum statistics

Threads
1,223,155
Messages
6,170,405
Members
452,325
Latest member
BlahQz

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