ADO - ADO Recordset MarshalOptions Property
The MarshalOptions property is an ADO Recordset property that controls which records in a Recordset are sent or transferred when the Recordset is marshalled between processes, applications, or different tiers of an application. It is particularly relevant in applications that use disconnected Recordsets or distribute data between a client and a server.
1. What Is Marshalling in ADO?
In ADO, marshalling refers to the process of transferring an object, such as an ADO Recordset, from one environment to another. For example, a Recordset may be created on a server and then transferred to a client application.
When a Recordset contains many records, transferring the entire Recordset can consume considerable network bandwidth and processing resources. ADO provides the MarshalOptions property to determine which records should be included when the Recordset is marshalled.
The property is therefore useful when controlling the amount of Recordset data transferred between application tiers.
2. Purpose of MarshalOptions
The primary purpose of MarshalOptions is to control the records included during Recordset marshalling.
Consider a Recordset containing 10,000 records. Suppose an application has modified only a small number of records. Sending all 10,000 records back to another application may be unnecessary.
MarshalOptions can be used to control whether ADO considers:
-
All records in the Recordset
-
Only records that have been modified
This can reduce unnecessary data transfer in appropriate disconnected or distributed application scenarios.
3. Values of MarshalOptions
The MarshalOptions property uses the ADMarshalOptionsEnum enumeration.
The commonly relevant values are:
adMarshalAll — 0
This value indicates that all records in the Recordset should be marshalled.
For example, if a Recordset contains 1,000 records, the complete set of records can be included during marshalling.
adMarshalModifiedOnly — 1
This value indicates that only records that have been modified should be marshalled.
This can be useful when a client has received a Recordset, changed only a few records, and needs to transfer those changes back to another environment.
4. Example Scenario
Suppose a client application receives a Recordset containing employee information:
EmployeeID Name Department
101 Ravi Sales
102 Anitha HR
103 Kumar Finance
104 Meena IT
105 Arun Support
The client modifies only the department of employee 103.
If MarshalOptions is configured to marshal all records, ADO can include the entire Recordset during the marshalling operation.
If it is configured for modified records only, ADO can restrict the marshalled information to the records that have been modified.
This becomes particularly useful when the Recordset is large but only a small portion of its data has changed.
5. Setting MarshalOptions
In classic ADO programming, the property can be assigned using the appropriate enumeration value.
Example:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM Employees", cn, _
adOpenStatic, adLockBatchOptimistic
rs.MarshalOptions = adMarshalModifiedOnly
Here:
-
rsrepresents the Recordset. -
adOpenStaticspecifies the cursor type. -
adLockBatchOptimisticallows changes to be accumulated and submitted as a batch. -
adMarshalModifiedOnlytells ADO to marshal only modified records.
6. Relationship with Disconnected Recordsets
MarshalOptions is especially relevant to disconnected Recordsets.
A disconnected Recordset can remain available to an application after its database connection has been closed.
For example:
Database
|
v
ADO Connection
|
v
Recordset
|
v
Connection Closed
|
v
Disconnected Recordset
The application can work with the Recordset locally. Users can modify records without maintaining a continuous database connection.
Later, the Recordset or its changes can be transferred back to another environment.
In such situations, controlling what is marshalled can help reduce unnecessary data movement.
7. MarshalOptions and Batch Updates
MarshalOptions is also related to the concept of batch updating.
With batch updating, changes can be accumulated locally and submitted to the database later.
For example:
Retrieve Records
|
v
Disconnect
|
v
Modify Records
|
v
Store Changes
|
v
Marshal Changes
|
v
Reconnect
|
v
Update Database
If only a few records have changed, transferring only the necessary modified records can be more efficient than transferring the complete Recordset.
8. Difference Between MarshalOptions and Filter
MarshalOptions should not be confused with the Filter property.
The Filter property controls which records are displayed or accessible through a Recordset.
For example:
rs.Filter = "Department = 'IT'"
This filters the Recordset based on a condition.
MarshalOptions, on the other hand, deals with which records are included when the Recordset is marshalled.
Therefore:
Filter
|
+-- Controls records visible/accessed in the Recordset
MarshalOptions
|
+-- Controls records considered for marshalling
They serve different purposes.
9. Difference Between MarshalOptions and UpdateBatch
MarshalOptions should also be distinguished from UpdateBatch.
UpdateBatch is used to submit pending changes from a Recordset to the underlying data source.
For example:
rs.UpdateBatch
MarshalOptions does not itself submit changes to the database. Instead, it determines what information is included when the Recordset is transferred through the ADO marshalling mechanism.
Thus:
-
MarshalOptions controls marshalling behavior.
-
UpdateBatch applies pending changes to the data source.
10. Advantages of MarshalOptions
Using MarshalOptions appropriately can provide several benefits.
Reduced data transfer: When only modified records need to be transferred, unnecessary records can be avoided.
Better network efficiency: Smaller amounts of data can reduce network traffic in distributed applications.
Improved performance: Applications may spend less time transferring large Recordsets.
Useful for disconnected applications: It complements scenarios where users work with data locally and synchronize changes later.
Better resource utilization: Reducing the amount of data transferred can decrease memory, processing, and network requirements.
11. Important Considerations
MarshalOptions does not replace proper database synchronization or transaction management. It simply provides control over what ADO marshals.
Applications that use disconnected Recordsets should also consider:
-
Record locking
-
Batch updates
-
Update conflicts
-
Transaction handling
-
Connection management
-
Error handling
-
Data consistency
For example, if two users modify the same record independently, simply transferring modified records does not automatically resolve the resulting conflict.
12. Summary
The ADO Recordset MarshalOptions property controls which records are included when an ADO Recordset is marshalled between application environments. Its two important settings are adMarshalAll, which represents all records, and adMarshalModifiedOnly, which represents only modified records.
The property is particularly useful in disconnected Recordsets, distributed applications, and batch-update scenarios, where minimizing unnecessary data transfer can be important. It should not be confused with filtering records or applying database updates: Filter controls record visibility, UpdateBatch submits pending changes, while MarshalOptions controls the data considered during Recordset marshalling.