Solved! Go to Solution.
I've tried this ....def onEnter(self): mxd = arcpy.mapping.MapDocument("CURRENT") df = mxd.activeDataFrame Lots = arcpy.mapping.ListLayers(mxd, "Lots", df) plan = SearchPlanNumber_1.value query = '\"PlanNumber\" = \'' + str(plan) + '\'' print plan print query arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query) df.zoomToSelectedFeatures for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name == "LotSelection": lyr.definitionQuery = '"PlanNumber" = \'' + plan + "'" arcpy.RefreshActiveView() arcpy.RefreshTOC()
If I comment out the SelectByAttribute and df.zoomToSelectedFeatures, I can manipulate the definition query. However, It looks like there is something wrong with the where clause in the SelectByAttribute tool. It fails with the following error ...Traceback (most recent call last): File "C:\Users\dchaboya\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{7657B98A-A37F-CA5E-A8E1-C6CEF093100B}\SearchPlanNumber_addin.py", line 32, in onEnter arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query) File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute raise e RuntimeError: Object: Error in executing tool
Both print statements are returning the values that i'm expecting. For example, if I type in VIP88129 in the combo box, the python window in ArcMap returns VIP88129. Also, it returns the query with the syntanx that I believe should work in the SelectByAttribute tool, which is "PlanNumber" = 'VIP88129'. Not sure what's going on there.
Lots = arcpy.mapping.ListLayers(mxd, "Lots", df)
for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name == "Lots": lyr.definitionQuery = '"PlanNumber" = \'' + plan + "'" arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query)
I have used onEditChange for capturing the type-in input
and then used an OK button to start the processing.
def onEnter(self): mxd = arcpy.mapping.MapDocument("CURRENT") df = mxd.activeDataFrame Lots = arcpy.mapping.ListLayers(mxd, "Lots", df) plan = SearchPlanNumber_1.value query = '\"PlanNumber\" = \'' + str(plan) + '\'' print plan print query arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query) df.zoomToSelectedFeatures for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name == "LotSelection": lyr.definitionQuery = '"PlanNumber" = \'' + plan + "'" arcpy.RefreshActiveView() arcpy.RefreshTOC()
Traceback (most recent call last): File "C:\Users\dchaboya\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{7657B98A-A37F-CA5E-A8E1-C6CEF093100B}\SearchPlanNumber_addin.py", line 32, in onEnter arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query) File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute raise e RuntimeError: Object: Error in executing tool
def onEditChange(self, text):
print query
Paste the result of the line:print query
"PlanNumber" = 'VIP88129'
I've tried this ....def onEnter(self): mxd = arcpy.mapping.MapDocument("CURRENT") df = mxd.activeDataFrame Lots = arcpy.mapping.ListLayers(mxd, "Lots", df) plan = SearchPlanNumber_1.value query = '\"PlanNumber\" = \'' + str(plan) + '\'' print plan print query arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query) df.zoomToSelectedFeatures for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name == "LotSelection": lyr.definitionQuery = '"PlanNumber" = \'' + plan + "'" arcpy.RefreshActiveView() arcpy.RefreshTOC()
If I comment out the SelectByAttribute and df.zoomToSelectedFeatures, I can manipulate the definition query. However, It looks like there is something wrong with the where clause in the SelectByAttribute tool. It fails with the following error ...Traceback (most recent call last): File "C:\Users\dchaboya\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{7657B98A-A37F-CA5E-A8E1-C6CEF093100B}\SearchPlanNumber_addin.py", line 32, in onEnter arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query) File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute raise e RuntimeError: Object: Error in executing tool
Both print statements are returning the values that i'm expecting. For example, if I type in VIP88129 in the combo box, the python window in ArcMap returns VIP88129. Also, it returns the query with the syntanx that I believe should work in the SelectByAttribute tool, which is "PlanNumber" = 'VIP88129'. Not sure what's going on there.
Lots = arcpy.mapping.ListLayers(mxd, "Lots", df)
for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name == "Lots": lyr.definitionQuery = '"PlanNumber" = \'' + plan + "'" arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query)
I think your error when attempting to execute the SelectByAttributes is because of the way you are setting the "Lots" layer. If you say the definition query is working correctly, then use the same process to set the layer:
Instead of:Lots = arcpy.mapping.ListLayers(mxd, "Lots", df)
Use:for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name == "Lots": lyr.definitionQuery = '"PlanNumber" = \'' + plan + "'" arcpy.SelectLayerByAttribute_management(Lots, "NEW_SELECTION", query)
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", query)