OutOfBoundsException on Listbox

lbEvents.addRow "1" lbEvents.cell( i, 0 ) = sDate lbEvents.cell( i, 1 ) = sType

The above code gets an OutOfBoundsException when adding the 8th row to the lbEvents ListBox. i = 7 and sDate = “2016/07/07” (from the debugger window). The data from the same table is properly shown in another form without problem - including the same row causing the problem here.

I read the docs on OutOfBoundException and it doesn’t give me a clue what might be wrong (there are 25 displayed rows on the listbox though I know that doesn’t have anything to do with it, but just the same…)

Where do I look for a problem? Have traced with debugger multiple times and it gets the same error at the same point each time.

thanks, always know I can count on you guys here…

Just out of curiosity, I commented out the sDate line and reran the test. It then got the outofbounds on the sType line of the same record.

Out of bounds: you tried to write outside of the defined Row(s) or Cell(s).

Also, Row #1 is Row = 0 in code (0-based).

lbEvents.cell( i, 1 ) = sType

I suppose that your Listbox have two Columns (Column 0 and Column 1) ?

And you know you can write:

lbEvents.addRow sDate, sType

?

Can you please post the complete code?

Also, you could try…

lbEvents.addRow “1”
lbEvents.cell( lbEvents.LastIndex, 0 ) = sDate
lbEvents.cell( lbEvents.LastIndex, 1 ) = sType

Actually, I found the problem. What I didn’t include in the code was that it is being executed inside an IF statement. And I had mistakenly left the i=i+1 line outside the IF which was apparently causing the problem. Moved the increment statement to the correct place and all worked correctly.

Very sorry to have jumped the gun and bothered you folks before I had fully researched the problem…

Actually, that’s often the best way to find the problem, I’ve found. Once you’ve composed the post, the solution usually becomes obvious milliseconds after you press the ‘Start Conversation’ button. That button is one of my favorite debugging tools. :wink:

Maintaining your own index is generally a bad idea. Use LastIndex, or supply all the values to AddRow.

[quote=283564:@Kenneth Lossman]lbEvents.addRow “1”
lbEvents.cell( lbEvents.LastIndex, 0 ) = sDate
lbEvents.cell( lbEvents.LastIndex, 1 ) = sType[/quote]
Usually, I do:

[code]
Dim LocRow As Integer

lbEvents.addRow “1”

LocRow = lbEvents.LastIndex

lbEvents.cell(LocRow , 0 ) = sDate
lbEvents.cell(LocRow 1 ) = sType[/code]

Can also be useful when the Row is filled in a loop.

BTW: sometimes (less and less often nowadays) I have the chance to understand before clicking the Post a Reply button.