Any ideas why I am having problems by trying to have user input a string value for a feature, then have the feature selected.
#Import Modules
import os,arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
#os.chdir(r'C:\Users\dunderwood\Documents\MY_TEMPLATES\WASTEWATER_EDIT_TEMPLATE')
# Set the parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)
whereClause = FACILITYID == InputValue"
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereClause)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
arcpy.AddMessage("Same as print when running tools") # for print equivalent in arctoolbox
Thanks. What messages would I want to troubleshoot my syntax ?
Your where clause is ever-so-slightly different from mine - missing a ')' after InputField.
I have the following yet it is not working. Something still wrong with the expression.
Does it matter whether it is a .sde feature class .or file geodatabase feature ?
#Import Modules
import os,arcpy
#Set to current mxd and dataframe
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
# Set the tool parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)
# SQL expression used in selecting a feature
whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField),InputValue)
#Select feature by facilityid (InputValue)
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereclause)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
Try passing the full path to your feature class for InputFeatureClass. Otherwise, what's the exact error message?
I placed the full path and still no luck. Here is the following error message.
Traceback (most recent call last):
File "C:\Users\...Scripts\QueryMxdFeatureClass.py", line 18, in <module>
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereclause)
File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute
raise e
ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).
Failed to execute (QueryMxdFeatureClass).
Failed at Tue Feb 07 09:06:24 2017 (Elapsed Time: 0.47 seconds)
What do you get when you print the whereclause?
arcpy.AddMessage(whereclause)
Interesting, I used arcpy.AddMessage(whereclause) and get the following results.
I am assuming that it successfully received the Inputvalue., but can't select the feature.
Executing: QueryMxdFeatureClass "SWR Layers\Manholes" FACILITYID 5564
Start Time: Tue Feb 07 09:19:10 2017
Running script QueryMxdFeatureClass...
"FACILITYID" = 5564
Failed script QueryMxdFeatureClass...
Can you try moving the "Manholes" layer outside of the "SWR Layers" group and try it just as "Manholes"?
I removed it from the group and same error.