Keep last entered date on form

R

ryguy7272

I am curious to know if a TextBox on a form can be set to the last date that
was in the TextBox. I have this little piece of code that sets the TextBox
to the current date.
Private Sub TDATE_Click()
TDATE.Value = Date
End Sub

However, as I think about it, I will enter dates using the form, but the
date, and related data, may not be today; I may enter data, and related
dates, for yesterday or two days ago, so I’d like to click the TextBox and
have the date be today’s date or if I choose another date, such as yesterday,
keep this date in that TextBox, for the next record that I’ll enter, after
the current record is entered. Does that make sense?

Thanks!
Ryan---
 
J

John Spencer

Basically, you want to have the control default to today's date. However,
once you enter a different date during a data entry session, you want the
control to default to whatever date you have entered.

If you don't want that as a default value (automatic fill when you create a
new record), but only when you click on the control then you need to store the
date in a variable and use that. UNTESTED CODE follows

Option Compare Database
Option Explicit

Dim DefaultDate As Date

Private Sub TDate_AfterUpdate()
If IsNull(Me.TDate) = False Then
DefaultDate = Me.TDate
End If
End Sub

Private Sub TDATE_Click()
IF IsNull(DefaultDate) Then
Me.TDATE = Date
Else
Me.TDATE = DefaultDate
End IF
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
J

John W. Vinson

I am curious to know if a TextBox on a form can be set to the last date that
was in the TextBox. I have this little piece of code that sets the TextBox
to the current date.
Private Sub TDATE_Click()
TDATE.Value = Date
End Sub

However, as I think about it, I will enter dates using the form, but the
date, and related data, may not be today; I may enter data, and related
dates, for yesterday or two days ago, so I’d like to click the TextBox and
have the date be today’s date or if I choose another date, such as yesterday,
keep this date in that TextBox, for the next record that I’ll enter, after
the current record is entered. Does that make sense?

I'd use the Doubleclick event rather than Click - it's much too easy to
unintentionally click a textbox. Then set the control's default value in its
own AfterUpdate event, to make the entry "sticky":

Private Sub TDATE_AfterUpdate()
Me!TDATE.DefaultValue = """" & Format(Me!TDATE, "yyyy-mm-dd") & """"
End Sub

The default property is a text string, hence the quotes; and the formatting
ensures that the default is a valid date/time value. You will need to change
the format if you include a time portion or have an input mask.
 
R

ryguy7272

All great advice! John, i was thinking of something like that. I actually
went with Ken's suggestion:

Private Sub Form_AfterUpdate()
Me.TDATE.DefaultValue = """" & Me.TDATE & """"
End Sub

Thanks everyone!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top