I have developed a command button for ArcGIS 9.2 (Visual Studio 2005) that copies the selected features of a featureclass to a shapefile. It then examines the attribute table of the shapefile (via a loop) for three specific field names. If a field name isn't found, a new column (type text) is created with that name, and the user is presented a Windows form that I created that offers three options for populating that field:
- Copy an existing field: The user selects an existing field from a drop-down comboBox
- Enter a value: The user enters a value to be used to populate the field
- Leave blank: A space is entered in the field
All of these are accomplished via a call to CalculateField_management within the loop.
If the user chooses the same option for the first and second time through the loop, it will work for the third iteration. However, if different options are chosen for the first and second it will fail on the third iteration, and I get this message in the debugger:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in SDtools.dll System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
The error points to the line with the CalculateField command in the code.
Does anyone know why this is occurring? Will post code if necessary.
-code-
Dim pGP As IGeoProcessor = New GeoProcessor
Dim pParams As IVariantArray = New VarArrayClass
pGP.OverwriteOutput = True
Dim Form As frmAddFieldAttribute = New frmAddFieldAttribute
Dim pTable As ITable = CType(pMxDoc.FocusMap.Layer(0), IAttributeTable).AttributeTable
SearchFields(pTable.Fields, Form.cbCopyAttribute) 'Adds existing fields to the comboBox (option 1)
Dim fieldList As New System.Collections.ArrayList
fieldList.Add("TRACTNBR")
fieldList.Add("FARMNBR")
fieldList.Add("CLUNBR")
Dim X As String
Dim strExpression as String = ""
For Each X In fieldList
If pTable.FindField(X) < 0 Then
AddField(pTable, X) 'Adds the attribute to the end of the table
Form.ShowDialog() 'User chooses from 3 radioButtons, selects from a comboBox (option 1),
'enters a value (option 2), or leaves the new field blank (option 3).
'User then clicks a button to close the form, and the code continues.
If Form.rbCopyAttribute.Checked = True Then
strExpression = String.Format("[{0}]", Form.cbCopyAttribute.SelectedItem.ToString)
ElseIf Form.rbEnterValue.Checked = True Then
strExpression = String.Format("""{0}""", CStr(Form.txtEnterValue.Text.ToString.Trim))
ElseIf Form.rbLeaveBlank.Checked = True Then
strExpression = """ """ 'This should represent a space
Else
Debug.Print("This should never occur.")
Exit Sub
End If
'Populate the new field
pParams.RemoveAll()
pParams.Add(pTable)
pParams.Add(X)
Debug.Print("{0} = {1}", X, strExpression)
pParams.Add(strExpression)
pGP.Execute("CalculateField_management", pParams, Nothing) 'HRESULT error occurs here
End If
Next