For loop problem

603
2
Jump to solution
07-16-2012 04:58 AM
DaveCouture
New Contributor III
I'm trying to figure out what is wrong with my For loop. To make it easy, I stripped down the code and this is what it supposed to do:

- Make sure there are Single Tables in TOC (if true, continue)
- Check if the Subdivision Plans table is in TOC.
- If Subdivision Plans table is in TOC, show "Subdivision Plans Table Found" message
- If Subdivision Plans table is not in TOC, show "Subdivision Plans Table Not Found" message

If the Subdivision Plans table is in the TOC, everything works as it should. But, if the Subdivision Plans table isn't found, it goes to the Catch Exception error, instead of the "Subdivision Plans Table Not Found" message. Why?


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            
            ' Variables declarations 
            Dim ANBLS As String = Me.ComboBox1.Text.Trim
            Dim pMap As IMap = My.ArcMap.Document.ActiveView
            Dim queryFilter As String = "FileNumber = '" + ANBLS + "'"
            Dim tableName As String = "CSJGISSDE.GISDATA.Subdivision_Plans_Table"
            Dim pStTabColl As IStandaloneTableCollection
            Dim foundTable As Integer = 0
            pStTabColl = pMap

            ' Check if there are tables are in TOC
            If pStTabColl.StandaloneTableCount <> 0 Then

                ' Check if Subdivision Plans table is in TOC
                For i As Integer = 0 To pStTabColl.StandaloneTableCount
                    If (pStTabColl.StandaloneTable(i).Name = tableName) Then
                        MsgBox("Subdivision Plans Table found")
                        foundTable = 1
                        Exit For
                    End If
                Next i
            Else
                MsgBox("No tables found")
                Return
            End If

            ' Subdivision Plans Table Not found in the TOC
            If foundTable = 0 Then
                MsgBox("Subdivision Plans Table Not found")
                Return
            End If


            ' If all fails...
        Catch ex As Exception
            MsgBox("This action cannot be done. Save your project, then restart ArcMap.")
        End Try
 End Sub
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
For i As Integer = 0 To pStTabColl.StandaloneTableCount

The table index begins at 0, therefore you must stop your loop at Count -1.

For i As Integer = 0 To pStTabColl.StandaloneTableCount -1

View solution in original post

0 Kudos
2 Replies
NeilClemmons
Regular Contributor III
For i As Integer = 0 To pStTabColl.StandaloneTableCount

The table index begins at 0, therefore you must stop your loop at Count -1.

For i As Integer = 0 To pStTabColl.StandaloneTableCount -1
0 Kudos
DaveCouture
New Contributor III
Newbie mistake..haha!  Thanks Neil!!
0 Kudos