is Order By UniqueIDField Desc okay with you? Or you have to have sorted Asc?
Mike
Main Topics
Browse All TopicsNeed SubForm to show last 5 records on open in datasheet view.
SubForm window is sized to show 6 rows; 5 last and 1 new, focus needs to be left at last/new record, because some of the columns/fields of the actively selected SubForm record is always displayed in the parent form.
The problem is: getting the form to show the 4 previous records, it only shows that last record and new record row. In other words, how to fill up the SubForm window with records. The User needs to see the last few records when the form opens, if they exist.
I repeat it olnly show that last two rows/records.
I've tried some creative coding:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Open
DoCmd.GoToRecord , , acLast
SendKeys "{pgup}"
SendKeys "{pgup}"
SendKeys "^{DOWN}"
'=========='These caused errors when no previous records exist.=======
' DoCmd.GoToRecord , , acLast
' DoCmd.GoToRecord , , acPrevious
' DoCmd.GoToRecord , , acPrevious
Solutions greatly appreciated.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Try this
Dim rs As DAO.Recordset
Dim i As Integer
Dim bLoop As Boolean
DoCmd.GoToRecord , , acLast
Set rs = Me.RecordsetClone
rs.MoveLast
bLoop = True
i = 1
Do While bLoop = True
If rs.BOF = True Then
rs.MoveFirst
bLoop = False
Else
rs.MovePrevious
i = i + 1
If i = 5 Then bLoop = False
End If
Loop
If rs.EOF = False And rs.BOF = False Then Me.Bookmark = rs.Bookmark
rs.Close
Set rs = Nothing
Thanks all, I'm glad I was in training for most of the day, because it allowed you all to keep tweaking the solution.
Unfortunatly I get a "type missmatch #13" error on the "Set rs = Me.RecordsetClone" line.
I don't have a clue why.
========Start========
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Open
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
If rs.EOF = False Then DoCmd.GoToRecord , , acLast, 5
Exit_Open:
Exit Sub
Err_Open:
If Err.Number = "2105" Then
Resume Next
Else
MsgBox Err.Description & Chr(13) & "Error #: " & Err.Number
Resume Exit_Open
End If
End Sub
========end========
Did I mention that this is a ADP (linked to an sql db).
try one of two things. not sure if it matters that if its an ADP
change
Dim rs as DAO.Recordset
to
Dim rs as Object
or
go to Tools/References and check Microsoft DAO Object Library (latest version)
RecordsetClone is DAO property of the form, u wont be able to use ADO but I dont know about ADP's. Im sure its still DAO
talk about putting me on the spot!
I aint falling for that trap :)
I'll let u decide, look at the responses, eghtebas came up with a tidier solution from my original one, and I in turn made it even shorter!
So I'll let u go from there
And its good to know, ADP based forms RecordsetClone makes use of ADO not DAO, thanks
Just in case I was clear, which I'm often not as much as I think I am.
Dim rs As ADODB.Recordset
Set rs = Me.RecordsetClone
If rs.EOF = False Then DoCmd.GoToRecord , , acLast, 5
BTW: Thanks for skiping capricorn1 comment, (nothing personal cap), because I didn't want to limit the records. They need to see at least the last few, sometimes all (~100s) before they create a *new one.
In addition, the users are accustomed to adding new records to the bottom with acending order; Oldest on top down to newest on bottom. I don't think you can move the add new record * line to the top and change the order to desc.
You guys are great, its refreshing to see such honesty, respect, and humility.
With I could give you both 500pts.
I'm going with the average of both your suggestions, which I calc to being 1 : .375
(62.5% 37.5) 312/188
Thanks again.
BTW:
MS Visual Basic Help states:
Recordset Property...
The read/write behavior of the Recordset property is determined by the type of recordset (ADO or DAO) and the type of data (Jet or SQL) contained in the recordset identified by the property.
Recordset type Based on SQL data Based on Jet data
ADO Read/Write Read/Write
DAO N/A Read/Write
Business Accounts
Answer for Membership
by: capricorn1Posted on 2006-09-11 at 16:04:37ID: 17498617
you can set the sub form record source to a query that will return the last 5 records
select top 5 *
from tableName
Order By UniqueIDField Desc