Back on 07Ju11 I explained how to make Next and Previous buttons on an unbound form. It’s a little different on a bound form. I did, however, want some of the same behavior. I don’t like the Previous button to show if you are on the first record or the Next button to show if you are on the last record. This prevents the user from click a button when there is nowhere to go. The code below is how I control the display of the buttons. I ended up including Previous, Next, and Last buttons, a First button could also be included if you like. I also included a text box to let the user know that they are on “record 1 of 7”, or wherever they are.
For the sake of simplicity I used the exact same code for all three buttons:
Private Sub cmdPrevious _Click()
'Private Sub cmdNext _Click() 'Private Sub cmdLast _Click()
Dim intIndex As Integer
Dim intCount As Integer
'For the Previous button use:
DoCmd.GoToRecord , , acPrevious
'For the Next button use:
'DoCmd.GoToRecord , , acNext
'For the Last button use:
'DoCmd.GoToRecord , , acLast
'Get the count of records in the form's record set.
intCount = Me.Recordset.RecordCount
'Debug.Print " Count = " & intCount
'Get the cursor position in the recordset.
'Since the cursor position for the first record is zero
'add 1 to the cursor position so that the last one will equal the record count.
intIndex = Me.Recordset.AbsolutePosition + 1
'Debug.Print "Postion = " & intIndex
If intCount = intIndex Then
'The last record is being displayed, only the Previous button should show.
'In order to change the visible status of the Next and Last buttons, one which has the focus,
'make sure the Previous button is visible and move the focus to it.
Me.cmdPrevious.Visible = True
Me.cmdPrevious.SetFocus
Me.cmdNext.Visible = False
Me.cmdLast.Visible = False
ElseIf intIndex = 1 Then
'The first record is being displayed, only the Previous button should show.
'In order to change the visible status of the Previous button, which has the focus,
'make sure the Next button is visible and move the focus to it.
Me.cmdNext.Visible = True
Me.cmdNext.SetFocus
Me.cmdPrevious.Visible = False
Me.cmdLast.Visible = True
ElseIf intIndex > 1 And intIndex < intCount Then
'The displayed record is not the first or last record, all buttons should show.
'Since you are not changing the visible status of the button used
'the focus does not need to be reset.
Me.cmdPrevious.Visible = True
Me.cmdNext.Visible = True
Me.cmdLast.Visible = True
End If
'Give the user an indication of where they are in the record set.
Me.txtItemCount = "Item " & intIndex & " of " & intCount End Sub
No comments:
Post a Comment