Move record selected in Listbox to top of listbox control

G

Guest

I have code in the form current event that selects a record in a listbox,
based on what was selected in a previous form. This works fine, except that
you have to scroll down the list box manually to see the record selected, if
it's not one of the first 20.

How can I get the listbox to move the selected record to the top of the
listbox without reordering the list?
 
G

Guest

I don't know the answer to your question.

Instead of solving the list box scrolling - I might instead simply add a new
textbox labled "Selected': that auto enters that same
information that was selected in this box so it is upfront and center to the
viewer's eye.....and thus they don't need to look in the listbox

Not sure if this cheat would work for you though....
 
G

Guest

Let me know if this helps, it works for me.

If you FIRST select List Item 0, and THEN select the Item you REALLY want,
it ends up highlighting and exposing the desired item, in this case Item 25
in my listbox which I made only one inch high:

Private Sub btnTestListVal_Click()
Dim ctlTmp As Control
Set ctlTmp = Me.lstTest

ctlTmp.Selected(0) = True
ctlTmp.Selected(25) = True
End Sub
 
G

Guest

Hi Holly,

How about something like this:

For intLoop = 0 To (Me.MyList.ListCount - 1)
If (Me.MyList.Column(1, intLoop) = CStr(Me.txtSaveProposalId)) =
True Then
SaveThisNumber = intLoop
End If
Next intLoop

Me.MyList.Selected(0) = True
Me.MyList.Selected(SaveThisNumber) = True
 
G

Guest

This didn't work. It selects record number 0 in the list. I can't select
another record until I unselect the first record (MyList.Selected(0)=False)
Am I doing something wrong? Here's my code:

Dim intLoop As Integer
Dim blnTemp as Boolean

For intLoop = 0 To (Me.MyList.ListCount - 1)
blnTemp = (Me.MyList.Column(1, intLoop) = CStr(Me.txtSaveProposalId))
Me.MyList.Selected(0) = blnTemp
Me.MyList.Selected(intLoop) = blnTemp
Next intLoop

Thanks for you help.
 
G

Guest

No, that did not work either. Access won't let me select another record from
the list box until I unselect the first record (MyList.Selected(0)=False).
Using your example, the first record stays selected.

Even doing this, produces the same result, it only selects record 0:

Private Sub Form_Current()
Me.MyList.Selected(0) = True
Me.MyList.Selected(30) = True
Exit Sub

If you got this to work, can you send me an .mdb with it in it?
 
G

Guest

I figured out why it wasn't working, the Multi-select property is set to
Extended. The user can select multiple records from this list box. If I
change it to None, your code works. I tried re-setting the multi-select
property before and after my code, but Access won't allow me to set this
property at this time.

Any other ideas?
 
G

Guest

Someone had suggested having two list boxes, one right behind the other
(visible = true, visibile = false) one having Extended, the other None.

And then toggle back and forth depending upon the requirements. You could
even allow the user to toggle this themselves.

Is that helpful?

Short of resorting the list (something you didn't want to do-although not
difficult) I think I'm stumped.
 
G

Guest

Good idea about the two listboxes. Resorting would be too much work, do to
other selection/sorting criteria on the form. I think I'll play around with
setting the multi select option a little more, and if I can't get it to work,
I'll use your idea.

Thanks for all your help! Have a great day!
 

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

Similar Threads


Top