Selecting an item in a listbox (1 Viewer)

rede96

Registered User.
Local time
Today, 19:03
Joined
Apr 2, 2004
Messages
134
Hi All,

I have 2 listboxes (set to value list) which I transfer data between. After I move a record from ListboxA to ListboxB for example, I want to keep the selection on the next record down in ListboxA.

This isn't a problem as I can use
Code:
 Me.ListBoxA.Selected(i) = True
where (i) is the index of the record I want to select.

The problem is that after my code executes, when I press the down key to move down the list in ListboxA, the cursor jumps back up to the top of the listbox and highlights the first record.

I'd like the cursor to stay on the highlighted record and move down from there. (Like if I click on it with the mouse.)

I've had a good old search around but so far can't find what I'm looking for.

Can anyone help please?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:03
Joined
May 7, 2009
Messages
19,169
replace your code with this one:

Me.listBoxA = Me.listBoxA.ItemData(i)
 

rede96

Registered User.
Local time
Today, 19:03
Joined
Apr 2, 2004
Messages
134
replace your code with this one:

Me.listBoxA = Me.listBoxA.ItemData(i)

Thanks for the reply. Unfortunately that does not work. The cursor still jumps to the top of the list on the first press of the down arrow key.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:03
Joined
May 7, 2009
Messages
19,169
Show me your code.
 

rede96

Registered User.
Local time
Today, 19:03
Joined
Apr 2, 2004
Messages
134
Show me your code.

Ok sure, code is below.

Just for reference, ListboxA is called lstOpenStrips and ListboxB is lstPriorityList

Private Sub CmdWanted_Click()
Dim IntX As Integer
Dim ItemsCount As Integer
Dim ItemS As Variant
Dim ItemI As Integer
Dim iPos As Integer
Dim rstWO As String
Dim rstPart As String
Dim rstDesc As String
Dim rstCell As String
Dim rstTAT As String
Dim rstAge As String
Dim rstLast As Date
Dim AddLine As String
Dim rstSSHours As Double
Dim IntY As Integer
Dim ItemR As Integer
Dim ItemAr() As Integer 'Array for storing row index to be deleted after transfer

' Set variables
IntX = 0
ItemsCount = Me.lstOpenStrips.ItemsSelected.Count
'Redim ItemArray
ReDim ItemAr(0 To (ItemsCount - 1))

' Check if any items have been selected
If ItemsCount = 0 Then Exit Sub

' Cycle thrugh items selected and store row details.
For Each ItemS In Me.lstOpenStrips.ItemsSelected
ItemI = ItemS
iPos = Nz(Me.lstOpenStrips.Column(0, ItemI))
rstWO = Nz(Me.lstOpenStrips.Column(1, ItemI))
rstPart = "'" & Nz(Me.lstOpenStrips.Column(2, ItemI)) & "'"
rstDesc = "'" & Nz(Me.lstOpenStrips.Column(3, ItemI)) & "'"
rstCell = Nz(Me.lstOpenStrips.Column(4, ItemI))
rstTAT = Nz(Me.lstOpenStrips.Column(5, ItemI))
rstAge = Nz(Me.lstOpenStrips.Column(6, ItemI))
rstLast = Nz(Me.lstOpenStrips.Column(7, ItemI))
rstSSHours = Nz(Me.lstOpenStrips.Column(8, ItemI))
AddLine = iPos & ";" & rstWO & ";" & rstPart & ";" & rstDesc & ";" & rstCell & ";" & rstTAT & ";" & rstAge & ";" & rstLast & ";" & rstSSHours
' Add line to Priority List
Me.lstPriorityList.AddItem AddLine
' store row index for delete after transfer
ItemAr(IntX) = ItemI
IntX = IntX + 1
Next
' Delete rows that have been transferred
For IntY = 0 To IntX - 1
ItemR = ItemAr(IntY) - IntY
Me.lstOpenStrips.RemoveItem (ItemR)
Next IntY

' set focus in lstOpenStrips to where last record was transferred
Me.lstOpenStrips.SetFocus
Me.lstOpenStrips.Selected(ItemR) = True
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:03
Joined
May 7, 2009
Messages
19,169
If the Bound column of your listbox is 1 then

Me.lstOpenStrips=iPos
 

rede96

Registered User.
Local time
Today, 19:03
Joined
Apr 2, 2004
Messages
134
If the Bound column of your listbox is 1 then

Me.lstOpenStrips=iPos

Yes the bound column is 1 but it still doesn't work :confused:

EDIT: I can set the iPos to any number but every time I press the down arrow the cursor moved back to the top of the list.

What I might try is setting a key press event to select the row index, see if that works. But I shouldn't have to do that should I?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:03
Joined
May 7, 2009
Messages
19,169
everytime you delete an item from the list
the listindex is recreated again to 0, 1,2,3,4 (in sequence).
therefore there will be no gap in the index
even if you delete several item.

so i think that the last item you removed, say index 10
will not be found when the actual left list is only less
than 10. so it goes back to index 0.

just for a test create a public function
to return the minimun number:

Code:
public function minOf2(p1, p2)
minof2=p1
if p2<p1 then minof2=p2
end function


now you set your lstOpenStrips to:

me.lstOpenStrips = minOf2(iPos, me.lstOpenStrips.ListCount-1)
 

rede96

Registered User.
Local time
Today, 19:03
Joined
Apr 2, 2004
Messages
134
everytime you delete an item from the list
the listindex is recreated again to 0, 1,2,3,4 (in sequence).
therefore there will be no gap in the index
even if you delete several item.

so i think that the last item you removed, say index 10
will not be found when the actual left list is only less
than 10. so it goes back to index 0.

just for a test create a public function
to return the minimun number:

Code:
public function minOf2(p1, p2)
minof2=p1
if p2<p1 then minof2=p2
end function


now you set your lstOpenStrips to:

me.lstOpenStrips = minOf2(iPos, me.lstOpenStrips.ListCount-1)

Ok thanks, I will try the code later. But I don't think it is a problem with the list index. The list has over 100 rows but even if I just remove one row somewhere near the top of the list the problem still happens.

Also, when I use
Me.ListBoxA.Selected(i) = True
the row is highlighted no problem. So it looks like it should work ok, but when I press the down arrow key the cursor jumps back to the top of the list.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:03
Joined
May 7, 2009
Messages
19,169
Im out of thoughts.
Maybe Requery it first before setting the value
 

rede96

Registered User.
Local time
Today, 19:03
Joined
Apr 2, 2004
Messages
134
Im out of thoughts.
Maybe Requery it first before setting the value

I've had a play around and still can't get it to work. The only thing I thought of is that as I am in an access form (not programming in VBA) then maybe there is some property of the form I could work with. But so far nothing.

I can select the item, I can even move the item to the next list box but if I click the down key, the cursor moves back to the top. :confused:

Thanks anyway, your time is appreciated.
 

Users who are viewing this thread

Top Bottom