Anand said:
Hello,
Am using A2k.
Have created a facility to search data in unbound forms. Have a listbox in
the form to display the search results. Clicking on any record in the
listbox
pulls up the data regarding the record through a function and populates
fields in the form.
Want to know how I can use backward/forward buttons on the same form to
move
the context in the listbox while also pulling the relevant data for the
record. Must be able to move back or forward from any selected record in
the
listbox.
You don't give quite enough information to fully answer this question, but
you can select the next or previous row in the list box using functions like
these:
'------ start of code ------
Function fncSelectNextInListbox( _
lst As Access.ListBox, _
Optional bWrap As Boolean) _
As Long
' Select the next item in the list box passed as <lst>.
' If no item is selected, select the first one.
' If the optional <bWrap> argument is True, then the selection
' will wrap around from the last item in the list box to the
' first item.
' NOTE: THIS FUNCTION ONLY WORKS FOR NON-MULTISELECT LIST BOXES.
' Copright (c) 2009, DataGnostics LLC.
' You are free to use this code in your application, so long
' as the copyright notice remains unchanged.
Dim lngMaxIndex As Long
Dim lngFirstIndex As Long
With lst
If .ListCount <> 0 Then
lngFirstIndex = Abs(.ColumnHeads)
lngMaxIndex = .ListCount - lngFirstIndex - 1
If .ItemsSelected.Count = 0 Then
.Value = .ItemData(lngFirstIndex)
Else
If .ListIndex >= lngMaxIndex Then
' We're at the end of the list box.
If bWrap Then
.Value = .ItemData(lngFirstIndex)
End If
Else
.Value = .ItemData(.ListIndex + lngFirstIndex + 1)
End If
End If
End If
fncSelectNextInListbox = .ListIndex
End With
End Function
Function fncSelectPreviousInListbox( _
lst As Access.ListBox, _
Optional bWrap As Boolean) _
As Long
' Select the previous item in the list box passed as <lst>.
' If no item is selected, select the first one.
' If the optional <bWrap> argument is True, then the selection
' will wrap around from the first item in the list box to the
' last item.
' NOTE: THIS FUNCTION ONLY WORKS FOR NON-MULTISELECT LIST BOXES.
' Copright (c) 2009, DataGnostics LLC.
' You are free to use this code in your application, so long
' as the copyright notice remains unchanged.
Dim lngMaxIndex As Long
Dim lngFirstIndex As Long
With lst
If .ListCount <> 0 Then
lngMaxIndex = .ListCount - 1
lngFirstIndex = Abs(.ColumnHeads)
If .ItemsSelected.Count = 0 Then
.Value = .ItemData(lngMaxIndex + lngFirstIndex)
Else
If (.ListIndex + lngFirstIndex) <= lngFirstIndex Then
' We're at the top of the list box.
If bWrap Then
.Value = .ItemData(lngMaxIndex)
End If
Else
.Value = .ItemData(.ListIndex - 1 + lngFirstIndex)
End If
End If
End If
fncSelectPreviousInListbox = .ListIndex
End With
End Function
'------ end of code ------