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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subscript out of range....

Status
Not open for further replies.

homesick

Programmer
Dec 5, 2001
55
0
0
CA
I have a function that totals columns from a grid and displays the amounts in a textbox....it works great when there are records found but if the user clicks on the "total" button with no records i get the error "subscript out of range"...i want it to display "0".....i tried changing....For R = 0 To grid.Rows -1 to For R= 0 To -1..it displays a "0" when no records are found but it doesn't help......can anyone help......here is what i have....



Private Function TotalColumn(grid As MSFlexGrid, ByVal ColIndex As Long) As Long
Dim R As Long
Dim total As Long

For R = 0 To grid.Rows - 1
If IsNumeric(grid.TextMatrix(R, ColIndex)) Then
total = total + (grid.TextMatrix(R, ColIndex))
End If
Next R

TotalColumn = total

End Function
 
Try something like:
if grid.rows <> 0 then
put your loop in here
else
total = 0
end if
Durkin
 
What is the value of ColIndex when the error is produced.

I don't think the problem is with your for loop.

If colIndex is a number greater than the number of columns in your grid, you'll definitly receive a &quot;Subscript out of Range&quot; error.

Remember that the first row is row 0 and the first col is col 0. If you have 4 columns and are trying to refer to col4 that would be a bad thing.

Good Luck!
Josh
 
My ColIndexes are all fine.....when i have... For R = 0 To grid.Rows - 1....it sums fine but will give me the error when there are no records showing in the grid.....when i change it to... For R = 0 To grid.Rows 0 - 1...it shows &quot;0&quot; when there are no records displayed in the grid....but now it won't sum.....confused.......
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top