1

I got a building layer. And i got a table that contains 2 fields named SDE_OBJECT_ID and DOOR_NO. This table is not spatial table, it's Oracle table which i can join with my building layer if i want and i can use building layer's OID field with this table's "SDE_OBJECT_ID" field. What i try to do is to select a building with my mouse and then running this tool to show all the door numbers that is located in this Oracle table. There can be 1+ door numbers so i am listing them in my listbox as you can see.

The code works if i change

Dim FeatOID As String
FeatOID = "SDE_OBJECT_ID = '" & pFeature.OID & "'"

to

Dim FeatOID As String
FeatOID = "SDE_OBJECT_ID = 48565"

i mean it works if i type an OBJECTID number but it doesnt work while i try to grap the OBJECTID of the selected feature.

I also tried to do this

pQueryFilter.WhereClause = "SDE_OBJECT_ID = '" + pFeature.Value(pFeature.Fields.FindField("OBJECTID")) +"' "

but it doesn't work...

Here is my code:

Private Sub CommandButton1_Click()

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pMap As IMap
Set pMap = pMxDoc.FocusMap

Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = pMap.Layer(2)

Dim pFeatureSelection As IFeatureSelection
Set pFeatureSelection = pFeatureLayer

Dim pFeatureCursor As IFeatureCursor
pFeatureSelection.SelectionSet.Search Nothing, False, pFeatureCursor

Dim pFeature As IFeature
Set pFeature = pFeatureCursor.NextFeature

Do Until pFeature Is Nothing
'MsgBox pFeature.Value(pFeature.Fields.FindField("OBJECTID")) 'it works, shows the OID of the selected feature
 MsgBox pFeature.OID 'it works too
        Set pFeature = pFeatureCursor.NextFeature
    Loop

Dim pTableCollection As ITableCollection
Set pTableCollection = pMap

Dim pTable As ITable

For i = 0 To pTableCollection.TableCount - 1   'i am selecting the Oracle table
Set pTable = pTableCollection.Table(i)
Next i

Dim FeatOID As Long
FeatOID = pFeature.OID

Dim Query As String
Query = "SDE_OBJECT_ID = '" & FeatOID & "'"

Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter

pQueryFilter.WhereClause = Query

Dim pCursor As ICursor
Set pCursor = pTable.Search(pQueryFilter, False)

Dim pRow As IRow
Set pRow = pCursor.NextRow

While Not pRow Is Nothing
        'MsgBox pRow.Value(pRow.Fields.FindField("DOOR_NO"))
        ListBox1.AddItem (pRow.Value(pRow.Fields.FindField("DOOR_NO")))
        Set pRow = pCursor.NextRow
    Wend


End Sub
3

Your code won't work. You loop the cursor until it finishes. At that point pFeature is Nothing. After that you try to access the OID value of pFeature, which is Nothing.

If you only select a single building or only want the first building OID of any selection set don't loop the cursor, just get rid of the cursor loop. Then you should make sure you got an actual feature by including all the rest of the code within an if condition that makes sure pFeature is not Nothing. You only need:

Dim pFeature as IFeature
Set pFeature = pFeatureCursor.NextFeature

if not pFeature is Nothing Then

  ' All the rest of your code beginning with: Dim pTableCollection As ITableCollection

End If

If you really want to use each OID of each building, the nextfeature and Loop statements need to go at the bottom of your code:

Dim pFeature As IFeature
Set pFeature = pFeatureCursor.NextFeature

Do Until pFeature Is Nothing

  ' All the rest of your code beginning with: Dim pTableCollection As ITableCollection

    Set pFeature = pFeatureCursor.NextFeature
Loop

You have the same problem with your loop to select the oracle table. You will always be using just the last table in the map, since you complete the loop before using any tables. You should include an if condition that checks the table name of each table and drops out of the loop when you find the oracle table that works with your buildings.

Dim pDs As IDataset
For i = 0 To pTableCollection.TableCount - 1   'i am selecting the Oracle table
   Set pTable = pTableCollection.Table(i)
   Set pDs = pTable
   if pDs.name = "YourTableName" Then Exit For
Next i

What version of ArcMap are you using? VBA is effectively a dead language in the most recent releases of ArcMap and should not be used for programming. Start programming in Python or VB.Net with Visual Studio.

  • Thank you for your answer! I have arranged my code according to your suggestion but i get an error at Dim pCursor As ICursor Set pCursor = pTable.Search(pQueryFilter, False) it says "data type mismatch". I added some msgboxes to ensure the code is running correctly and i get every answer correct. I just wanna choose one building and show the door numbers of it. I am using both 9.3.1 and 10.2.2 Arcmaps. I know that VBA is not an effective language but i am trying to learn it =) I want to start learning Python but i dont know if i can create interfaces with it... – Buğra Otken Jul 4 '15 at 13:05
  • 1
    Query = "SDE_OBJECT_ID = '" & FeatOID & "'" should be Query = "SDE_OBJECT_ID = " & FeatOID It is not a string field. – Richard Fairhurst Jul 4 '15 at 15:56
  • Oh...That solved my whole problem! I have learned so much today thanks to you Mr. Fairhurst! Thank you very much! – Buğra Otken Jul 4 '15 at 20:33
  • You are welcome. I am glad your problem is solved and that you now have a better understanding of how loops work. – Richard Fairhurst Jul 6 '15 at 14:09
1

I think SDE_OBJECT_ID might be data type LONG. Have you tried pFeature.OID.ToString(), or whatever the conversion method in VBA is? Not sure if it might fix the issue. Just an idea.

  • In VBA the string conversion is handled by using the & to concatenate values. The & syntax automatically changes everything to a string.. – Richard Fairhurst Jul 3 '15 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.