Use user input in Combo Box to perform a Definition Query

687
7
Jump to solution
06-06-2013 11:07 AM
danielchaboya
New Contributor III
I'm nut sure which function (i'm assuming onEnter) to use and how to write the code to be able to get a users input ( type it in the text box of the combo box), hit enter and have it performe a definition query on a layer.  I can accomplish this by using a search cursor and populating the combo box with the returned values, but i prefer no to do this as returns a rather large list.  Any thoughts?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
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.


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)

View solution in original post

0 Kudos
7 Replies
markdenil
Occasional Contributor III
I have used onEditChange for capturing the type-in input
and then used an OK button to start the processing.
0 Kudos
danielchaboya
New Contributor III
I have used onEditChange for capturing the type-in input
and then used an OK button to start the processing.


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.
0 Kudos
markdenil
Occasional Contributor III
I would suggest setting a queryVal variable to some default value ("") at the start of the toolbar script.

Next, in the onEditChange event handler for the combo box,
declare the queryVal variable as global, and set queryVal to the combobox text.
    def onEditChange(self, text):

        global queryVal
        queryVal = str(text)[\CODE]

Now the queryVal vaiable is available for use in the onClick handler
for an Apply button.
This is where you would build the query string using queryVal as the value,
and pass it to the geoprocessing tool (and apply the definitionQuery).

Doing it this way also gives the user a moment to inspect what she has typed,
before the (possibly nonsensical) value is applied as a query....


rereading your post, I see your concern is now with the query not working... oops.
Maybe breaking out the value setting and value applying, as I suggest, would help make the issue clearer.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Paste the result of the line:

print query
0 Kudos
danielchaboya
New Contributor III
Paste the result of the line:

print query


This is what is returned ...
"PlanNumber" = 'VIP88129'
0 Kudos
JamesCrandall
MVP Frequent Contributor
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.


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)
0 Kudos
danielchaboya
New Contributor III
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)


Thanks James, that did the trick. One thing, I had to change the name of the input dataset to this ...

arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", query)


as the variable Lots is no longer being referenced. 

Thanks again.
0 Kudos