Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSFlexGrid Error 3

Status
Not open for further replies.

jadams0173

Technical User
Feb 18, 2005
1,210
I have a MSFLEXGRID on a form. When the form loads I populate the FlexGrid. I also have a cmd button to let the user add a row. All is well except if the is no data to put in the flexgrid. The only row there is the fixed row with the column headings. Now when I push the button to add a row so i can input some data I get a sub script out of range. Can someone help me out? Here is the code
Code:
Private Sub cmdAddPart_Click()

Debug.Print frmDetails.MSFlexParts.Rows
frmDetails.MSFlexParts.Rows = frmDetails.MSFlexParts.Rows + 1
[red]frmDetails.MSFlexParts.TextMatrix(frmDetails.MSFlexParts.Row + 1, 1) = 0[/red]

If frmDetails.MSFlexParts.Rows > 13 Then
    frmDetails.MSFlexParts.ScrollBars = flexScrollBarVertical
    frmDetails.MSFlexParts.Width = 5005
Else
    frmDetails.MSFlexParts.ScrollBars = flexScrollBarNone
    frmDetails.MSFlexParts.Width = 4775
End If

End Sub
Subscript out of range occurs on the red line. I am trying to set a field in the new row to 0. Again, if the grid has data in it then this works. The error only accurs when the grid is empty. TIA
 
So add If statement that will take care of empty grid
 
I don't quite understand vladk. Even if the grid is empty when the user clicks the command button I still want to add a row and set the value to zero. Can you elaborate?
 
I got it fixed. I just trapped that error number and added a different line of code to set the value to 0. I still don't understand why the error is occuring when the grid is empty.
 
That it was I meant. But instead of trapping the error I wanted If (like if ...rows = N then). I think, the problem is in this:

...The only row there is the fixed row with the column headings...

This is impossible case. Just try design the grid with the only row that will be the fixed row.

You can also use additem instead. You will avoid your problem. Use vbTab to separate columns.
 
Exactly. It would have been much easier to simply do:
Code:
Private Sub cmdAddPart_Click()

fmDetails.MSFlexParts.AddItem vbTab & 0

If frmDetails.MSFlexParts.Rows > 13 Then
<etc>


 
I'll give that a try also. Thanks to both of you for responding.
 
jasen and vladk,

Thanks again for answering my question. Jasen, you example above worked great. Both of you deserve a star for showing me a better way to write my code!! Thanks a bunch.
 
[red]frmDetails.MSFlexParts.TextMatrix(frmDetails.MSFlexParts.Row + 1, 1) = 0[/red]

This should be frmDetails.MSFlexParts.Rows - 1, which would be the row you just added in rows + 1

Don't forget that the first row is 0 as is the first column

Of course, as pointed out by jasen, the better way to do it is to additem, which adds the row and data in one line.

One other thought:
Where you check for the number of rows before adding the scrollbar, you could instead check whether the height of the rows multiplied by the number of rows is greater than the height of the flexgrid itself. If it is, then add the scrollbar else set it to none as in the code below:

Code:
If (MSFlexParts.RowHeight(0) * MSFlexParts.Rows) > MSFlexParts.Height Then
  MSFlexParts.ScrollBars = flexScrollBarVertical
Else
  MSFlexParts.ScrollBars = flexScrollBarNone
End If

This way, if I resize the flexgrid, I don't have to worry about the number of rows.

[bigcheeks]
 
DeltaTech, thanks for the info. Another great tip. I will make this change. This is a great site thanks to members like yourself who offer tips that aren't even asked about. One day I hope to be able to answer some questions instead of ask so man! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top