<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Looping through a definition query in data driven pages in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97604#M7583</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much for the Clean Code and comments as someone new to Python and Arcmap this really helps me understand whats going on. Thanks again I appreciate it. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 21 Apr 2015 17:43:52 GMT</pubDate>
    <dc:creator>KendrickSchuett</dc:creator>
    <dc:date>2015-04-21T17:43:52Z</dc:date>
    <item>
      <title>Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97593#M7572</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm hoping someone can help me with this one; I've read every post I could find and I'm still stuck. I've got a series of data driven pages that I can get to export with no problem, but I also need to update a mask layer while I'm exporting each page. The ddp name is based on state names and the mask layer is also based on state name. I've got a simple definition query that I use to turn the mask layer on and off for each state. For example, the current ddp is Arizona so the definition query on the mask layer would be "STATE_NAME" &amp;lt;&amp;gt; 'Arizona'. This turns the mask layer off for Arizona while leaving the surrounding states grayed out. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the script that I have so far:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy, sys, os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.OverWriteOutput = 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;print "Enter save as pdf location:"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;pdfDir = arcpy.GetParameterAsText(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;outputFolder = pdfDir + r"\PDFs"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;maskLayer = arcpy.mapping.ListLayers(mxd, "state_bnd110", df)[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;maskField = "STATE_NAME"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.currentPageID = pageNum&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pageName = mxd.dataDrivenPages.pageRow.STATE_NAME&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == maskLayer:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = '"STATE_NAME" &amp;lt;&amp;gt; pageName'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.ExportToPDF(mxd, os.path.dirname(outputFolder)+ os.sep + pageName + ".pdf")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = ""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del mxd&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.GetMessages()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I take out the layer loop the script will export all the ddp with no trouble (and with no mask symbology). As the script is now, it will export the first state (Arizona) with all the states grayed out (for this page, Arizona should not be) then it will export Arizona again and again.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help would be greatly appreciated!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Karla&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Mar 2012 22:09:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97593#M7572</guid>
      <dc:creator>KarlaMayne</dc:creator>
      <dc:date>2012-03-20T22:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97594#M7573</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your loops and indentation are important in this case, read this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code"&gt;http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Mar 2012 12:54:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97594#M7573</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-21T12:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97595#M7574</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you for replying with that information!&amp;nbsp; I couldn't figure out how to get my script to post with the indentations intact.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here it is again, complete with indentations:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, sys, os
arcpy.OverWriteOutput = 1
mxd = arcpy.mapping.MapDocument("CURRENT")

print "Enter save as pdf location:"
pdfDir = arcpy.GetParameterAsText(0)
outputFolder = pdfDir + r"\PDFs"

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
maskLayer = arcpy.mapping.ListLayers(mxd, "state_bnd110", df)[0]
maskField = "STATE_NAME"

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.currentPageID = pageNum
&amp;nbsp;&amp;nbsp;&amp;nbsp; pageName = mxd.dataDrivenPages.pageRow.STATE_NAME
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == maskLayer:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = '"STATE_NAME" &amp;lt;&amp;gt; pageName'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.ExportToPDF(mxd, os.path.dirname(outputFolder)+ os.sep + pageName + ".pdf")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = ""
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()

del mxd
arcpy.GetMessages()
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:06:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97595#M7574</guid>
      <dc:creator>KarlaMayne</dc:creator>
      <dc:date>2021-12-11T06:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97596#M7575</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your main problem is most likely your query. Try this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;'"STATE_NAME" &amp;lt;&amp;gt; %s' % pageName&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, that is the wrong format for the overwrite ouput. Use this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.env.overwriteOutput = True&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Mar 2012 15:07:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97596#M7575</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-21T15:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97597#M7576</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the suggestion, that solved the problem of exporting the ddp with the mask symbology.&amp;nbsp; Unfortunately, it's still looping through the same state (the first one, Arizona) over and over again without moving on to the next page in the ddp.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Mar 2012 16:15:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97597#M7576</guid>
      <dc:creator>KarlaMayne</dc:creator>
      <dc:date>2012-03-21T16:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97598#M7577</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try moving your export back one, so it is under the pageNum loop instead of the layer loop. You may want the activeview refresh before the export as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.currentPageID = pageNum
&amp;nbsp;&amp;nbsp;&amp;nbsp; pageName = mxd.dataDrivenPages.pageRow.STATE_NAME
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == maskLayer:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = '"STATE_NAME" &amp;lt;&amp;gt; %s' % pageName
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.ExportToPDF(mxd, os.path.dirname(outputFolder)+ os.sep + pageName + ".pdf")
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = ""
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:06:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97598#M7577</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T06:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97599#M7578</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Okay, I gave that a try and now I'm getting a Runtime Error after it exports the first ddp.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;type 'exceptions.RuntimeError'&amp;gt;: LayerObject: Set attribute definitionQuery does not exist&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Mar 2012 16:55:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97599#M7578</guid>
      <dc:creator>KarlaMayne</dc:creator>
      <dc:date>2012-03-21T16:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97600#M7579</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Delete the last lyr definition query in your script after the pdf export.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Mar 2012 17:20:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97600#M7579</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-21T17:20:01Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97601#M7580</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is clearly very late:&amp;nbsp; but when you have DDP enabled, you then get a special definition query button.&amp;nbsp; In properties where you would normally got to set your query builder, there is now a "page definition" button.&amp;nbsp; you can specify that a lyr match or don't match the index layer name.&amp;nbsp; if i understand correctly this is what you were trying to get your code to do.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Apr 2012 19:42:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97601#M7580</guid>
      <dc:creator>Lynn415</dc:creator>
      <dc:date>2012-04-11T19:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97602#M7581</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I just had great success with the following edits to this code. Thanks for sharing it. I also was able to change it to export JPEGs.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm attaching my Python script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#DDP: Set Definition Query on a secondary index 
 #Variables that need to be set by user:
 #mxdpath = r'C:\Users\Test.mxd' # string
 #outputfolder = 'PDFFolder' # string
 #namefield = 'THE FIELD CONTAINING THE NAME OF THE RESULTING PDF FILE' #string
 #queryfield = 'THE FIELD CONTAINING THE VALUE FOR THE DESIRED QUERY' #string
 #layers = ['LIST','OF','LAYERS'] # list ('Layer1_UpdateQuery', 'Layer2_UpdateQuery', 'etc')

#Import Modules
import arcpy, os

# Set Variables
mxdpath = r'D:\arcgis\Samples\DDP_Index2.mxd' # string
outputfolder = 'D:\PDF' # string
namefield = 'FIPS' # string
queryfield = 'STATE_NAME' # string
layers = ['CountySel_FIPS'] # list

mxd = arcpy.mapping.MapDocument(mxdpath)
# Start Loop through Data Driven Pages

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.currentPageID = pageNum # set the current page
&amp;nbsp;&amp;nbsp;&amp;nbsp; pageName = mxd.dataDrivenPages.pageRow.getValue(namefield) # get the pageName
&amp;nbsp;&amp;nbsp;&amp;nbsp; pageName += '.pdf' # add '.pdf' extension for file name
&amp;nbsp;&amp;nbsp;&amp;nbsp; pdf = os.path.join(outputfolder, pageName) # add pageName to output folder to get full output path
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Start Loop through all layers of mxd file

&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd): # for every layer in mxd's Layer List
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name in layers: # if layer name in user input list above
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #value = mxd.DataDrivenPages.pageRow.getValue(queryfield) # get value of query field
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = mxd.dataDrivenPages.pageRow.getValue(queryfield)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.definitionQuery == True: # if a definitionQuery Exsts...
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = lyr.definitionQuery + "AND" + queryfield + "=" + "\"" + value + "\"" # Add new to old
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = queryfield + "=" + "\"" + value + "\"" # else create a new query
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = query # set the layers defintionQuery
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Next' # else print next, this isn't necassary, but its a good place holder

&amp;nbsp;&amp;nbsp;&amp;nbsp; print pageName
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.ExportToPDF(mxd, pdf) # export the current Page to pdf
del mxd, lyr, pageName, pageNum, query, value
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:07:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97602#M7581</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T06:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97603#M7582</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for sharing this.&amp;nbsp; It is just what I was looking for.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 05 Sep 2014 00:01:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97603#M7582</guid>
      <dc:creator>JanetRogers</dc:creator>
      <dc:date>2014-09-05T00:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97604#M7583</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much for the Clean Code and comments as someone new to Python and Arcmap this really helps me understand whats going on. Thanks again I appreciate it. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Apr 2015 17:43:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97604#M7583</guid>
      <dc:creator>KendrickSchuett</dc:creator>
      <dc:date>2015-04-21T17:43:52Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through a definition query in data driven pages</title>
      <link>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97605#M7584</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I agree that is incredibly helpful for folks to post their final code.&amp;nbsp; Thanks for that.&amp;nbsp; I do have a question about line-38.&amp;nbsp; Dont you want "lyr.definitionQuery = query # set the layers defintionQuery&amp;nbsp; " to be outside of the 'else'?&amp;nbsp; You want to set the def query to the variable 'query' either way, and NOT just in the case of the else, right?&amp;nbsp; I cant say for sure, because I am elementary with my python.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Jun 2015 13:14:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looping-through-a-definition-query-in-data-driven/m-p/97605#M7584</guid>
      <dc:creator>RustyRex</dc:creator>
      <dc:date>2015-06-03T13:14:54Z</dc:date>
    </item>
  </channel>
</rss>

