Hi,I am having problem with my code I am trying to use zoom Modules in my project, BUT after I run the code, it goes directly to the layer but with deleting all the information in Attribute table?here is my code ?Option Explicit Private pMxDoc As IMxDocument Private pFLayer As IFeatureLayer Private pEnumLayer As IEnumLayer Private pCurrentLayer As IFeatureLayer Private pFClass As IFeatureClass Private pFeature As IFeature Public Sub ZoomToSDnumber(SDnumberId As String) Set pMxDoc = ThisDocument Set pFLayer = pMxDoc.FocusMap.Layer(0) Set pFClass = pFLayer.FeatureClass Dim pEnv As IEnvelope Dim pActView As IActiveView Dim pFCursor As IFeatureCursor Dim pQFilt As IQueryFilter Dim queryStr As String queryStr = "SD_NUMBER = '" & SDnumberId & "'" Set pActView = pMxDoc.ActiveView Set pQFilt = New QueryFilter pQFilt.WhereClause = queryStr Set pFCursor = pFClass.Search(pQFilt, True) Set pFeature = pFCursor.NextFeature If pFeature Is Nothing Then MsgBox "Check spelling and case", vbCritical + vbExclamation, "State Not Found!" Else pActView.Extent = pFeature.Shape.Envelope Set pEnv = pActView.Extent pEnv.Expand 1.1, 1.1, True pActView.Extent = pEnv End If Dim flDef As IFeatureLayerDefinition Set flDef = pFLayer flDef.DefinitionExpression = "SD_NUMBER='" & SDnumberId & "'" pActView.Refresh pMxDoc.UpdateContents End Sub Public Function CreateSelLayer(SDnumberId As String) Set pMxDoc = ThisDocument Set pFLayer = pMxDoc.FocusMap.Layer(0) ''** Make a selection on the FeatureLayer Dim queryStr As String Dim qF As IQueryFilter Set qF = New QueryFilter queryStr = "SD_NUMBER='" & SDnumberId & "'" qF.WhereClause = queryStr Dim pFSel As IFeatureSelection Set pFSel = pFLayer pFSel.SelectFeatures qF, esriSelectionResultNew, False Dim pFDef As IFeatureLayerDefinition Set pFDef = pFLayer Dim SelFeatLayer As IFeatureLayer Set SelFeatLayer = pFDef.CreateSelectionLayer(StateId, True, "", "") With pMxDoc .AddLayer SelFeatLayer .FocusMap.MoveLayer SelFeatLayer, 2 .FocusMap.ClearSelection .ActiveView.Refresh .UpdateContents End With End Function
Option Explicit Private pMxDoc As IMxDocument Private pFLayer As IFeatureLayer Private pEnumLayer As IEnumLayer Private pCurrentLayer As IFeatureLayer Private pFClass As IFeatureClass Private pFeature As IFeature Public Sub ZoomToSDnumber(SDnumberId As String) Set pMxDoc = ThisDocument Set pFLayer = pMxDoc.FocusMap.Layer(0) Set pFClass = pFLayer.FeatureClass Dim pEnv As IEnvelope Dim pActView As IActiveView Dim pFCursor As IFeatureCursor Dim pQFilt As IQueryFilter Dim queryStr As String queryStr = "SD_NUMBER = '" & SDnumberId & "'" Set pActView = pMxDoc.ActiveView Set pQFilt = New QueryFilter pQFilt.WhereClause = queryStr Set pFCursor = pFClass.Search(pQFilt, True) Set pFeature = pFCursor.NextFeature If pFeature Is Nothing Then MsgBox "Check spelling and case", vbCritical + vbExclamation, "State Not Found!" Else pActView.Extent = pFeature.Shape.Envelope Set pEnv = pActView.Extent pEnv.Expand 1.1, 1.1, True pActView.Extent = pEnv End If Dim flDef As IFeatureLayerDefinition Set flDef = pFLayer flDef.DefinitionExpression = "SD_NUMBER='" & SDnumberId & "'" pActView.Refresh pMxDoc.UpdateContents End Sub Public Function CreateSelLayer(SDnumberId As String) Set pMxDoc = ThisDocument Set pFLayer = pMxDoc.FocusMap.Layer(0) ''** Make a selection on the FeatureLayer Dim queryStr As String Dim qF As IQueryFilter Set qF = New QueryFilter queryStr = "SD_NUMBER='" & SDnumberId & "'" qF.WhereClause = queryStr Dim pFSel As IFeatureSelection Set pFSel = pFLayer pFSel.SelectFeatures qF, esriSelectionResultNew, False Dim pFDef As IFeatureLayerDefinition Set pFDef = pFLayer Dim SelFeatLayer As IFeatureLayer Set SelFeatLayer = pFDef.CreateSelectionLayer(StateId, True, "", "") With pMxDoc .AddLayer SelFeatLayer .FocusMap.MoveLayer SelFeatLayer, 2 .FocusMap.ClearSelection .ActiveView.Refresh .UpdateContents End With End Function
I followed an example from an arcobjects' book how to create selection layer, but I need to modify in part of query and zoom to feature, when I got stuck in query, I ask to this forum by the title confusing query filter. Now, the problem is solved. Thanks Richard, Thank you so much..Regards,Amie
Option Explicit Public Sub ZoomToLandUnit(ByVal strLandUnit As String, ByVal strKabupaten As String, ByVal bKabupaten As Boolean) Dim pDocument As IMxDocument Set pDocument = ThisDocument Dim pMap As IMap Set pMap = pDocument.FocusMap Dim pLayer As ILayer Dim i As Long For i = 0 To pMap.LayerCount - 1 If pMap.Layer(i).Name = "Soil" Then Set pLayer = pMap.Layer(i) End If Next i If pLayer Is Nothing Then Exit Sub 'set up the selection Dim pFeatLayer As IFeatureLayer Set pFeatLayer = pLayer Dim pFeatDef As IFeatureLayerDefinition Set pFeatDef = pFeatLayer pFDef.DefinitionExpression = "" ' Clear any definition query so your feature selection query is not blocked. Dim pFeatSelection As IFeatureSelection Set pFeatSelection = pFeatLayer Dim strWhereClause As String If bKabupaten Then strWhereClause = "Land_Unit = '" & strLandUnit & "' And KABUPATEN = '" & strKabupaten & "'" Else strWhereClause = "Land_Unit = " & "'" & strLandUnit & "'" End If Dim pQueryFilter As IQueryFilter Set pQueryFilter = New queryFilter pQueryFilter.WhereClause = strWhereClause pFeatSelection.Clear pFeatSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False 'access the feature Dim pSelectionSet As ISelectionSet Set pSelectionSet = pFeatSelection.selectionSet If pSelectionSet.Count = 0 Then MsgBox " The expression was verified successfully, but no records were returned", vbInformation Unload frmCreateLayout Exit Sub End If 'zoom to all selected features Dim pEnumGeom As IEnumGeometry Dim pEnumGeomBind As IEnumGeometryBind Set pEnumGeom = New EnumFeatureGeometry Set pEnumGeomBind = pEnumGeom pEnumGeomBind.BindGeometrySource Nothing, pSelectionSet Dim pGeomFactory As IGeometryFactory Set pGeomFactory = New GeometryEnvironment Dim pGeom As IGeometry Set pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom) 'update the extent of the map to match the extent of the selected features Dim pActiveView As IActiveView Set pActiveView = pMap pActiveView.Extent = pGeom.Envelope pActiveView.Refresh ' Do you mean to do this code or do you intend to use the other Sub to do this? pFDef.DefinitionExpression = strWhereClause ' Do you really want this line to store the definition query on the Soils layer so that it does not display all features? Dim SelFeatLayer As IFeatureLayer Set SelFeatLayer = pFDef.CreateSelectionLayer(strLandUnit, True, "", "") With pDocument .AddLayer SelFeatLayer .FocusMap.MoveLayer SelFeatLayer, 2 ' .FocusMap.ClearSelection ' Not sure if you want to Clear the Selection. Remove the first comment quote if you do. .activeView.Refresh .UpdateContents End With End Sub ' This Is not a Function, since it does not return anything. ' THis is a Sub and it is conflicts with the prior Sub unless it is called by the prior Sub only so that it always matches the input variables. Public Sub CreateSelLayer(ByVal strLandUnit As String, ByVal strKabupaten As String, ByVal bKabupaten As Boolean) Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument Dim pMap As IMap Set pMap = pMxDoc.FocusMap Dim pLayer As ILayer Dim i As Long For i = 0 To pMap.LayerCount - 1 If pMap.Layer(i).Name = "Soil" Then Set pLayer = pMap.Layer(i) End If Next i If pLayer Is Nothing Then Exit Function Dim pFLayer As IFeatureLayer Set pFLayer = pLayer Dim pFDef As IFeatureLayerDefinition Set pFDef = pFLayer pFDef.DefinitionExpression = "" ' Clear any definition query so your feature selection query is not blocked ' or else delete the above line if this Sub is always called by the other sub and always matches its inputs. 'Make a selection on the FeatureLayer Dim pFeatSelection As IFeatureSelection Set pFeatSelection = pFLayer Dim pQueryFilter As IQueryFilter Set pQueryFilter = New queryFilter If bKabupaten Then pQueryFilter.WhereClause = "Land_Unit = '" & strLandUnit & "' And KABUPATEN = '" & strKabupaten & "'" Else pQueryFilter.WhereClause = "Land_Unit = " & "'" & strLandUnit & "'" End If pFeatSelection.Clear pFeatSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False Dim SelFeatLayer As IFeatureLayer Set SelFeatLayer = pFDef.CreateSelectionLayer(strLandUnit, True, "", "") With pMxDoc .AddLayer SelFeatLayer .FocusMap.MoveLayer SelFeatLayer, 2 .FocusMap.ClearSelection .activeView.Refresh .UpdateContents End With End Sub
Thanks Richard,No more error from the compiler now, but when I run for the second time it goes to the message box. I open the attribute table and I see only the records of the previous land unit I�??ve chosen, then I compare with the attribute table from the example. The difference is the records from the example attribute table always change every running and mine don�??t. I�??ve also change this line:Set SelFeatLayer = pFeatDef.CreateSelectionLayer(Soil, True, "", "")To:Set SelFeatLayer = pFeatDef.CreateSelectionLayer(strLandUnit, True, "", "")But still have no change. Please more suggestion. Thanks a lot Richard..Amie
Public Sub ZoomToLandUnit(byVal strLandUnit As String, byVal strKabupaten as String, byVal bKabupaten as Boolean) Dim pDocument As IMxDocument Set pDocument = ThisDocument Dim pMap As IMap Set pMap = pDocument.FocusMap Dim pFeatLayer As IFeatureLayer Dim pFeatClass As IFeatureClass Dim pLayer As ILayer Dim i As Long For i = 0 To pMap.LayerCount - 1 If pMap.Layer(i).Name = "Soil" Then Set pLayer = pMap.Layer(i) End If Next i If pLayer Is Nothing Then Exit Sub Dim pFeatLayerDef As IFeatureLayerDefinition ' Remove the duplication of this line that appears lower in the code. Set pFeatLayerDef = pLayer pFeatLayerDef.DefinitionExpression = "" ' Etc.
Private Sub cmdNewLayer_Click() Call ZoomTasks.ZoomToLandUnit(cboSatuanLahan.Text, cboKabupaten.Text, cboKabupaten.Visible) �??Assumes this command Button can see the comboboxes. End Sub Private Sub UserForm_Initialize() Dim strFile1 As String strFile1 = "D:\AMIE\BELAJAR\Belajar ArcObjects\LandUnit.txt" Dim strLandUnit As String Open strFile1 For Input As #1 Do Until EOF(1) Input #1, strLandUnit cboSatuanLahan.AddItem strLandUnit Loop Close #1 cboSatuanLahan.Value = "-Land Unit-" Dim strFile2 As String strFile2 = "D:\AMIE\BELAJAR\Belajar ArcObjects\Kabupaten.txt" Dim strKabupaten As String Open strFile2 For Input As #2 Do Until EOF(2) Input #2, strKabupaten cboKabupaten.AddItem strKabupaten Loop Close #2 cboKabupaten.Value = "-Kabupaten-" End Sub Public Sub ZoomToLandUnit(byVal strLandUnit As String, byVal strKabupaten as String, byVal bKabupaten as Boolean) Dim pDocument As IMxDocument Set pDocument = ThisDocument Dim pMap As IMap Set pMap = pDocument.FocusMap Dim pFeatLayer As IFeatureLayer Dim pFeatClass As IFeatureClass Dim pLayer As ILayer Dim i As Long For i = 0 To pMap.LayerCount - 1 If pMap.Layer(i).Name = "Soil" Then Set pLayer = pMap.Layer(i) End If Next i If pLayer Is Nothing Then Exit Sub 'set up the selection Dim pFeatSelection As IFeatureSelection Set pFeatSelection = pLayer Dim pQueryFilter As IQueryFilter Set pQueryFilter = New queryFilter If bKabupaten Then pQueryFilter.WhereClause = "Land_Unit = '" & strLandUnit & "' And KABUPATEN = '" & strKabupaten & "'" Else pQueryFilter.WhereClause = "Land_Unit = " & "'" & strLandUnit & "'" End If pFeatSelection.Clear pFeatSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False 'access the feature Dim pSelectionSet As ISelectionSet Set pSelectionSet = pFeatSelection.selectionSet Dim pDataset As IDataset Set pDataset = pFeatClass Set pFeatLayer = pLayer Set pFeatClass = pFeatLayer.FeatureClass If pSelectionSet.Count = 0 Then MsgBox " The expression was verified successfully, but no records were returned", vbInformation Unload Me Exit Sub End If Dim pFeatCursor As IFeatureCursor pSelectionSet.Search Nothing, False, pFeatCursor Dim pFeature As IFeature Set pFeature = pFeatCursor.NextFeature() 'zoom to all features Dim pEnumGeom As IEnumGeometry Dim pEnumGeomBind As IEnumGeometryBind Set pEnumGeom = New EnumFeatureGeometry Set pEnumGeomBind = pEnumGeom pEnumGeomBind.BindGeometrySource Nothing, pSelectionSet Dim pGeomFactory As IGeometryFactory Set pGeomFactory = New GeometryEnvironment Dim pGeom As IGeometry Set pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom) Dim pFeatLayerDef As IFeatureLayerDefinition Set pFeatLayerDef = pLayer If bKabupaten Then pFeatLayerDef.DefinitionExpression = "Land_Unit = '" & strLandUnit & "' And KABUPATEN = '" & strKabupaten & "'" Else pFeatLayerDef.DefinitionExpression = "Land_Unit = " & "'" & strLandUnit & "'" End If 'update the extent of the map to match the extent of the feature Dim pActiveView As IActiveView Set pActiveView = pMap pActiveView.Extent = pGeom.Envelope pActiveView.Refresh Dim pFeatDef As IFeatureLayerDefinition Set pFeatDef = pFeatLayer Dim SelFeatLayer As IFeatureLayer Set SelFeatLayer = pFeatDef.CreateSelectionLayer(Soil, True, "", "") pActiveView.Refresh pDocument.UpdateContents End Sub
Private Sub cmdNewLayer_Click() Call ZoomTasks.ZoomToLandUnit() �??Your Sub has no argument parameters so do not pass any End Sub
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.