My python script in ArcMap has an initial dialog box with two inputs: REG_CODE (values are in a list and include 'AKR', 'IMR', 'NER', etc) and Selection_type which offers 'NEW_SELECTION', 'REMOVE_FROM_SELECTION', etc.
My code runs just fine unless I pick the value 'NER'. Then I get this error:
Traceback (most recent call last): File "C:\Temp\SelectRegionalCREnterpriseData.py", line 99, in selectRelatedRecords(originTable, destinationTable, primaryKeyField, foreignKeyField) File "C:\Temp\SelectRegionalCREnterpriseData.py", line 96, in selectRelatedRecords arcpy.SelectLayerByAttribute_management(destinationTable, selectType, whereClause) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).
I can verify that my layers and tables have a valid field called REG_CODE, that 'NER' does exist as an attribute value, and that there are no associated NULL values in the CR_ID field.
The script does two things... 1) goes through each layer, and selects features where REG_CODE = (say, NER) 2) Finds all records in CR_Catalog table where REG_CODE = and then selects the related records in the CR_Link table.
<SPAN class="kwd" style="color: #00008b;">import</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> os</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> string
</SPAN><SPAN class="kwd" style="color: #00008b;">import</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="pln" style="color: #000000;">mapping </SPAN><SPAN class="kwd" style="color: #00008b;">as</SPAN><SPAN class="pln" style="color: #000000;"> MAP</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /><BR /></SPAN>
<SPAN class="com" style="color: #808080;">#Needed CR layers as well as Catalog and Link tables should be loaded into current mxd</SPAN><SPAN class="pln" style="color: #000000;">
mxd </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> MAP</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">MapDocument</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="str" style="color: #800000;">"CURRENT"</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
df </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> MAP</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">ListDataFrames</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">mxd</SPAN><SPAN class="pun" style="color: #000000;">)[</SPAN><SPAN class="lit" style="color: #800000;">0</SPAN><SPAN class="pun" style="color: #000000;">]</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /></SPAN>
<SPAN class="com" style="color: #808080;">#set Reg_Code value for regional data into a sql expression</SPAN><SPAN class="pln" style="color: #000000;">
fld </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">AddFieldDelimiters</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="pln" style="color: #000000;">env</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="pln" style="color: #000000;">workspace</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">"REG_CODE"</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
sqlExp </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> fld </SPAN><SPAN class="pun" style="color: #000000;">+</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">" = "</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">+</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">" '{0}' "</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="pln" style="color: #000000;">format</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">GetParameterAsText</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="lit" style="color: #800000;">0</SPAN><SPAN class="pun" style="color: #000000;">))</SPAN><SPAN class="pln" style="color: #000000;">
arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">AddMessage</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">sqlExp</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;">#set selection type</SPAN><SPAN class="pln" style="color: #000000;">
selectType </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">GetParameterAsText</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="lit" style="color: #800000;">1</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;"><BR /><BR /><BR /><BR /></SPAN>
<SPAN class="com" style="color: #808080;">#Make lists of all feature layers and another list of all tables in dataframe</SPAN><SPAN class="pln" style="color: #000000;">
layers </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> MAP</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">ListLayers</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">mxd</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">""</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> df</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
tables </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> MAP</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">ListTableViews</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">mxd</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">""</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> df</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /></SPAN>
<SPAN class="com" style="color: #808080;">#Set destinationTable (CR_Link) to empty string. This gets populated only if Link table is loaded into mxd.</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;"># else, the select layer by attribute tool doesn't run</SPAN><SPAN class="pln" style="color: #000000;">
destinationTable </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">""</SPAN><SPAN class="pln" style="color: #000000;">
originTable </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">""</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /></SPAN>
<SPAN class="com" style="color: #808080;">#used in generating list of related CR_IDs for match Catalog to Link table. Related by CR_ID field.</SPAN><SPAN class="pln" style="color: #000000;">
primaryKeyField </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">"CR_ID"</SPAN><SPAN class="pln" style="color: #000000;">
foreignKeyField </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">"CR_ID"</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /></SPAN><SPAN class="com" style="color: #808080;">#These will be used to get records in Link that are related to Catalog where #Reg_Code = <input></SPAN><SPAN class="pln" style="color: #000000;"><BR /><BR /></SPAN>
<SPAN class="kwd" style="color: #00008b;">for</SPAN><SPAN class="pln" style="color: #000000;"> table </SPAN><SPAN class="kwd" style="color: #00008b;">in</SPAN><SPAN class="pln" style="color: #000000;"> tables</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
descTable </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">Describe</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">table</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
tableNameList </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">descTable</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="pln" style="color: #000000;">name</SPAN><SPAN class="pun" style="color: #000000;">).</SPAN><SPAN class="pln" style="color: #000000;">split</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="str" style="color: #800000;">"."</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
tableName </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> tableNameList</SPAN><SPAN class="pun" style="color: #000000;">[-</SPAN><SPAN class="lit" style="color: #800000;">1</SPAN><SPAN class="pun" style="color: #000000;">]</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">if</SPAN><SPAN class="pln" style="color: #000000;"> tableName </SPAN><SPAN class="pun" style="color: #000000;">==</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">"CR_Catalog"</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
originTable </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> table
</SPAN><SPAN class="kwd" style="color: #00008b;">elif</SPAN><SPAN class="pln" style="color: #000000;"> tableName </SPAN><SPAN class="pun" style="color: #000000;">==</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">"CR_Link"</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
destinationTable </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> table
</SPAN><SPAN class="kwd" style="color: #00008b;">else</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">pass</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /><BR /><BR /></SPAN><SPAN class="com" style="color: #808080;">#Main code to select all records in Catalog with matching Reg_code and then getting related records in Link table</SPAN><SPAN class="pln" style="color: #000000;"><BR /></SPAN>
<SPAN class="kwd" style="color: #00008b;">if</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="kwd" style="color: #00008b;">not</SPAN><SPAN class="pln" style="color: #000000;"> str</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">destinationTable</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">==</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">""</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="kwd" style="color: #00008b;">and</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="kwd" style="color: #00008b;">not</SPAN><SPAN class="pln" style="color: #000000;"> str</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">originTable</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">==</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">""</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">def</SPAN><SPAN class="pln" style="color: #000000;"> buildWhereClauseFromList</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">originTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> primaryKeyField</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> valueList</SPAN><SPAN class="pun" style="color: #000000;">):</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;"># Add DBMS-specific field delimiters</SPAN><SPAN class="pln" style="color: #000000;">
fieldDelimited </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">AddFieldDelimiters</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">Describe</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">originTable</SPAN><SPAN class="pun" style="color: #000000;">).</SPAN><SPAN class="pln" style="color: #000000;">path</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> primaryKeyField</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;"># Determine field type</SPAN><SPAN class="pln" style="color: #000000;">
fieldType </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">ListFields</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">originTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> primaryKeyField</SPAN><SPAN class="pun" style="color: #000000;">)[</SPAN><SPAN class="lit" style="color: #800000;">0</SPAN><SPAN class="pun" style="color: #000000;">].</SPAN><SPAN class="pln" style="color: #000000;">type
</SPAN><SPAN class="com" style="color: #808080;"># Add single-quotes for string field values</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">if</SPAN><SPAN class="pln" style="color: #000000;"> str</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">fieldType</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">==</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">'String'</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
valueList </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">[</SPAN><SPAN class="str" style="color: #800000;">"'%s'"</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">%</SPAN><SPAN class="pln" style="color: #000000;"> value </SPAN><SPAN class="kwd" style="color: #00008b;">for</SPAN><SPAN class="pln" style="color: #000000;"> value </SPAN><SPAN class="kwd" style="color: #00008b;">in</SPAN><SPAN class="pln" style="color: #000000;"> valueList</SPAN><SPAN class="pun" style="color: #000000;">]</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;"># Format WHERE clause in the form of an IN statement</SPAN><SPAN class="pln" style="color: #000000;">
whereClause </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">"%s IN(%s)"</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">%</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">fieldDelimited</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">', '</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="pln" style="color: #000000;">join</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">map</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">str</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> valueList</SPAN><SPAN class="pun" style="color: #000000;">)))</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">return</SPAN><SPAN class="pln" style="color: #000000;"> whereClause</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /> </SPAN>
<SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="kwd" style="color: #00008b;">def</SPAN><SPAN class="pln" style="color: #000000;"> selectRelatedRecords</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">originTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> destinationTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> primaryKeyField</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> foreignKeyField</SPAN><SPAN class="pun" style="color: #000000;">):</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;">## Defines the record selection from the record selection of the OriginTable</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;">## and applys it to the DestinationTable using a SQL WHERE clause built</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;">## in the previous defintion"""</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;"># Set the SearchCursor to look through the selection of the OriginTable</SPAN><SPAN class="pln" style="color: #000000;">
sourceIDs </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> set</SPAN><SPAN class="pun" style="color: #000000;">([</SPAN><SPAN class="pln" style="color: #000000;">row</SPAN><SPAN class="pun" style="color: #000000;">[</SPAN><SPAN class="lit" style="color: #800000;">0</SPAN><SPAN class="pun" style="color: #000000;">]</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="kwd" style="color: #00008b;">for</SPAN><SPAN class="pln" style="color: #000000;"> row </SPAN><SPAN class="kwd" style="color: #00008b;">in</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="pln" style="color: #000000;">da</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">SearchCursor</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">originTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> primaryKeyField</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> sqlExp</SPAN><SPAN class="pun" style="color: #000000;">)])</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /> </SPAN>
<SPAN class="com" style="color: #808080;"># Establishes the where clause used to select records from DestinationTable</SPAN><SPAN class="pln" style="color: #000000;">
whereClause </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> buildWhereClauseFromList</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">destinationTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> foreignKeyField</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> sourceIDs</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;"># Process: Select Layer By Attribute</SPAN><SPAN class="pln" style="color: #000000;">
arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">SelectLayerByAttribute_management</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">destinationTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> selectType</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> whereClause</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="com" style="color: #808080;"># Process: Select related records between OriginTable and DestinationTable</SPAN><SPAN class="pln" style="color: #000000;">
selectRelatedRecords</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">originTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> destinationTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> primaryKeyField</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> foreignKeyField</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">del</SPAN><SPAN class="pln" style="color: #000000;"> tables</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> tableName</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> tableNameList</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> primaryKeyField</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> foreignKeyField
</SPAN><SPAN class="kwd" style="color: #00008b;">del</SPAN><SPAN class="pln" style="color: #000000;"> originTable</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> destinationTable</SPAN>
<SPAN class="pln" style="color: #000000;"><BR /><BR /></SPAN>
<SPAN class="com" style="color: #808080;">#If Link isn't in mxd, send a message</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">else</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">AddMessage</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="str" style="color: #800000;">"CR_Link and CR_Catalog tables must be added to dataframe in order to create selection in CR_Link"</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">for</SPAN><SPAN class="pln" style="color: #000000;"> lyr </SPAN><SPAN class="kwd" style="color: #00008b;">in</SPAN><SPAN class="pln" style="color: #000000;"> layers</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
fieldList </SPAN><SPAN class="pun" style="color: #000000;">=</SPAN><SPAN class="pln" style="color: #000000;"> arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">ListFields</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">lyr</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="pln" style="color: #000000;">dataSource</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="str" style="color: #800000;">"REG_CODE"</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">if</SPAN><SPAN class="pln" style="color: #000000;"> len</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">fieldList</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="pun" style="color: #000000;">==</SPAN><SPAN class="pln" style="color: #000000;"> </SPAN><SPAN class="lit" style="color: #800000;">1</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
arcpy</SPAN><SPAN class="pun" style="color: #000000;">.</SPAN><SPAN class="typ" style="color: #2b91af;">SelectLayerByAttribute_management</SPAN><SPAN class="pun" style="color: #000000;">(</SPAN><SPAN class="pln" style="color: #000000;">lyr</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> selectType</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> sqlExp</SPAN><SPAN class="pun" style="color: #000000;">)</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">else</SPAN><SPAN class="pun" style="color: #000000;">:</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">pass</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">del</SPAN><SPAN class="pln" style="color: #000000;"> lyr</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> layers</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;">
</SPAN><SPAN class="kwd" style="color: #00008b;">del</SPAN><SPAN class="pln" style="color: #000000;"> sqlExp</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> fld</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> mxd</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> df</SPAN><SPAN class="pun" style="color: #000000;">,</SPAN><SPAN class="pln" style="color: #000000;"> fieldList </SPAN>