English

How To: Programmatically loop through a text file using ArcObjects

Summary

Note:
This article pertains to ArcGIS versions 8.x and 9.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.

This article provides a sample of looping through a text file using ArcObjects.

Note: 
Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward

Procedure

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl
Note:
For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  1. Right-click the UIButtonControl and select View Source.
  2. Copy this code into the UIButtonControl's click event.
Dim pMxdoc As IMxDocument
Set pMxdoc = ThisDocument
Dim pTable As ITable
Dim pStandColl As IStandaloneTableCollection
Dim pStandTab As IStandaloneTable
Set pStandColl = pMxdoc.FocusMap
Set pStandTab = pStandColl.StandaloneTable(0)
Set pTable = pStandTab
Dim pRow As IRow, pCursor As ICursor
Set pCursor = pTable.Search(Nothing, False)
Set pRow = pCursor.NextRow

'Display a message box for each record
Do Until pRow Is Nothing
  MsgBox pRow.Value(0) & " : " & CStr(pRow.Value(1))
  Set pRow = pCursor.NextRow
Loop
  1. Create a text file called Cities.txt with the following contents. Column names must be enclosed in double quotes and record values need to be comma delimited.
"CITY","POPULATION" 
Los Angles,14234953 
New York,17834034
  1. Add Cities.txt to ArcMap.
  2. Click the new UIButtonControl to loop through the text file.