Go Back   Access World Forums > Apps and Windows > Visual Basic

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 03-23-2018, 03:28 AM   #1
rede96
Newly Registered User
 
Join Date: Apr 2004
Posts: 129
Thanks: 5
Thanked 1 Time in 1 Post
rede96 is on a distinguished road
Selecting an item in a listbox

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?

__________________
It's simple... only when you know how!
rede96 is offline   Reply With Quote
Old 03-23-2018, 03:42 AM   #2
arnelgp
Registered User
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 7,199
Thanks: 57
Thanked 2,312 Times in 2,218 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Selecting an item in a listbox

replace your code with this one:

Me.listBoxA = Me.listBoxA.ItemData(i)
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 03-23-2018, 04:02 AM   #3
rede96
Newly Registered User
 
Join Date: Apr 2004
Posts: 129
Thanks: 5
Thanked 1 Time in 1 Post
rede96 is on a distinguished road
Re: Selecting an item in a listbox

Quote:
Originally Posted by arnelgp View Post
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.

__________________
It's simple... only when you know how!
rede96 is offline   Reply With Quote
Old 03-23-2018, 04:07 AM   #4
arnelgp
Registered User
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 7,199
Thanks: 57
Thanked 2,312 Times in 2,218 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Selecting an item in a listbox

Show me your code.
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 03-23-2018, 04:36 AM   #5
rede96
Newly Registered User
 
Join Date: Apr 2004
Posts: 129
Thanks: 5
Thanked 1 Time in 1 Post
rede96 is on a distinguished road
Re: Selecting an item in a listbox

Quote:
Originally Posted by arnelgp View Post
Show me your code.
Ok sure, code is below.

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

Quote:
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
__________________
It's simple... only when you know how!
rede96 is offline   Reply With Quote
Old 03-23-2018, 05:12 AM   #6
arnelgp
Registered User
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 7,199
Thanks: 57
Thanked 2,312 Times in 2,218 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Selecting an item in a listbox

If the Bound column of your listbox is 1 then

Me.lstOpenStrips=iPos
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 03-23-2018, 05:43 AM   #7
rede96
Newly Registered User
 
Join Date: Apr 2004
Posts: 129
Thanks: 5
Thanked 1 Time in 1 Post
rede96 is on a distinguished road
Re: Selecting an item in a listbox

Quote:
Originally Posted by arnelgp View Post
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

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?

__________________
It's simple... only when you know how!
rede96 is offline   Reply With Quote
Old 03-23-2018, 07:35 AM   #8
arnelgp
Registered User
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 7,199
Thanks: 57
Thanked 2,312 Times in 2,218 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Selecting an item in a listbox

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)
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 03-23-2018, 08:05 AM   #9
rede96
Newly Registered User
 
Join Date: Apr 2004
Posts: 129
Thanks: 5
Thanked 1 Time in 1 Post
rede96 is on a distinguished road
Re: Selecting an item in a listbox

Quote:
Originally Posted by arnelgp View Post
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
Quote:
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.
__________________
It's simple... only when you know how!
rede96 is offline   Reply With Quote
Old 03-23-2018, 09:46 AM   #10
arnelgp
Registered User
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 7,199
Thanks: 57
Thanked 2,312 Times in 2,218 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Selecting an item in a listbox

Im out of thoughts.
Maybe Requery it first before setting the value
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 03-24-2018, 04:30 AM   #11
rede96
Newly Registered User
 
Join Date: Apr 2004
Posts: 129
Thanks: 5
Thanked 1 Time in 1 Post
rede96 is on a distinguished road
Re: Selecting an item in a listbox

Quote:
Originally Posted by arnelgp View Post
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.

Thanks anyway, your time is appreciated.
__________________
It's simple... only when you know how!
rede96 is offline   Reply With Quote
Old 03-24-2018, 07:56 PM   #12
arnelgp
Registered User
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 7,199
Thanks: 57
Thanked 2,312 Times in 2,218 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Selecting an item in a listbox

I think this will help the proper way to delete items in list. You start from the last item.
https://stackoverflow.com/questions/...ing-first-sele

__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
group query listbox - amend each record represented by grouped listbox item(s) NBRJ Forms 3 04-27-2016 02:24 PM
Selecting an item in a listbox using VB CrArC Modules & VBA 6 11-30-2011 07:56 AM
How to print report from listbox without selecting item maya Forms 1 08-20-2003 09:46 PM
pre-selecting listbox item billwagnon Forms 0 02-03-2003 08:38 AM
Opening a form when selecting an item in Listbox Kmsdove Forms 2 07-02-2000 07:48 AM




All times are GMT -8. The time now is 04:39 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World