Error Handling for FindLayerByName

320
1
04-22-2010 12:21 AM
PriyankaMehta
New Contributor
Hi all,
   Need help with some debugging. I m using this FindLayerByName function to set my layer with its name.
Just in case, this layer is not available in TOC, how do i handle that error ?

sub()
Set pFeatLayer = FindLayerByName(pMap, "Road1")
Set pDispTable = pFeatLayer
Set pFCLayer = pDispTable.DisplayTable
Set pTLayer = pFCLayer

End Sub


Function FindLayerByName(pMap As IMap, sName As String) As ILayer

Dim i As Integer
For i = 0 To pMap.LayerCount - 1
  If pMap.Layer(i).name = sName Then
  Set FindLayerByName = pMap.Layer(i)
   End If
Next

End Function


Regards,
Priyanka
0 Kudos
1 Reply
AndrewMay
New Contributor
If FindLayerByName doesn't find a matching layer then it will return Nothing.  So you just need to check for this after calling the function:

if not pFeatLayer = nothing then
    Set pDispTable = pFeatLayer
    Set pFCLayer = pDispTable.DisplayTable
    Set pTLayer = pFCLayer
else
    ' No matching layer
end if

Regards
0 Kudos