AnsweredAssumed Answered

Using a QueryTable to populate a combobox

Question asked by bradleyglennjones on Apr 25, 2016
Latest reply on Apr 26, 2016 by bradleyglennjones

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:
                print row
        del mxd, df


This is my first Python add-in.




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"
        table = arcpy.MakeQueryTable_management(eadFacil, "results", "", "", "", "Name" + " like '%" + query + "%'")
        result = arcpy.GetCount_management(table)
        count = int(result.getOutput(0))

        # 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