0

I am creating a tool that applies definition query to certain layers of the mxd. The user clicks the button and it should hardwire the definition query into the layer. This is useful if there are many layers you want to apply the same def query onto. When I click the button I get the following error message

Error message

Obviously my code has flaws in it that would make such drastic error message.

Public Sub DefQUpdate_HardWired()
    Try
        Dim pMxd As IMxDocument
        pMxd = My.ArcMap.Application.Document
        Dim pMap As IMap
        pMap = pMxd.FocusMap
        Dim pEnumLayer As IEnumLayer = pMap.Layers
        pEnumLayer.Reset()
        Dim pLayer As ILayer = pEnumLayer.Next

        Dim pFlyr As IFeatureLayer, pFlyrDef As IFeatureLayerDefinition
        pFlyr = Nothing
        pFlyr.FeatureClass = pLayer

        Do While Not pLayer Is Nothing
            If TypeOf pLayer Is IFeatureLayer Then
                pFlyr.Name = pFlyr.FeatureClass.AliasName
                pFlyrDef = pFlyr
                If pFlyr.Name = "UPL" Then
                    pFlyrDef.DefinitionExpression = "LASTEDITED < 'TO_DATE('2013-12-03 09:09:29','YYYY-MM-DD HH24:MI:SS')'" 
                End If
            End If
            pLayer = pEnumLayer.Next
        Loop
        pMxd.ActiveView.Refresh()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
2

Here is your code corrected:

Public Sub DefQUpdate_HardWired()
    Try
        Dim pMxd As IMxDocument
        pMxd = My.ArcMap.Application.Document

        Dim pMap As IMap
        pMap = pMxd.FocusMap

        Dim pEnumLayer As IEnumLayer = pMap.Layers
        pEnumLayer.Reset()
        Dim pLayer As ILayer 
        pLayer= pEnumLayer.Next

        Dim pFlyr As IFeatureLayer
        Dim pFlyrDef As IFeatureLayerDefinition

        Do While Not pLayer Is Nothing
            If TypeOf pLayer Is IFeatureLayer Then
                pFlyr = pLayer                
                If pFlyr.Name = "UPL" Then
                    pFlyrDef = pFlyr
                    pFlyrDef.DefinitionExpression = "LASTEDITED < 'TO_DATE('2013-12-03 09:09:29','YYYY-MM-DD HH24:MI:SS')'" 
                End If
            End If
            pLayer = pEnumLayer.Next
        Loop
        pMxd.ActiveView.Refresh()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.