Hello, here is the code in UserForm1,
Option Compare Database
Option Explicit 'AussieBear 1 ste post
Private Sub btnPageDown_Click()
Dim lngPageSize As Long
Dim lngNewTopIndex As Long
Dim lngLastPossibleTopIndex As Long
' Determine the page size
lngPageSize = 40
' Adjust as needed
' Calculate the new TopIndex for Page Down
lngNewTopIndex = Me.ListBox1.listIndex + lngPageSize
' Calculate the maximum possible TopIndex to avoid going past the end
' ListCount - 1 is the last item index
' Subtracting (lngPageSize - 1) helps ensure the last page shows full content if possible
lngLastPossibleTopIndex = Me.ListBox1.ListCount
' Me.ListBox1.ItemData
' ListRows is often useful here
If lngLastPossibleTopIndex < 0 Then
lngLastPossibleTopIndex = 0
' Handle empty listbox
If lngNewTopIndex > lngLastPossibleTopIndex Then
lngNewTopIndex = lngLastPossibleTopIndex
End If
End If
'Aussiebear has just replied to a thread you have subscribed to entitled - error 7777 during execution - in the Access Help forum of VBA Express Forum.
Debug.Print "ListBox1.ListCount: " & Me.ListBox1.ListCount
Debug.Print "ListBox1.ListIndex: " & Me.ListBox1.listIndex
Stop ' This will pause execution so they can see the Immediate Window (Ctrl+G)
'Immediate Window (Ctrl+G)
'ListBox1.ListCount: 382
'ListBox1.ListIndex: -1
' Apply the new TopIndex
Me.ListBox1.listIndex = lngNewTopIndex 'this line causes error 7777
End Sub
Private Sub btnPageUp_Click()
Dim lngPageSize As Long
Dim lngNewTopIndex As Long
' Determine the page size (e.g., number of visible items)
' This might require some trial and error or calculation based on font size and listbox height
' For simplicity, let's assume a fixed page size for now
lngPageSize = 40
' Adjust as needed
' Calculate the new TopIndex for Page Up
lngNewTopIndex = Me.ListBox1.listIndex - lngPageSize
' Ensure we don't go below 0
If lngNewTopIndex < 0 Then
lngNewTopIndex = 0
End If
' Apply the new TopIndex
Me.ListBox1.listIndex = lngNewTopIndex
End Sub
Private Property Get listIndex() As Variant
ListBox1.listIndex = ListBox1.listIndex + 40 'for page down.
ListBox1.listIndex = ListBox1.listIndex - 40 'for page up.
End Property
Private Sub ListBox1_GotFocus() ' opmerking
With Me.ListBox1
[ListBox1].SetFocus
[ListBox1].listIndex = 0
End With
'Private Sub lstItems_GotFocus()
'[lstItems].SetFocus
'[lstItems].ListIndex = 0
End Sub
hope this helps
Greetings TonC