List Box Question

A

Anand

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.

TIA
Anand
 
K

Klatuu

Not clear what you want to move. Are you saying you want the buttons to move
from one item in the list to the next?
 
D

Dirk Goldgar

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 ------
 
D

Dirk Goldgar

Oops -- here's a quick correction for fncSelectPreviousInListbox:

'------ start of code ------
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)
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 ------
 
A

Anand

Thanks for the inputs. Was away from Internet for few days hence was not able
to respond. Was also not very clear about the question I posted. Sorry about
that.

The listbox functions you gave work like a charm. I just added a line to
force access to get the data related to the listbox and I have what I want.

Thanks again.
Anand
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top