This sample code demonstrates how to delete a field from a Grid value attribute table (VAT) using GridTableOp.
How to use
- Paste the code into VBA.
- Make sure the Spatial Analyst extension is checked.
- Add reference to ESRI SpatialAnalyst Object Library.
- Add reference to ESRI GeoAnalyst Object Library.
- Call the sub DeleteFieldFromVAT from another Sub or Function and pass the Grid path, name and the field name.
Sub DeleteFieldFromVAT(sGridPath As String, sGridName As String, sFieldName As String)
On Error GoTo ErrorHandler
'Open Grid As IRasterDataset
Dim pRasDS01 As IRasterDataset
Set pRasDS01 = OpenGridAsIRasterDataset(sGridPath, sGridName)
'Create a field that you want to delete
Dim pField As IField
Set pField = New esriGeoDatabase.Field
Dim pFieldEdit As IFieldEdit
Set pFieldEdit = pField
pFieldEdit.Name = sFieldName
'Create GridTableOp
Dim pGridTableOp As IGridTableOp
Set pGridTableOp = New GridTableOp
'Delete Field
pGridTableOp.DeleteField pRasDS01, pField
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub
Public Function OpenGridAsIRasterDataset(sPath As String, sRasterName As String) As IRasterDataset
'Returns RasterDataset given a file name and it's directory
On Error GoTo ErrorHandler
Dim pWSFact As IWorkspaceFactory
Dim pWS As IWorkspace
Dim pRasterWS As IRasterWorkspace
Dim pRasDS As IRasterDataset
Set pWSFact = New RasterWorkspaceFactory
If pWSFact.IsWorkspace(sPath) Then
Set pWS = pWSFact.OpenFromFile(sPath, 0)
Set pRasterWS = pWS
Set pRasDS = pRasterWS.OpenRasterDataset(sRasterName)
Set OpenGridAsIRasterDataset = pRasDS
Else
Set OpenGridAsIRasterDataset = Nothing
End If
Exit Function
ErrorHandler:
Set OpenGridAsIRasterDataset = Nothing
End Function