473,670 Members | 2,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ListBox ListIndex behavior

10 New Member
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.ListInde x > -1 Then
List98.Selected (List98.ListInd ex) = False
End If
2)
List98.RowSourc e = ""
3)
Set rst = CurrentDb.OpenR ecordset( "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 12172
ADezii
8,834 Recognized Expert Expert
@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 New Member
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 Recognized Expert Expert
@juing
Glad you figured it out.
Apr 23 '09 #4
NeoPa
32,569 Recognized Expert Moderator MVP
@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
7861
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 allso subclassed the window and do see all kinds of WS_??? messages coming by. But now I'm stuck :-\ I've got *no* idea what to do next, and all my searching on the web leads me
4
8486
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
42734
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 possible by code? Filip
2
4512
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 S7-300"
2
6066
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 the values from the bound column of a MultiSelect listbox, so that I can include them in a SQL query that will then be the recordsource for a report. This gives me 2 of the 3 values I need to put into the SQL query. However, I can't get the non...
2
11820
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
8423
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: ListBox1.AddItem('a') ListBox1.AddItem('b') ListBox1.AddItem('c') ListBox1.AddItem('d')
3
14442
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 feature is that the first row is at the top of the listbox.
1
4021
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 are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
0
8466
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7410
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.