Solved

Fill date field with value from previous record if a new record

Posted on 2015-01-12
9
281 Views
Last Modified: 2015-01-12
I have a form with a date field.  If the user is entering a new record I want that date field to auto-fill with the date from the previous record.

How?
0
Comment
Question by:SteveL13
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
from the previous record.
At any point in time, the "Previous" record could be any record of the set or no record at all if you are looking at the first record of a set.  Previous has no meaning in a relational database since tables/queries are unordered sets unless you sort them by something.  You would need to tell us precisely what you mean by previous and how to handle the "no previous" situation.

The date may belongs in the parent record so it doesn't have to be duplicated.
0
 

Author Comment

by:SteveL13
Comment Utility
Regarding... "You would need to tell us precisely what you mean by previous and how to handle the "no previous" situation."...

Suppose on the form I filled in a date of 11/1/2013.  When I go to the next NEW record I want the field to automatically fill with 11/1/2013.  But if I open the database again and go to a new record, I want the date field to be blank.

--Steve
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
when you save the record, place the value of the date to a variable

tempvars.add "mydate", me.txtDatexx.value

then when creating new record, use it to fill the date

if me.newrecord and Nz(tempvars!mydate,"") <>"" then
  me.txtDatexx=tempvars!mydate
end if
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
You would use the DefaultValue for this:

Private Sub txtDate_AfterUpdate()
    With Me!txtDate
        .DefaultValue = "#" & Format(.Value, "mm\/dd\/yyyy") & "#"
    End With
End Sub

Clear the value when opening the form (will also work when opening the app several times a day):

    Me!txtDate.DefaultValue = ""

/gustav
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:SteveL13
Comment Utility
Is not working quite right.  When I enter a new record the date goes back to the first record entered date.  See attachment.
Remember-last-value-entered.accdb
0
 

Author Comment

by:SteveL13
Comment Utility
Whoops!  My reply was for Rey.  I'll try Gustav suggestion.
0
 

Author Comment

by:SteveL13
Comment Utility
Gustav solution not working either See attached.
Remember-last-value-entered.accdb
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points
Comment Utility
It's working great if you put the code where it should be:

Private Sub txtDateEntered_BeforeUpdate(Cancel As Integer)
     With Me!txtDateEntered
         .DefaultValue = "#" & Format(.Value, "mm\/dd\/yyyy") & "#"
     End With
End Sub

You may wish to skip updating with Null:

Private Sub txtDateEntered_BeforeUpdate(Cancel As Integer)
     With Me!txtDateEntered
        If Not IsNull(.Value) Then
            .DefaultValue = "#" & Format(.Value, "mm\/dd\/yyyy") & "#"
        End If
     End With
End Sub

/gustav
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
Comment Utility
steve

you have to move the form to new record, your form is in edit mode..

click the New record first, enter the info then click Add Record
Remember-last-value-entered.accdb
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now