MS Transactions: What you should know

0
229
Microsoft Access is a terrific tool that provides a potent development environment for work applications. Microsoft Access can be used for a variety of things. For business applications, it is more comparable to a cloud. You may handle all of your information in one file. Thanks to its big storage capacity.

You are free to use tables within the access database files to store your data, construct reports to examine data in a specific pattern, Microsoft Access transactions, etc. Continue reading to learn how Microsoft Access is used to enter transactions using VBA. 

A brief about transactions

In simple words, a transaction is a fundamental concept of a relational database. In order to manage vast amounts of data in numerous locations with less effort, people use transactions and consolidate various operations into a single unit. 

All modifications are undone in the event of a transaction error, returning everything to how it was originally. For instance, the sending and receiving banks must have the same amount debited and credited in order for the financial transaction to be considered complete. The transaction automatically rolls back to the sender account if the recipient does not receive a credit in his account.

MS Transactions

Transactions are supported by both Access and the Jet/ACE-Database Engine. And for each action query that is run, it implicitly and by default uses them. Since only one query is run as part of the transaction, you rarely even notice one is taking place. However, it guarantees that all modifications made to the data by the query can be undone in the event of a mistake.

You can use explicit transactions in the VBA code also. This allows you to combine numerous distinct queries into a single transaction, after which you can either commit all of the changes or roll them all back. You can do this regardless of whether the queries are successful or unsuccessful since you are using your code to control the transaction.

Implementing transactions in VBA code

Implementation is comparatively easy. Utilizing the BeginTrans-Method, you tell the DBEngine to begin a transaction before running the first action query. The action queries that you want to include in the transaction are then all executed.

If there was no problem, you typically execute the CommitTrans-Method to commit the transaction and preserve all changes made by all queries. But the choice is yours. Even if all of the queries were successful and without errors, you can still roll back a transaction using the Rollback-Method. Even if some of the queries within a transaction fail, you can still commit it.

The process seems quite simple. However, you need to beware of some things like-

  • Handling potential issues that may arise while running the queries is crucial. You must roll back the transaction in the event of an error. If not, the database pages—internal storage units for multiple records—that were impacted by the transaction will stay locked, preventing other users from making changes to those records. As a result, until the Access instance that was initiating the transaction is closed, a portion of your database will be in a read-only state for everyone else. This reverses the transaction.
  • Furthermore, using the dbFailOnError-Option for the CurrentDb.Execute-Method. If you omit this option, the action SQL statement can fail.