VB Script help - updating a field in all tables in a GDB

Discussion created by aburdett on Mar 15, 2011
Latest reply on Mar 26, 2011 by VPrasad-esristaff

I am using 9.3..1 ArcMap (not 10) and have a need to be able to loop through all tables (not feature classes) and for each table add a new field (tablename) and update the field to store the name of the table against every row. I tried to do this in Python however dont think I can unless I use 10 (with ArcPy) which I do not have access to as yet.

I have instead written a VB script, however the script goes along OK however fails at the last step where I try and update the field. The error I get is along the lines of "calculated value is invalid for row with object id ...."

Any help appreciated!

The script is as follows:

Private Sub UIToolControl1_Select()

Dim pTableCollection As ITableCollection
Dim i As Integer
Dim pTable As ITable
Dim pDataset As IDataset
Dim pMap As IMap
Dim pMxApp As IMxApplication
Dim pMxDoc As IMxDocument
Dim Str, ThisName As String
Dim pFieldEdit As IFieldEdit
Dim pField As IField

Dim pCalculator As ICalculator

Dim pCursor As ICursor

Set pField = New Field
Set pFieldEdit = pField
With pFieldEdit
.Type = esriFieldTypeString
.Name = "Table_Name"
.Length = 50
End With

Str = ""
'+++ Get the focus map

Set pMxDoc = Application.Document
Set pTableCollection = pMxDoc.FocusMap

For i = 0 To pTableCollection.TableCount - 1
Set pTable = pTableCollection.Table(i)
Set pDataset = pTable

ThisName = pDataset.Name
Str = Str + ThisName + vbCrLf
pTable.AddField pFieldEdit

'set calculator
Set pCalculator = New Calculator
Set pCursor = pTable.Update(Nothing, True)

With pCalculator
.Field = "Table_Name"
.Expression = ThisName
Set .Cursor = pCursor
End With
'Perform the calculation:

Next i
MsgBox Str
End Sub