Select to view content in your preferred language

looping through the records of a standalone table

2180
6
08-29-2010 11:52 AM
DavidKelly1
Emerging Contributor
I am using VBA in version 9.3.1. I just want to loop through the records of a standalone table I added to a mxd.

Dim pAccessWSF As IWorkspaceFactory
Dim pWorkSpace As IWorkspace
Dim pFeatws As IFeatureWorkspace
Dim ptable As ITable

Set pAccessWSF = New AccessWorkspaceFactory

Set pWorkSpace = pAccessWSF.OpenFromFile("C:\Calsmoke\CalSmoke.mdb", 0)
Set pFeatws = pWorkSpace
Set ptable = pFeatws.OpenTable("Daysmoke_emissions")

Dim pMap As IMap
Dim pMx As IMxDocument
Set pMx = ThisDocument
Set pMap = pMx.FocusMap
Set pStTab = New StandaloneTable
Set pStTab.Table = ptable
Set pStTabColl = pMap
pStTabColl.AddStandaloneTable pStTab
pMx.UpdateContents

Dim pRow As IRow

Set pRow = pStTab.Table.GetRow(1)

When I execute the last line of code I get the following error message a run time error -2147467259: syntax error(missing operator) in query expression '=0'.

Thanks,

David
0 Kudos
6 Replies
NirYoscovitz
Emerging Contributor
Hi David,

Your code looks OK.
Does your table contain an OID column?

Regards,
Nir
0 Kudos
NeilClemmons
Honored Contributor
You access records in a table via a query.  You then loop through the cursor returned by that query.  To get all records in a table, pass in a null reference for the query filter.

Dim cursor As ICursor
Set cursor = table.Search(Nothing, False)
Dim row As IRow
Set row = cursor.NextRow
Do While Not row Is Nothing
  ' do something

  Set row = cursor.NextRow
Loop
0 Kudos
DavidKelly1
Emerging Contributor
Neil,

I changed the code to use a cursor as you suggested and it works.  The access table I am trying to update does not have a OID field so I get an error message when I store the Lat Long coordinates in the table.  The code I am using is below:

Dim pAccessWSF As IWorkspaceFactory
Dim pWorkSpace As IWorkspace
Dim pFeatws As IFeatureWorkspace
Dim pTables As IStandaloneTableCollection
Dim ptable As ITable
Dim pTableName As String

Set pAccessWSF = New AccessWorkspaceFactory

Set pWorkSpace = pAccessWSF.OpenFromFile("C:\Calsmoke\CalSmoke.mdb", 0)
Set pFeatws = pWorkSpace
Set ptable = pFeatws.OpenTable("Daysmoke_emissions")

Dim pStTab As IStandaloneTable
Dim pStTabColl As IStandaloneTableCollection

Dim pMap As IMap
Dim pMx As IMxDocument
Set pMx = ThisDocument
Set pMap = pMx.FocusMap
Set pStTab = New StandaloneTable
Set pStTab.Table = ptable
Set pStTabColl = pMap

pStTabColl.AddStandaloneTable pStTab

pMx.UpdateContents

Dim pRow As IRow
Dim pCenterXFld As IField
Dim pCenterXValue As Double
Dim pPoint As IPoint
Dim pGCS As IGeographicCoordinateSystem

Dim pSpRFc As ISpatialReferenceFactory
Set pSpRFc = New SpatialReferenceEnvironment
Set pGCS = pSpRFc.CreateGeographicCoordinateSystem(esriSRGeoCS_WGS1984)

Dim pTabCursor As ICursor

Set pTabCursor = pStTab.Table.Search(Nothing, False)
Set pRow = pTabCursor.NextRow

While Not pRow Is Nothing

    Set pPoint = New Point
     
    pPoint.x = pRow.Value(10)
         
    Debug.Print pPoint.x
   
    pPoint.y = pRow.Value(11)
          
    pPoint.PutCoords pRow.Value(10), pRow.Value(11)
     
    pPoint.Project pGCS
     
    pRow.Value(12) = pPoint.y
   
    pRow.Value(13) = pPoint.x
   
    'pRow.Store
     
    Set pRow = pTabCursor.NextRow
     
Wend

Thanks for your help.

David Kelly
0 Kudos
DavidKelly1
Emerging Contributor
My response above may have not been clear. I am trying to update two lat/long fields in a table in a personal geodatabase; it is a standalone table. Using the code above I can loop through the table and correctly generate the Lat/Longs from the Lambert coordinates in two other fields. Since the table does not have an OID field it will not let me store the generated Lat/Longs in their fields in the table.

How do you update fields in a standalone table in a personal geodatabase?

Thanks for your help,

David Kelly
0 Kudos
vincentLahaye
Emerging Contributor
hi,

did you tried update cursor. 


Set pTabCursor = pStTab.Table.update(Nothing, false)
Set pRow = pTabCursor.NextRow

While Not pRow Is Nothing


Set pPoint = New Point

pPoint.x = pRow.Value(10)

Debug.Print pPoint.x

pPoint.y = pRow.Value(11)

pPoint.PutCoords pRow.Value(10), pRow.Value(11)

pPoint.Project pGCS

pRow.Value(12) = pPoint.y

pRow.Value(13) = pPoint.x

pTabCursor.UpdateRow(pRow)

Set pRow = pTabCursor.NextRow

Wend
0 Kudos
DavidKelly1
Emerging Contributor
The Update cursor worked.  Thank you!  I was getting very frustrated.
0 Kudos