Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim queryFilter As String = "Code = '" + ComboBox1.Text.Trim + "'" Dim table as ESRI.ArcGIS.Geodatabase.ITable = ????? MsgBox(PerformAttributeQuery(table, queryFilter)) End Sub Public Function PerformAttributeQuery(ByVal table As ESRI.ArcGIS.Geodatabase.ITable, ByVal whereClause As System.String) As ESRI.ArcGIS.Geodatabase.ICursor Dim queryFilter As ESRI.ArcGIS.Geodatabase.IQueryFilter = New ESRI.ArcGIS.Geodatabase.QueryFilterClass() queryFilter.WhereClause = whereClause Dim cursor As ESRI.ArcGIS.Geodatabase.ICursor = table.Search(queryFilter, False) Return cursor End Function
Solved! Go to Solution.
James, credit was given to Ken. Ken usually always figure out my problems, so I'm never shy to give him credits..haha! The WhereClause is fine. You need single quotes to query strings.
The code doesn't produces an error, it just act like the query is always true, even when it's not.
Public Function PerformAttributeQuery(ByVal table As ESRI.ArcGIS.Geodatabase.ITable, ByVal whereClause As System.String) As ESRI.ArcGIS.Geodatabase.ICursor Dim queryFilter As ESRI.ArcGIS.Geodatabase.IQueryFilter = New ESRI.ArcGIS.Geodatabase.QueryFilterClass() queryFilter.WhereClause = whereClause ' create the where clause statement if table.RowCount(queryFilter) = 0 then Return Nothing ' query the table passed into the function and use a cursor to hold the results Dim cursor As ESRI.ArcGIS.Geodatabase.ICursor = table.Search(queryFilter, False) Return cursor End Function
Public Function OpenTable(ByVal directory As String, ByVal TableName As String, ByVal Type As String) As ESRI.ArcGIS.Geodatabase.ITable
Dim pWS As ESRI.ArcGIS.Geodatabase.IWorkspace
Dim pWSFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory
Dim pFWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace
Dim pTable As ESRI.ArcGIS.Geodatabase.ITable
Try
If Type = "DBF" Then
pWSFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory
ElseIf Type = "File Geodatabase" Then
pWSFactory = New ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory
ElseIf Type = "Personal Geodatabase" Then
pWSFactory = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory
ElseIf Type = "SDE" Then
pWSFactory = New ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactory
End If
pWS = pWSFactory.OpenFromFile(directory, 0)
pFWorkspace = pWS
pTable = pFWorkspace.OpenTable(TableName)
Return pTable
Catch ex As Exception
Windows.Forms.MessageBox.Show("Open Table error", ex.Message)
Return Nothing
End Try
End Function
Dim table as ESRI.ArcGIS.Geodatabase.ITable = CType(pFeatureClass, ESRI.ArcGIS.Geodatabase.ITable)
Public Function PerformAttributeQuery(ByVal table As ESRI.ArcGIS.Geodatabase.ITable, ByVal whereClause As System.String) As ESRI.ArcGIS.Geodatabase.ICursor
Dim pMap As IMap = My.ArcMap.Document.ActiveView
Dim queryFilter As String = "FileNumber = '175980'"
Dim table As ITable = DirectCast(pMap.FindByName("CSJGISSDE.GISDATA.Subdivision_Plans_Table"), FeatureLayer).ITable
MsgBox(PerformAttributeQuery(table, queryFilter))
Private Function GetTable(InTableName As String, pMap as IMap) As ITable Dim pStTabColl As IStandaloneTableCollection pStTabColl = pMap For j as Integer = 0 To pStTabColl.StandaloneTableCount If (pStTabColl.StandaloneTable(j).Name = InTableName) Then Return pStTabColl.StandaloneTable(j) End If Next j Return Nothing End Function
Thanks Ken. I'm trying to access an SDE Table that's already loaded in ArcMap. I just need to know how to assign the table (by its name), in the same fashion as you would assign a Feature Class by it's name.
'assumes you have set your m_pApp reference already Dim i As Integer Dim pLayer As IFeatureLayer pLayer = Nothing pDoc = m_pApp.Document pMap = pDoc.FocusMap For i = 0 To pMap.LayerCount - 1 If pMap.Layer(i).Name = "Your SDE Table Name Here" Then pLayer = pMap.Layer(i) Exit For End If Next 'now set your ITable Dim pTable As ITable Set pTable = pLayer.FeatureClass
Public Sub TableSel() Dim pMxDoc As IMxDocument Dim pMap As IStandaloneTableCollection Dim pStdAloneTbl As IStandaloneTable Dim pTableSel As ITableSelection Dim pQueryFilt As IQueryFilter Dim pSelSet As ISelectionSet ' Get the standalone table from the map Set pMxDoc = Application.Document Set pMap = pMxDoc.FocusMap Set pStdAloneTbl = pMap.StandaloneTable(0) Set pTableSel = pStdAloneTbl ' Make the query filter Set pQueryFilt = New QueryFilter pQueryFilt.WhereClause = "POP1990 > 10000000" ' Perform the selection pTableSel.SelectRows pQueryFilt, esriSelectionResultNew, False ' Report how many rows were selected Set pSelSet = pTableSel.SelectionSet MsgBox pSelSet.Count & " rows selected in " & pStdAloneTbl.Name End Sub
Thanks James, but that didn't work. I think it's because you make reference to a Feature Class Table. I want to query a Single (stand-alone) Table, that's inside an SDE. I can't believe how complicated this is..lol
Private Function GetTable(InTableName As String, pMap as IMap) As ITable Dim pStTabColl As IStandaloneTableCollection pStTabColl = pMap For j as Integer = 0 To pStTabColl.StandaloneTableCount If (pStTabColl.StandaloneTable(j).Name = InTableName) Then Return pStTabColl.StandaloneTable(j) End If Next j Return Nothing End Function