I am trying to create a button that will execute a Python script. I have limited Python experience. What we are trying to accomplish is to recreate these steps:

  1. Query on project Site and Radius Layers (both in an SDE)
  2. Zoom to the site
  3. Adjust scale to 10,500

I have pasted the code I wrote while trying to figure this all out, see below:

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.scale = 10500

This works, but is not anything that I can't do with ArcMap tools.

  • If it works, what is your question exactly? If it's step 1 that your question is really about, you will want to expand upon that point (what kind of query or queries need to be done, and what user interaction is required?).
    – blah238
    May 15, 2012 at 21:12
  • The user is putting definition queries on a point and polygon feature using the project number, which is in a field named "PID" within the attribute table. This will allow the user to select a layer and zoom to it using the zoom to layer tool. The code above simply zooms to a layer if the user manually defines the query and then selects the row. What would be the correct steps to incorporate the definition query and the selection of the row into a python script? Thanks for your help.
    – Colli
    May 15, 2012 at 21:25

1 Answer 1


Since user interaction is required (choose a layer, enter a number), I would suggest creating a script tool with parameters: one for the layer and another for the ID value. See this answer for a list of parameter types; I would suggest Feature Layer and Long but it depends on exactly what you want to do.

Then in your script, construct a SQL query expression (WHERE clause) from the chosen layer and specified ID value. Pass this, along with the layer name, into SelectLayerByAttribute_management and then continue with your script as-is.

A couple of tips for creating where clauses I always suggest are:

  • Use AddFieldDelimiters to automatically append the DBMS-specific field delimiters (e.g. "FIELD_NAME" for file geodatabases or [FIELD_NAME] for personal geodatabases)
  • Use Python string formatting (new or old styles) to more clearly format the SQL query.
  • If it is possible to enter multiple ID values, you might find the code in this answer helpful (also illustrates the other two suggestions).
  • This is indeed very helpful. Thank you for outlining this for me.
    – Colli
    May 16, 2012 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.