borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (data grids for a new applications) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Unexpected behavior with for each loop
Poster Message
BlackDuck603
Level: Whizz Kid


Registered: 04-10-2007
Posts: 14

icon Unexpected behavior with for each loop

I am getting unexpected behavior and an error with a For Next loop for a listbox with VBA.  

When the listbox has no items, the For condition seems to be ignored.
Does For-Next ALWAYS execute the loop at least once?

For intExistingRow = 0 To (listBoxCurrentInventoryItems.ListCount - 1)
   ' When the list is empty and the for loop erroneuosly executes, the following error occurs at the assignment on the next line of code
    ' Run-time error 94 - Invalid use of Null
    ' I understand that I can put a IsNull check here,
    ' but why does the code even get here if the ListCount is 0
    strExistingItem = listBoxCurrentInventoryItems.ItemData(intExistingRow)
    
    If (strQueuedItem = strExistingItem) Then
        ' Duplicate - do nothing
    Else
        bNewItem = True
    End If

Next intExistingRow
            

I appreciate and comments on this
- Bill
                

19-11-2007 at 03:10 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 972
icon Re: Unexpected behavior with for each loop

For-Next is executed zero or more times.
However, ensure that the list is EMPTY. Having even 1 item in it will execute this statement once.
Your invalid use of null indicates that there is at least 1 item in the list (otherwise you get an index out of range error(ish)).
Sugest you print the listboxcurrentinventoryitems.list(0). Is this Access? That may return a blank row from the rowsource.

Hope this helps,
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

21-11-2007 at 07:52 AM
View Profile Send Email to User Show All Posts | Quote Reply
BlackDuck603
Level: Whizz Kid


Registered: 04-10-2007
Posts: 14
icon Re: Unexpected behavior with for each loop

Kieron - Thanks for your response.

I am using Access, which might be the explanation. Perhaps it is returning a blank row. However, I am still perplexed as to why the loop even iterates one time. According to the lisbox's ListCount property (at runtime), the listbox is empty (count is 0)....yet the code inside the loop still executes for this condition.

I am leaning towards belief that this is some kind of weird hidden "feature" in Access's VBA.


As a workaround, I added a condition test above the loop to check the ListCount value. If it is 0, then I won't try the for loop.  

Happy Thanksgiving !!!


  

21-11-2007 at 01:07 PM
View Profile Send Email to User Show All Posts | Quote Reply
Nick2k3
Level: Big Cheese

Registered: 23-11-2003
Posts: 25
icon Re: Unexpected behavior with for each loop

quote:
BlackDuck603 wrote:
Kieron - Thanks for your response.

I am using Access, which might be the explanation. Perhaps it is returning a blank row. However, I am still perplexed as to why the loop even iterates one time. According to the lisbox's ListCount property (at runtime), the listbox is empty (count is 0)....yet the code inside the loop still executes for this condition.

I am leaning towards belief that this is some kind of weird hidden "feature" in Access's VBA.


As a workaround, I added a condition test above the loop to check the ListCount value. If it is 0, then I won't try the for loop.  

Happy Thanksgiving !!!


  


your for loop starts with 0


____________________________
Error 404 ...File not found...!!!!
19-01-2008 at 08:49 AM
View Profile Send Email to User Show All Posts | Quote Reply
BlackDuck603
Level: Whizz Kid


Registered: 04-10-2007
Posts: 14
icon Re: Unexpected behavior with for each loop

OK - I see.

This for loop behavior is a bit different than what I have used in c#.

The first statement initialises the iterator, the condition evaluates it against an end value, and the second statement changes the iterator value.
If I wrote this in C# it would have been
for (idx = 0; idx < ListCount; idx++)
{
}

My understanding (erroneous be it may) was that the TO in the FOR statement was acting as a LESS THAN (the upper limit), but iI realize now that TO is more like EQUAL TO.  

I see the error in my ways.
Makes sense to me now.

Thanks for the clarification Nick2k3

22-01-2008 at 01:05 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Unexpected behavior with for each loop
Next Topic (data grids for a new applications) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder