Introduction

This article describes a Transaction Wrapper class. It helps developers writing VBA code for Bentley Systems MicroStation™ 3D CAD tool.

You can read more questions and answers about MicroStation development by visiting the MicroStation Programming Forum.

Transactions in MicroStation

Often, we want to create or modify multiple graphic elements in a MicroStation DGN model. When contemplating a repetitious task, it makes sense to automate the operations where possible. MicroStation VBA makes it easy to write an application that performs that automation. When a user creates or modifies one or more elements, it is called a transaction.

Reversible Transactions

A MicroStation user expects to be able to undo an operation. For example, suppose your VBA application draws one hundred widgets, the user expects to be able to undo that operation. Moreover, she expects to be able to undo all one hundred widgets in one go. She does not expect to have to undo each of hundred widgets one-by-one.

Widgets

We call a set of connected operations a transaction. In the above example, the transaction is the act of creating multiple widgets. When we undo the transaction, we remove all of the widgets created during that transaction.

User expects to be able to undo an element creation operation …

Undo

Transactions in MicroStation VBA

You make a transaction reversible — or undoable — in MicroStation VBA by bracketting the operations with CommandState methods. A database developer would use the term roll back. A transaction may involve one or more element creation or manipulation operations …

CommandState.CommandName "My Command Name"
... one or more undoable element creation or modification operations
CommandState.StartDefaultCommand

The transaction is now undoable.

Transaction VBA Wrapper Class

We put those CommandState methods in class clsTransactionWrapper. That class is used like this …

Dim oWrapper As New clsTransactionWrapper
oWrapper.StartUndoableTransaction "My Command Name"
... undoable element creation or modification operations

The invisible code in the above is method clsTransactionWrapper.Terminate. Class method Terminate, of any class, is invoked automatically when an object is destroyed. Method Terminate invokes CommandState.StartDefaultCommand.

Transaction VBA Wrapper Class Code

Here's the entire code of the TransactionWrapper class …

' ---------------------------------------------------------------------
'	Code in VBA class module clsTransactionWrapper
' ---------------------------------------------------------------------
Public Sub StartUndoableTransaction (ByVal commandName As String)
	CommandState.CommandName = commandName
End Sub
' ---------------------------------------------------------------------
Private Sub Class_Terminate()
    CommandState.StartDefaultCommand
End Sub
' ---------------------------------------------------------------------

To use this code, create a new class module named clsTransactionWrapper in your MicroStation VBA project. Copy the above code into that class module. Wherever you want to be able to undo a transaction in your VBA code, use the class as described above.

Note for Object Oriented Programmers

Many programmers appreciate that Microsoft VBA is not a wholly objected-oriented language. If it were, we could write code like this …

Dim oWrapper As New clsTransactionWrapper ("My Command Name")
... undoable element creation or modification operations

The explicit method StartUndoableTransaction would be unnecessary, since the class constructor would do the required work.

But VBA is not fully committed to object orientation, and we're stuck with the explicit method as a work-around.