Displaying the last saved record in the form

  • Thread starter Thread starter sheela
  • Start date Start date
S

sheela

I like to display the last saved record from the database
into in a form, when ever the form is open. In the form
load I added the following code:

Private Sub Form_Load()
DoCmd.GoToRecord , , acLast
End Sub
It doesn't work. All the fields in the form are bound to
the table fields, except one field in the form is unbound.
Is it causing the problem?
Thanks for any help.
sheela
 
sheela said:
I like to display the last saved record from the database
into in a form, when ever the form is open. In the form
load I added the following code:

Private Sub Form_Load()
DoCmd.GoToRecord , , acLast
End Sub
It doesn't work. All the fields in the form are bound to
the table fields, except one field in the form is unbound.
Is it causing the problem?
Thanks for any help.
sheela

You don't say in what way it "doesn't work". However, the last record,
as far as the form is concerned, is just the last one in its current
sort order, which isn't necessarily the last record saved or updated.
If your form is bound to a table with a non-random autonumber primary
key, and has not been sorted on any other field, then the "last record"
is likely to be the record that was most recently added -- but not
necessarily the record that was most recently updated.

You cannot reliably do what you ask unless there is a "LastModified"
date/time field in each record, and that field is set to the current
date and time (from the Now() function) in the form's BeforeUpdate
event. Then it would be possible to identify the most recently modified
record and go to it:

'----- start of example code -----
Private Sub Form_Load()

Me.Recordset.FindFirst "LastModified=" & _
Format(DMax("LastModified", Me.RecordSource), _
"\#mm/dd/yyyy\#")

End Sub
'----- end of example code -----
 
Back
Top