ADO - ADO Recordset EditMode Property

The EditMode property of an ADO Recordset indicates the current editing status of the record that is being modified. It helps an application determine whether a record is currently being edited, whether no editing operation is in progress, or whether a new record is being added. This property is particularly useful when working with editable Recordsets because it allows the program to check the state of a record before performing operations such as updating or canceling changes.

The EditMode property belongs to the ADO Recordset object. When a Recordset is first opened and no record is being modified, its EditMode value is normally adEditNone. When the application begins modifying an existing record, the property changes to adEditInProgress. If the application starts adding a completely new record using the AddNew method, the state becomes adEditAdd. These values allow developers to identify what kind of editing operation is currently taking place.

EditMode Constants

ADO provides three important constants for the EditMode property:

Constant Value Meaning
adEditNone 0 No editing operation is currently in progress
adEditInProgress 1 An existing record is being modified
adEditAdd 2 A new record is being added

For example, after retrieving a record from a database, an application might modify one of its fields. While the changes are being made and before the Update method is called, the Recordset can report adEditInProgress. This tells the application that the current record contains changes that have not yet been saved to the database.

Checking Whether a Record Is Being Edited

The EditMode property can be used in conditional statements. For example, in classic ADO programming, a developer can check whether changes are currently being made before deciding what action should be taken.

If rs.EditMode = adEditInProgress Then
    MsgBox "The current record is being edited."
End If

Here, rs represents an ADO Recordset. If the current record is being modified, the condition evaluates to true and the message is displayed.

EditMode and Existing Records

When an existing record is modified, ADO tracks the editing state until the changes are either saved or canceled. Consider a Recordset containing employee information. Suppose an employee's department is changed from "Sales" to "Marketing."

rs.Fields("Department").Value = "Marketing"

Before the changes are committed, the Recordset may have an EditMode value of adEditInProgress. The application can then save the changes by using:

rs.Update

After the update is successfully completed, the editing state returns to adEditNone.

EditMode and Adding New Records

The EditMode property also helps distinguish between editing an existing record and creating a new one.

For example:

rs.AddNew
rs.Fields("Name").Value = "Rahul"
rs.Fields("Department").Value = "Finance"

While the new record is being prepared, the EditMode property can indicate adEditAdd. This is different from adEditInProgress, which represents changes to an existing record.

Once the application finishes assigning the required values, it can save the new record using:

rs.Update

After the operation is completed, the editing state normally returns to adEditNone.

Canceling an Edit

One important use of EditMode is determining whether there are unsaved changes that can be canceled. If a user starts modifying a record and then selects a Cancel option, the application can use the Recordset's cancel functionality rather than saving the changes.

For example:

If rs.EditMode <> adEditNone Then
    rs.CancelUpdate
End If

The condition checks whether an editing operation is active. If the Recordset is in an editing state, CancelUpdate can discard the pending changes.

Difference Between EditMode and Update

The EditMode property does not itself modify or save database information. It only reports the current editing state of the Recordset.

The Update method performs the actual operation of committing the pending changes.

The general sequence is:

Recordset opened
      |
      v
No editing operation
      |
      v
Modify existing record
      |
      v
adEditInProgress
      |
      +------> Update ------> Changes saved
      |
      +------> CancelUpdate -> Changes discarded

For a new record, the sequence is slightly different:

AddNew
   |
   v
adEditAdd
   |
   +------> Update ------> New record saved
   |
   +------> CancelUpdate -> New record discarded

Practical Example

Consider an employee management application. The user selects an employee and changes the employee's salary.

rs.Fields("Salary").Value = 50000

If rs.EditMode = adEditInProgress Then
    rs.Update
End If

The field is first changed in the Recordset. The application then checks the EditMode property. If an existing record is being edited, Update is called to save the modification.

A Cancel button could instead use:

If rs.EditMode <> adEditNone Then
    rs.CancelUpdate
End If

This allows the application to discard the unsaved modification.

Importance of the EditMode Property

The EditMode property is useful when an application provides interactive data editing. It helps developers determine whether a Recordset contains pending changes and whether those changes relate to an existing record or a newly created record. It can also prevent unnecessary calls to Update or CancelUpdate when no editing operation is active.

In summary, the ADO Recordset EditMode property is a status indicator for Recordset editing operations. adEditNone indicates that no edit is active, adEditInProgress indicates that an existing record is being modified, and adEditAdd indicates that a new record is being created. Understanding these states helps developers build safer and more predictable ADO-based database applications.