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()
The problem is your where clause. See here for how to specify a query in Python, especially the last example: Specifying a query in Python—ArcPy Get Started | ArcGIS Desktop
e.g.
whereclause = """{} = 2""".format(arcpy.AddFieldDelimiters(fc, fieldname))
Select analysis has a code snippet and links to formulating sql queries that will work. Your query lacks the proper quotation sequence
Thank you for the quick responses.
I will work on this, at first glance, I need brackets and/or add field delimiter, etc. I am taking away from this that my sql clause needs work.
I believe you can just use this for your case:
whereClause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass, 'FACILITYID'),InputValue)
Does it need 'FACILITYID' since it is referenced in InputValue ?
I tried this line and still having difficulty.
Oh, right. It should be like below so that all three input variables are dynamic.
whereClause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass, InputField),InputValue)
I get the following message, don't know what is wrong.
Executing: QueryMxdFeatureClass "SWR Layers\Manholes" FACILITYID 5564
Start Time: Mon Feb 06 15:41:32 2017
Running script QueryMxdFeatureClass...
Failed script QueryMxdFeatureClass...
SyntaxError: invalid syntax (QueryMxdFeatureClass.py, line 19)
Failed to execute (QueryMxdFeatureClass).
Failed at Mon Feb 06 15:41:33 2017 (Elapsed Time: 0.67 seconds)
Here is my updated syntax
#Import Modules
import os,arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
# Set the parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)
whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField,InputValue)
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereclause)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
Can you put in some print statements so that the exact format can be examined. If the input value is a number there is one format, if the input value there is another... plus Darren updated his where clause with a syntax different thatn you provided on line 10
Oh didn't catch that. I changed it. I am unable to use print statements to test it out since I am running it as a tool which doesn't return print statements.
whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField),InputValue)