473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ListBox ListIndex behavior

10
Hi All,

I am very new in the VBA coding and I've noticed this (to me strange) behavior:
Assume this code for "Value Items" type of single select two column ListBox:

1)
If List98.ListIndex > -1 Then
List98.Selected(List98.ListIndex) = False
End If
2)
List98.RowSource = ""
3)
Set rst = CurrentDb.OpenRecordset( "any query" )
rst.MoveFirst
4)
Do Until rst.EOF
List98.AddItem """" & rst![field1] & """" & ";" & """" & rst![field1] & """"
rst.MoveNext
Loop
5)
rst.Close

Let's say ListIndex was 2 before calling this code (third item is selected).
After point 1) ListIndex = -1 (no selection).

The "issue" is: When the third item is added in point 4) into the listbox the ListIndex property automatically sets itself back to 2 (the original value). Until the third item is added it remains -1. Adding next items doesn't change the ListIndex anymore so the final value is 2.

What am I doing wrong?
Thanks a lot.
juing
Apr 22 '09 #1
4 12079
ADezii
8,834 Expert 8TB
@juing
ho this can hapen
I don't see how this can happen, but as a newly added last code line, try Requerying the List Box, namely:
Expand|Select|Wrap|Line Numbers
  1. Me![List98].Requery
Apr 22 '09 #2
juing
10
I believe using "Requery()" method on ListBox with RowSourceType = "Value list" doesn't have any effect.

Anyway, I finally got it working ...
The problem is that it is not possible to use "Selected()" method on single selection ListBox.
Although this line
Expand|Select|Wrap|Line Numbers
  1. List98.Selected(List98.ListIndex) = False
changes the ListIndex to -1 but the Value property remains set to the last selected item (it's bound).
This causes that when I put item with the same value as stored in the "Value" property the ListBox automatically sets its ListIndex to index of that value.

To avoid this beahavior it is necessary to unselect single selection ListBox using code:
Expand|Select|Wrap|Line Numbers
  1.  List98.Value = Null 
Thanks.
juing
Apr 23 '09 #3
ADezii
8,834 Expert 8TB
@juing
Glad you figured it out.
Apr 23 '09 #4
NeoPa
32,556 Expert Mod 16PB
@juing
Thanks for posting Juing. That area (with MultiSelects etc) can be quite complicated. The interface is far from intuitive.
Apr 23 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
4
by: John Guarnieri | last post by:
Hi All, I need some code to drag items in a list box either up or down along with not just the text but with the itemdata too. Can anyone hook me up? TIA John
4
by: Filips Benoit | last post by:
Dear All, I have code that selects - find and select - 1 item in a large listbox. This works Ok but most of the time the selected item isn't visible without scrolling down manualy. Is this...
2
by: James A | last post by:
I have 3 problems which I'd like people to solve for me (I've run out of ideas) 1) I have a table called s7-300. If i simply run this line of code: lstResult.rowsource = "Select * FROM...
2
by: Alan Lane | last post by:
Hello world: I'm using Access 2003. I have 2 listboxes. One is a single column. The other has two columns. I can use Dev Ashish's code (thanks Dev!) from the Access MVP Website to accumulate...
2
by: Parintas Themis STE Kardias | last post by:
How can i select the first row in a list box, after pressing the TAB key
1
by: andrew.panin | last post by:
Hi there! I've got a trouble with these things. What's going on? 1ST STEP: we have ListBox item. Let's call it ListBox1. We're adding four values to it using AddItem method:...
3
by: superjacent | last post by:
Hope someone can help. I have a listbox displaying time periods in blocks of 15 mins for a 24 hour period, all up 96 rows. The listbox can only visibly show 20 rows a time. The default...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.