Select to view content in your preferred language

I need help on this error message

1115
6
08-07-2012 03:04 PM
OLANIYANOLAKUNLE
Frequent Contributor
I need help on this error message
I was trying to write a custom python script for Definition query for a particular layer and this error message was what i got
NameError: name 'field' is not defined

after entering the following codes -
#Variables to form defintion query
field = '"Name_Allottee"'
value = "'Fadare A Adetoye'"
#concatenate query syntax
queryStr = "Name_Allottee" + "=" + "Fadare A Adetoye"
#Specify the MXD project (CURRENT), dataframe (Layers)
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")
#Apply defintion query to specified layer group (KWAGIS_Parcels)
for lyr in arcpy.mapping.ListLayers(mxd, "KWAGIS_Parcel", df):
if lyr.supports("DEFINITIONQUERY"):
lyr.definitionQuery = queryStr
arcpy.RefreshActiveView()
0 Kudos
6 Replies
BruceNielsen
Frequent Contributor
Try this:
queryStr = "\"Name_Allottee\" = 'Fadare A Adetoye'"

In a query string, field names need to be enclosed in double quotes, and string values in single quotes.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Do you have any python script that can clip the data frame extent to a selected feature, a script different from the clipping under the geoprocesssing tool?

Thanks a bunch
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
the Code:
queryStr = "\"Name_Allottee\" = 'Fadare A Adetoye'" seems not to work with my layer under a parcel fabric i.e. my layer is a parcel layer under a parcel fabric dataset and when i run dis definitionquery for the parcel layer it does not return any results, what would return a result is this sql statement - [Name_Allottee] = 'Fadare A Adetoye', how can i put this into my python script below and make it work?

import arcpy
import pythonaddins
import os

        mxd = arcpy.mapping.MapDocument("Current")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        lyr= arcpy.mapping.ListLayers(mxd,"Parcels")[0]
        lyr.definitionQuery = '"Name_Allot" =\'Fadare A Adetoye\''
        arcpy.RefreshTOC
        arcpy.RefreshActiveView


Thanks - what i want is how to concatenate the lyr.definitionquery for features in a geodatabase
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
mxd = arcpy.mapping.MapDocument("Current")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        lyr= arcpy.mapping.ListLayers(mxd,"Parcels")[0]
        lyr.definitionQuery = '"Name_Allottee" =\'Fadara A Adatoyo\''
        arcpy.RefreshTOC()
        arcpy.RefreshActiveView()
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
i used the following python script to create a script tool to select an attribute of a particular feature, but it returns all the records of the feature specified in the FeatureClass;

import arcpy
arcpy.env.overwriteOutput = True

try:
    # Get the input parameters for the selection tool
    FeatureClass = arcpy.GetParameterAsText(0)
    SQLStatement = arcpy.GetParameterAsText(1)
   
    # Select by Layer Attribute
    arcpy.SelectLayerByAttribute_management(FeatureClass, "NEW_SELECTION", "")


    # Report a success message   
    arcpy.AddMessage("All done!")

except:
    # Report an error messages
    arcpy.AddError("Could not complete the selection")

    # Report any error messages that the selection tool might have generated   
    arcpy.AddMessage(arcpy.GetMessages())

Please any suggestions. Thanks
0 Kudos
BruceNielsen
Frequent Contributor
A SelectLayerByAttribute statement without the query statement is like saying "SELECT ALL FROM...". Since you're accepting a SQL statement as a parameter, I believe you want something like this:
arcpy.SelectLayerByAttribute_management(FeatureClass, "NEW_SELECTION", SQLStatement)
0 Kudos