AnsweredAssumed Answered

Writing a where_clause to Select Layer By Attribute for ArcMap Add-in

Question asked by _d@n.m-l on May 12, 2019
Latest reply on May 13, 2019 by rvburton

I have an add-in script for a toolbox in ArcMap that lets a user select a data frame, a layer within it, a field from the layer, and a value from the field. I'm struggling to write a where_clause for the button class that is to execute the selection. I have global variables for each of the combo boxes, but I'm not unsure of how to translate the global variable for the field value to something usable in the Select By Attribute where_clause.


This is the code for the button class that should execute the selection. layer, field, and valueS are the global variables for the user-chosen layer, field, and value, respectively.


class OK(object):"""Implementation for ok.button (Button)"""def __init__(self):    self.enabled = True    self.checked = Falsedef onClick(self):    mxd = arcpy.mapping.MapDocument("CURRENT")    df = mxd.activeDataFrame     arcpy.MakeFeatureLayer_management(layer, "layerSel")      where_clause = "\"field\"= + '"+ valueS + "'"    arcpy.SelectLayerByAttribute_management("layerSel","NEW_SELECTION",where_clause)    df.zoomToSelectedFeatures()    arcpy.RefreshActiveView()

This error appears when I run the script:

ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

I know it has to do with the where_clause, I'm just now sure how to re-write it. I've read the Select Layer by Attribute article, as well as a few on SQL expressions, and I'm still very lost.

It has been suggested that I use str.format(), and it does seem that would simply things a lot, but, again, I'm not sure how to incorporate it into my code.

I've tried:

where_clause = '{}'.format(valueSelect)
arcpy.SelectLayerByAttribute_management("layerSel","NEW_SELECTION", where_clause)

but the result is the same error.

What would go in the curly braces for this kind of application? 

Is there a better alternative?

I've got a feeling the solution is ridiculously simpler than I realize...

All help will be greatly appreciated!