Solved! Go to Solution.
Public Sub CalculateField(ByVal InputTable As Object, ByVal FieldName As String, ByVal Expression As String, Optional ByVal ExpressionType As String = Nothing, Optional ByVal CodeBlock As String = Nothing, Optional ByVal Output As Object = Nothing) Dim Calc As New ESRI.ArcGIS.DataManagementTools.CalculateField Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2 Try Using releaser As New ESRI.ArcGIS.ADF.ComReleaser releaser.ManageLifetime(Calc) Calc.in_table = InputTable Calc.field = FieldName Calc.expression = Expression Calc.expression_type = ExpressionType 'VB, PYTHON, or PYTHON_9.3 Calc.code_block = CodeBlock Calc.out_table = Output Result = RunTool(Calc, Nothing) If Result Is Nothing Then System.Windows.Forms.MessageBox.Show("Could not calculate field") End If End Using Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString, "Calculate Field error") End Try End Sub Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Try System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult) If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then If GP.MessageCount > 0 Then For Count = 0 To Globals.GP.MessageCount - 1 Message += GP.GetMessage(Count) & vbNewLine Next System.Windows.Forms.MessageBox.Show(Message, "GP Failure") End If Return Nothing End If Return Result Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace, "Run Geoprocessor") Return Nothing End Try End Function
Public Sub CalculateField(ByVal InputTable As Object, ByVal FieldName As String, ByVal Expression As String, Optional ByVal ExpressionType As String = Nothing, Optional ByVal CodeBlock As String = Nothing, Optional ByVal Output As Object = Nothing) Dim Calc As New ESRI.ArcGIS.DataManagementTools.CalculateField Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2 Try Using releaser As New ESRI.ArcGIS.ADF.ComReleaser releaser.ManageLifetime(Calc) Calc.in_table = InputTable Calc.field = FieldName Calc.expression = Expression Calc.expression_type = ExpressionType 'VB, PYTHON, or PYTHON_9.3 Calc.code_block = CodeBlock Calc.out_table = Output Result = RunTool(Calc, Nothing) If Result Is Nothing Then System.Windows.Forms.MessageBox.Show("Could not calculate field") End If End Using Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString, "Calculate Field error") End Try End Sub Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Try System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult) If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then If GP.MessageCount > 0 Then For Count = 0 To Globals.GP.MessageCount - 1 Message += GP.GetMessage(Count) & vbNewLine Next System.Windows.Forms.MessageBox.Show(Message, "GP Failure") End If Return Nothing End If Return Result Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace, "Run Geoprocessor") Return Nothing End Try End Function