When I click button I want to put todays date in textbox

Ben2k

Board Regular
Joined
Oct 16, 2002
Messages
77
Hi,

I have a form field like this

Label TextBox CommandButton

When I press the command button I want todays date to be inserted into the textbox.

So far i've tried getting the button to run a macro called GetTodaysDate

In the macro i've send it to RunCode, a function called FuncTodaysDate

This is where i'm stuck, where do I create the function and what code do I use?

Thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You can set the default value of the field (in table) or the forms control to put in todays date by puting in

Now()

but if you really want to use the button, here is one way:

double click the button and in the ONclick property activate the code builder by clicking the button with 3 dots.

Put in the following to match you control names:


Private Sub Command10_Click()

Text7.SetFocus

If IsNull(Me![Text7]) Then
'works also
'If Text7.Text = "" Then

todaysdate

End If

MsgBox "Field already filled", vbOKOnly

End Sub

Private Sub todaysdate()

Text7.Text = Now()

End Sub
 
Upvote 0
Two comments here, it is probably better to use Date() rather than Now() as Now() is a Date/Time stamp and makes criteria like =#01/01/2004# a bit of a problem.

You can also add the date with the kb shortcut ctrl-;

HTH

Peter
 
Upvote 0

Forum statistics

Threads
1,221,773
Messages
6,161,855
Members
451,724
Latest member
sledparty

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