Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting the DefaultValue property

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I want to set the default value of a field to the value of the last entry. For example, after moving to a new record, the default value of a date field would be what ever the value is of the last date entered.

The variable name is vDate and the field name is Date. The syntax that I used was:

Forms![Accident Data]!Date.DefaultValue = vDate

1. Is the syntax correct.
2. What module should contain the above line (Before Update, After Update, After Insert, all of the above)???

I'm sure that the answer is a simple one for an experienced Access Programmer (which I'm not).
 
Actually, try using this in the default value property of the field or form control:

=DLast("DateFieldName", "tablename")
 
In the forms module just under:
"Option Compare Database
Option Explicit"

Type in: Dim dtMyDate as Date

In the after update event for your Date field add:

dtMyDate = Me![Date]
Me![Date].DefaultValue = dtMyDate

Be careful using the "Date" name in VB~Access as it is kind of a reserve word and must be surrounded by square brackets in order to avoid confusion.





Gord
ghubbell@total.net
 
If you have a couple of fields wich need to be filled with the previous value you should put the values of the form in a temp table (on after update of the form) and then put the table in the new record (on current).
here is the code after update:

Private Sub Form_AfterUpdate()
' make recordset called rstpref from a table called temp
' delete the entire tables data
Set rstpref = CurrentDb.OpenRecordset("temp")
Do While Not rstpref.EOF
rstpref.Delete
rstpref.MoveNext
Loop

' put the current forms data in the temp table
' in this case I only put in the date but other fields can be put in
rstpref.AddNew
rstpref.date = Me.date
rstpref.Update
End Sub

here is the code on current

Private Sub Form_Current()

' open the temp table to see what the last entry was
Set rstpref = CurrentDb.OpenRecordset("temp")

' if the temp table is not empty and the form is at
' a new record fill in the previous value
If Not rstpref.EOF And Me.NewRecord Then
Me.date = rstpref!date
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top