Move to last record after form opened

B

BrunoKP

I want a form coded to move to the last record immediately after the form has
been opened.
I don't want the recordset to be sorted, and I can't use Open event as the
first record has not yet been displayed.
 
M

Maurice

if it's an editable form you could use something like:

docmd.openform "formname",,,, acformAdd

this will take you to a new record in the form. In a continuous form that
will show the last record and a new record where you can start typing...
 
B

BrunoKP

Hello Maurice
No I don't want to add a new record, I just want to move to the last
existing record.
I have tried this, but end up in the first record:

Private Sub Form_Load()
Dim rst As dao.Recordset
Dim dbsCurrent As Database

Set dbsCurrent = CurrentDb()
Set rst = Me.RecordsetClone
rst.MoveLast
rst.Close
dbsCurrent.Close
End Sub

I suppose that the recordset has not yet been displayed when Form_Load() is
activated. The question is, which event is the right one to use?

Thank you
Bruno

"Maurice" skrev:
 
M

Maurice

In that case try this in the Open event of the form:

DoCmd.RunCommand acCmdRecordsGoToLast

that should do the trick...
 
M

Marshall Barton

BrunoKP said:
I have tried this, but end up in the first record:

Private Sub Form_Load()
Dim rst As dao.Recordset
Dim dbsCurrent As Database

Set dbsCurrent = CurrentDb()
Set rst = Me.RecordsetClone
rst.MoveLast
rst.Close
dbsCurrent.Close
End Sub

I suppose that the recordset has not yet been displayed when Form_Load() is
activated. The question is, which event is the right one to use?


NOTE: You should never use Close on something you did not
Open!

When you use RecordsetClone, you have to sync the form's
current record to the RecordsetClone's current record:

With Me.RecordsetClone
.MoveLast
Me.Bookmark = .Bookmark
End With

OTOH, In A2000 and later, for the simple operation you want
to do here, there is no need to use RecordsetClone (and
possibly have to deal with an emptry recordset). It is
simpler to use:

Me.Recordset.MoveLast
 
B

BrunoKP

I'm happy to use the simple and elegant solution: Me.Recordset.Movelast in
Form_Open.

Thank you Maurice and Marshall

"Marshall Barton" skrev:
 
M

Marshall Barton

BrunoKP said:
I'm happy to use the simple and elegant solution: Me.Recordset.Movelast in
Form_Open.


The Open event is supposed to be too early to do that. The
Load event is the recommended place for this kind of thing.
I can't explain why, but if it works ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top