Solved

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

Posted on 2015-01-12
9
284 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 35

Expert Comment

by:PatHartman
ID: 40544728
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
ID: 40544817
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40545084
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40545123
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
 

Author Comment

by:SteveL13
ID: 40545130
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
ID: 40545131
Whoops!  My reply was for Rey.  I'll try Gustav suggestion.
0
 

Author Comment

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

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 40545162
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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 40545180
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

785 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