473,796 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List.Listcount property not working

2 New Member
My code:

If List.Listcount > 0 then
For intI = 0 to List.Listcount - 1
'code
Next intI
End If

For some reason, and not all the time, this loop only runs to the second last entry. I have even witnessed it in break mode and following it step by step with Shift + F8, where it reports - for example - that the listcount is 14, when if fact it is 15 and has index values of 0 to 14. I made a quick fix, but I don't like this solution as it affects my ability to bug test the rest of the sub.

Quick and dirty fix:

On Error Resume Next
If List.Listcount > 0 then
For intI = 0 to List.Listcount
'code
Next intI
End If

This way if listcount is correct, the loop will error on the last value, but will simply resume after the loop, but when it reports it incorrectly, it should still retrieve all items in the list.

My question is why is the listcount property not reporting the correct value, if anybody else has encountered this problem, and if anybody knows a better fix for this. (ps. the ubound property is even less reliable for me)
Apr 12 '07 #1
5 3510
iburyak
1,017 Recognized Expert Top Contributor
It usually happens when you delete items from list.
In this case you have to go opposite way

Expand|Select|Wrap|Line Numbers
  1. If List.Listcount > 0 then
  2. For intI = List.Listcount – 1 to 0 Step -1
  3. 'code
  4. Next intI
  5. End If

Good Luck.
Apr 13 '07 #2
Esmael
58 New Member
My code:

If List.Listcount > 0 then
For intI = 0 to List.Listcount - 1
'code
Next intI
End If

For some reason, and not all the time, this loop only runs to the second last entry. I have even witnessed it in break mode and following it step by step with Shift + F8, where it reports - for example - that the listcount is 14, when if fact it is 15 and has index values of 0 to 14. I made a quick fix, but I don't like this solution as it affects my ability to bug test the rest of the sub.

Quick and dirty fix:

On Error Resume Next
If List.Listcount > 0 then
For intI = 0 to List.Listcount
'code
Next intI
End If

This way if listcount is correct, the loop will error on the last value, but will simply resume after the loop, but when it reports it incorrectly, it should still retrieve all items in the list.

My question is why is the listcount property not reporting the correct value, if anybody else has encountered this problem, and if anybody knows a better fix for this. (ps. the ubound property is even less reliable for me)

Hi...

I try to evaluate that in my PC... but i dont see any problem...
Heres my code:

If LstGroups.ListC ount > 0 Then
For i = 0 To LstGroups.ListC ount - 1
Debug.Print i
Next i
End If


On my Immediate window i successfully see the output from 0-14 of 15 records... and it has no error...

What was the error message?


GoodLuck...
Apr 13 '07 #3
eleksoft
2 New Member
It does not give me any error. This loop is pulling the list entries from a list that is on another form. Each time I return to my parent form and add something new to the list, it shows what I added to the list just before that item in the view form (after it again executes this loop).
Apr 14 '07 #4
Killer42
8,435 Recognized Expert Expert
It does not give me any error. This loop is pulling the list entries from a list that is on another form. Each time I return to my parent form and add something new to the list, it shows what I added to the list just before that item in the view form (after it again executes this loop).
If no error occurs, then your addition of On Error code will have no effect.

The only way I can think of that this could happen is if the number of items in the list changes during the execution of the loop. Don't forget, the limits of the loop are set at the start, so if the number of items in this list decreases during execution of the loop, you will simply go past the end of the list and start returning empty strings. (Try it - List(n) just returns nothing, if n is too high.)
Apr 14 '07 #5
Killer42
8,435 Recognized Expert Expert
Sorry, I just re-read your original post. I see you have increased the loop end and put in the On Error to catch it. But as I said, going past the end of the List() array doesn't produce an error - it simply returns no string. So the On Error will still achieve nothing.
Apr 14 '07 #6

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

Similar topics

3
3614
by: BillB | last post by:
When I activate frmList.List0, it positions at the end of the table: - frmList Private Sub Form_Activate() Me.List0.SetFocus Me.List0.ListIndex = Me.List0.ListCount - 1 - When I DblClick a row in frmList.List0, I close frmList and open frmEdit to edit record I clicked on: -frmList
19
3574
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main form, the subform returns the records based on the two criteria. The criteria used on the main form are values selected in two list boxes. When the user clicks on the first list box (lstCollege), it returns values in the second list box...
0
1710
by: Darryl Kerkeslager | last post by:
Access 2002 on Win98 at least, not tested elsewhere: Bug: The ListCount property of an unbound listbox may be incorrect if you use column heads. Model: Set up a simple database with two simple tables (I used employer and person). Create a bound form based on the employer table, with a txtName textbox, a txtID textbox, and an unbound listbox to the employer table . Add a second unbound listbox for the person table, with column...
2
2032
by: Ellen Manning | last post by:
I'm developing an A2K adp that contains doctor info and the months they are not available for teaching. A doctor can be unavailable for some months for one year and other months for another year. I want the user to enter the year then use a multi-select list box to choose the months not available, then go to a fresh screen, enter another year and choose the months not available for that year, and so on. In the table, there is one record...
7
2582
by: Dave Hopper | last post by:
Hi I posted a question recently regarding problems I am having getting a value from a list box to use in a query. I got a lot of help, for which I thank you and it's nearly working! But I need a little more help on one more point. This is what i've got. I have code that hunts for updated appointments in a public folder based on the order that happens to be open (code fires on an on open event) This works fine and updates my
6
4899
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
8
2745
by: sara | last post by:
I am learning Access and programming. I wanted to have the user select the departments for an ad from the list of all departments. Found code (that I could understand) on this site, and it works. But I have 2 quesitons: 1. How can I REMOVE a selection from the Destination List box - and keep the others there? My first code removes ALL from the Source list; this code does nothing. 2. How can I now USE the data - I have to...
3
6303
by: peter.mosley | last post by:
I've tried googling for the answer to this problem, without any luck. I'm sure the truth must be out there somewhere! I have a multiselect listbox populated with many items (set by the RowSource property). In my code I cause a number of these to be selected - but if the first selected item is not within the currently visible range then there is no change to the display. This gives the impression that nothing has happened. I think...
4
1792
by: Tony Toews [MVP] | last post by:
A client asked why some code behind a list box suddenly started updating 57386 records in a table instead of the expected 38 records. Turns out the problem is a difference in behavior between Access 2007 and previous versions of Access. The following code selects all 38 records in the list box in A2003. For lngLoop = 0 To Me.lstUnreceivedPieces.ListCount - 1 Me.lstUnreceivedPieces.Selected(lngLoop) = True Next lngLoop
0
9683
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
9529
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,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9054
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...
0
6792
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
5443
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...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
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.