I have a function which a part shown below. The problem is that I get a Run-time error 91 (object variable or with variable not set) and the debug highlights the last line of the code ( Set pFeatureClass = pFeatureLayer.FeatureClass) shown below while the layer file exist on the table of contents. The strange thing is that when I use this function by giving directly the arguments as shown in the example with asterisks below it works fine but when the function is used within a loop in a procedure it presents that error 91.
Could you help me please?
Public Function GetAveX(BlockID As String, Solindex1 As String, Solindex2 As String _
, MPindex1 As String, MPindex2 As String, ParcelID As Long) As Double
'Get the current map document
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
'Get the active map (data frame)
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap
' Get the proper layer
Dim pLayers As IEnumLayer
Set pLayers = pMap.Layers
Dim pLayer As ILayer
Set pLayer = pLayers.Next
Do Until pLayer Is Nothing
If pLayer.Name = "VectorB" & BlockID & "MP" & "_" & Solindex1 & "_" & MPindex1 Then
Exit Do
End If
Set pLayer = pLayers.Next
Loop
'Get the feature class
Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = pLayer
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureLayer.FeatureClass
'rest of the code
....
'**********An example that works fine
Dim i As Double
i = GetAveX(25, 27, 36, 19, 20, 45)
MsgBox i
pLayer is Nothing, then drop out of the loop. Then youSet pFeatureLayer = pLayer, which is stillNothing. Finally, you request the.FeatureClassofpFeatureLayer, which--guess what!--is stillNothing. Making a request ofNothingis an error. This behavior must have occurred because the conditional statement inside the loop was never true. – whuber♦ Jan 10 '12 at 14:52