Solved

Grid logic problem

Posted on 2004-04-20
4
304 Views
Last Modified: 2010-05-02
I can't think today.  I have a grid loaded with items.  I have a loop that runs through each row and selectively removes what rows aren't required.  Here's what I've been playing with:

For x = 0 To Grid1.Rows - 1
   If Grid1.Cell(flexcpText, x, 8) = "0" Then
       Grid1.RemoveItem x
       ' x = x + 1
   End If
Next x

This uses a VSFlexgrid but a grid's a grid for these purposes.  Anyways, my problem is, I've started the loop from 0 to rows-1.  Lets say there's 6 rows when the loop starts.  When I remove an item, Grid1.Rows is now 5, and when x reaches its upper value (6) it's an invalid cell reference beacuse the number of items was reduced.  After the Removeitem, I've tried adding 'x=x+1' to force it out of the loop but for some reason I still get the same problem; 5 items (0-4)  but an x value of 5 because one was removed.

Is there some way of re-assigning the upper bounds of the for loop while the loop is running?  (or some other solution...)

Thanks!
0
Comment
Question by:MIKEV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Shauli earned 250 total points
ID: 10871786
do it backword, from the last row to 0

For x =(Grid1.Rows - 1) To 0 Step -1
   If Grid1.Cell(flexcpText, x, 8) = "0" Then
       Grid1.RemoveItem x
       ' x = x + 1
   End If
Next x

S
0
 
LVL 6

Expert Comment

by:LunaSkye
ID: 10871819
I have had this problem in the past,

I have not found a way to get past this limitation, once a loop starts, its set..

I have implemented several methods to get past this..
1. When an item is removed, use a goto to move back to ONE LINE before the loop starts so that the loop is re calculated on the fly.

StartAgain:
  For x = 0 to Grid.Rows-1
     If <OkToRemoveItem> = True then
        Grid.RemoveItem x
        Goto StartAgain
     End If
  Next x

The problem with this is that it could present a slowdown, if you have a VERY long list, and the same members are being scanned several times..

The other way is:
2. For the times when you have to remove a list item, subtract one from the loop counter.  So that when it reaches the "NEXT" statement, the counter is incremented up to the next item, which would be the same item you just removed, but now it is a new one.

At the end of the loop, before the item is incremented, place a check.  If the counter is goign past its bounds, exit for.
 
  For x = 0 to Grid.Rows-1
     If <OkToRemoveItem> = True then
        Grid.RemoveItem x
        x = x -1
     End If

     if (x + 1) > Grid.Rows then exit for
  Next x

Maybe this will help

-Andrew
0
 

Author Comment

by:MIKEV
ID: 10871827
Awesome, thanks. :)
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10871874
You are welcome :)

S
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question