Search code examples
vb6msflexgrid

Why is VB6 FlexGrid throwing a run-time error 381 'Subscript out of range'?


I don't really know much about VB6, but I am currently having a problem with double clicking a cell in a FlexGrid. I can see that this is a popular question that has been asked many times, but most of the users are looping around the grid. Mine is more simple, but yet it crashes.

The grid has 3 rows and 3 columns. When double clicking on any of the columns from the first row, it works fine. But when I get to the others, it throws an error. The error says:

Run-time error '381':

Subscript out of range.

It currently crashes at the following line, where the row is 2 and the col is 3:

TextMatrix = myFlexGrid.TextMatrix(row, col)

I am just wondering why it crashes when the indexes of the row and the column are fine. I thought that it might be zero-based but when clicking the third cell on the first row, it works fine.

Is there a way to see the contents of the grid in the debugger (Microsoft Visual Basic 6.0)?


Solution

  • The rows and cols of a flexgrid are indeed 0 based. However, if you take the FlexGrid.Row and FlexGrid.Col values, then they are always going to be in range, as these denote the currently selected cell. Something like this should work:

    private sub FlexGrid_DblClick()
        dim CellContents as string
        with FlexGrid
            CellContents = .TextMatrix(.Row,.Col)
        end with
    end sub
    

    Although in the case that you are interested in the selected cell, you can always just use

    CellContents = FlexGrid.Text

    You can get a lot of info on any of your controls (or variables) at run time using the "Locals" window of the IDE, while execution is paused. Unfortunatley, that doesn't seem to include the cell contents of a flexgrid. You can always use the Immediate window to feel around the grid though, with lines like ? FlexGrid.TextMatrix(0,2) to find the values and the limits that cause errors.