find last and first record in navigation (1 Viewer)

amir0914

Registered User.
Local time
Yesterday, 23:50
Joined
May 21, 2018
Messages
151
hi all, i make tow button to next and previous records in subform , now i want to msgbox when select first and last record by button. is it possible???

Screenshot (620).png
 

June7

AWF VIP
Local time
Yesterday, 22:50
Joined
Mar 9, 2014
Messages
5,424
Here is code from my db:

Code:
Private Sub btnNext_Click()
    With Me
        .RecordsetClone.Bookmark = .Bookmark
        .RecordsetClone.MoveNext
        If Not .RecordsetClone.EOF Then
            DoCmd.GoToRecord acForm, , acNext
        Else
            .RecordsetClone.MoveLast
            MsgBox "Last record."
            .btnNext.Enabled = False
        End If
        .btnPrevious.Enabled = True
     End With
End Sub

Private Sub btnPrevious_Click()
    With Me
        .RecordsetClone.Bookmark = .Bookmark
        .RecordsetClone.MovePrevious
        If Not .RecordsetClone.BOF Then
            DoCmd.GoToRecord acForm, , acPrevious
        Else
            .RecordsetClone.MoveFirst
            MsgBox "First record."
            .btnPrevious.Enabled = False
        End If
        .btnNext.Enabled = True
    End With
End Sub
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 06:50
Joined
Sep 21, 2011
Messages
14,048
Extra code has slipped in there June
Code:
.btnPrevious.Enabled = TrueEnd Sub


The O/P will probably copy it as is?
 

amir0914

Registered User.
Local time
Yesterday, 23:50
Joined
May 21, 2018
Messages
151
thanks june, i use your code but i get error as below image :

Screenshot (621).png
 

June7

AWF VIP
Local time
Yesterday, 22:50
Joined
Mar 9, 2014
Messages
5,424
Thanks, Gasman. Edited typo in my post. Also edited to use Me instead of form name.

amir, did you modify code to use your form and control names? Post your exact code.
 

Mark_

Longboard on the internet
Local time
Yesterday, 23:50
Joined
Sep 12, 2017
Messages
2,111
Something important to note regarding "First" and "Last" record; these can change at any time. If you are looking at a list of all customers who's name begins with "C", as soon as another user inserts or edits a record your "First" or "Last" could change.

This can be a nice feature in some cases. Often simply seeing no records before or after the one you are on is enough, especially in high volume databases.
 

Cronk

Registered User.
Local time
Today, 17:50
Joined
Jul 4, 2013
Messages
2,770
Why not use the sub form's navigation buttons to move through records? The box displaying "x of y" records is an indicator of first/last.
 

amir0914

Registered User.
Local time
Yesterday, 23:50
Joined
May 21, 2018
Messages
151
thanks all, yes june, i change control name in my form, but i use image instead button in form and i change it to image name.

Forms!frm_Date2!Child111.SetFocus

With Me
.RecordsetClone.Bookmark = .Bookmark
.RecordsetClone.MoveNext
If Not .RecordsetClone.EOF Then
DoCmd.GoToRecord acForm, , acNext
Else
.RecordsetClone.MoveLast
MsgBox "Last record."
.Image658.Enabled = False
End If
.Image657.Enabled = True
End With
 

Users who are viewing this thread

Top Bottom