Solved

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

Posted on 2015-01-12
9
291 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 36

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

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 50

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Null or "" 28 51
ORDER BY 7 36
access query filter for lower case lett 7 15
MS Access VBA For Each Field Designation 3 15
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

679 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