Using a QueryTable to populate a combobox

2395
6
Jump to solution
04-25-2016 01:03 PM
BradJones
New Contributor III

I'm working on a Python add-in with a combobox where a user queries a table of facilities by name then zooms to a feature selected (many more steps because it's a nonspatial table with related featureclass). Because there can be multiple facilities with the same name I have the results in a query table added to the TOC.  If there is only one result the mxd it zooms to the selected extent.  If the result is >1 I want to populate a combo box (same or second one) with the results in the query table so the user can select from that list (facility name and address).

The problem is it won't recognize the results query table in the mxd. When I create a search cursor to read the query table and append them to the combobox I get: RuntimeError: 'in_table' is not a table or a featureclass

Here is the code from  onFocus.

    def onFocus(self, focused):

        # Empty the Combobox items list
        self.items = []

        # Establish a reference to the current MXD
        mxd = arcpy.mapping.MapDocument("Current")
        print mxd

        # Establish a reference to the first DataFrame object in the current MXD
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        print df

        # Create table object for results query table
        qtable = arcpy.mapping.ListTableViews(mxd, "results", df)
        print qtable

        with arcpy.da.SearchCursor(qtable, ["Name", "Address"]) as cursor:
            for row in cursor:
                self.items.append(row)
                print row
        del mxd, df

This is my first Python add-in.

Thanks

EDIT:

Here is some more code:

    def onEnter(self):
        # Set current mxd and dataframe.
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd, "")[0]

        # Make Query Table
        env.workspace = r"********PATH TO SDE CONNECTION********"
        eadFacil = "FEATURECLASS"
        print(query)
        table = arcpy.MakeQueryTable_management(eadFacil, "results", "", "", "", "Name" + " like '%" + query + "%'")
        print(table)
        result = arcpy.GetCount_management(table)
        count = int(result.getOutput(0))
        print(count)

        # Conditional statement based on number of results returned.
        if count == 1:
            with arcpy.da.SearchCursor(table, ["BO", "Factype"], "Name" + " like '%" + query + "%'") as cursor:
                for row in cursor:
                    building = row[0]
                    print building
                    code = row[1]
                    print code
0 Kudos
1 Solution

Accepted Solutions
BradJones
New Contributor III

I found my stoopid mistake.

The result of this function is a list:

qtable = arcpy.mapping.ListTableViews(mxd, "results", df)  

As such I need a for loop to iterate over the list, even if it's just one item in the list.

So changing the MakeQueryTable function to this: 

table = arcpy.MakeTableView_management(eadFacil, "results",  "Name like '%" + query + "%'")
and adding the for loop solves my problem.

View solution in original post

0 Kudos
6 Replies
DanPatterson_Retired
MVP Esteemed Contributor

is it a layer or a table view ListTableViews—Help | ArcGIS for Desktop

0 Kudos
BradJones
New Contributor III

The table is created with MakeQueryTable_management

As per your comment I replaced ListLayers with ListTableViews on line 10 of the code snippet above and received this error message:

>>>

<geoprocessing Map object object at 0x060262E0>

<geoprocessing Data Frame object object at 0x11D7F020>

[<TableView object at 0x11b38cf0[0x11d80cc8]>]           ***NOTE: Table object is recognized now****

Traceback (most recent call last):

  File "C:\Users\brad.jones\AppData\Local\ESRI\Desktop10.3\AssemblyCache\{8865179D-7740-4EE8-B020-9C6E94E9174D}\EADpart2_addin.py", line 259, in onFocus

    with arcpy.da.SearchCursor(qtable, ["Name", "Address"]) as cursor:

RuntimeError: 'in_table' is not a table or a featureclass    ***NOTE: But not with the Search Cursor****

0 Kudos
BradJones
New Contributor III

I suppose I could add the table view and then use  DefinitionQuery property instead of MakeQueryTable....

0 Kudos
AdrianWelsh
MVP Notable Contributor

Brad,

Would it help if you posted more of your code for us to see? Maybe showing the 'in_table' variable (and more)?

0 Kudos
BradJones
New Contributor III

the in_table is the object (qtable) created from on line 15 of the onFocus code snippet in the original post.

        qtable = arcpy.mapping.ListTableViews(mxd, "results", df)

0 Kudos
BradJones
New Contributor III

I found my stoopid mistake.

The result of this function is a list:

qtable = arcpy.mapping.ListTableViews(mxd, "results", df)  

As such I need a for loop to iterate over the list, even if it's just one item in the list.

So changing the MakeQueryTable function to this: 

table = arcpy.MakeTableView_management(eadFacil, "results",  "Name like '%" + query + "%'")
and adding the for loop solves my problem.

0 Kudos