English

How To: Add a new row to a geodatabase table using VBA

Summary

This article shows how to create and populate a new row in a personal geodatabase table.

Procedure

  1. Open ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in ThisDocument code module will only run in the current map document. To store the code in all map documents, open the code in the Normal.mxt ThisDocument code module.

  4. Paste the following code into the code module.

    Code:
    Sub AddRow()
    Dim pPropset As IPropertySet
    Set pPropset = New PropertySet
    pPropset.SetProperty "DATABASE", "C:\Country.mdb"
    pPropset.SetProperty "DATAPROVIDER", "Access Data Source"
    Dim pwf As IWorkspaceFactory
    Set pwf = New AccessWorkspaceFactory
    Dim fws As IFeatureWorkspace
    Set fws = pwf.Open(pPropset, 0)
    'Open the Table
    Dim ptable As ITable
    Set ptable = fws.OpenTable("Names")

    'Create new row
    Dim pCursor As ICursor
    Dim arow As IRow
    Dim pRowBuff As IRowBuffer
    Set pRowBuff = ptable.CreateRowBuffer
    'Populate the row with values
    pRowBuff.Value(1) = 100 'Numeric column
    pRowBuff.Value(2) = "Canada" 'Text column
    Set pCursor = ptable.Insert(True)
    pCursor.InsertRow pRowBuff
    End Sub

  5. Change the database path, table name, and values to apply to your data.
  6. Close the Visual Basic Editor.
  7. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.