ACC2000: How to Control Bound Form Transactions in Access Databases

ID: Q248011


The information in this article applies to:
  • Microsoft Access 2000

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access database (.mdb).


SUMMARY

This article shows you how use Data Access Objects (DAO) to control transactions in a form that is based on a table (a bound form) in a Microsoft Access (Jet) database.


MORE INFORMATION

Typically, a developer needs to be able to let users of a database application commit all or none of their data changes on a form. In versions of Access earlier than version 2000, you could not encapsulate data changes made on bound forms within transactions. This was because all transactions in bound forms were handled in a separate workspace, accessible only to Microsoft Access.

In Microsoft Access 2000, however, you can use a combination of Data Access Objects (DAO) and the new, form Recordset property to provide this capability. The example in this article assumes that the user wants to be prompted to commit all changes made to the form's data when the user closes the form. At that time, the user can either commit all of the changes or none of the changes.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you need to reference the Microsoft DAO 3.6 Object Library.

NOTE: This example is designed to work with single main forms. It is not designed to work with linked subforms.

Controlling Transactions on Bound Forms

  1. Open the sample database Northwind.mdb.


  2. Create a new form based on the Customers table.


  3. Add all fields to the detail section of the form.


  4. On the View menu, click Code to view the module of the form, and then add the following code to the module:


  5. 
    Option Compare Database
    Option Explicit
    
    Private boolFrmDirty As Boolean
    Private boolFrmSaved As Boolean
    
    Private Sub Form_AfterDelConfirm(Status As Integer)
        If Me.Saved = False Then Me.Saved = (Status = acDeleteOK)
    End Sub
    
    Private Sub Form_AfterUpdate()
        Me.Saved = True
    End Sub
    
    Private Sub Form_Delete(Cancel As Integer)
        If Me.Dirtied = False Then DBEngine.BeginTrans
        Me.Dirtied = True
    End Sub
    
    Private Sub Form_Dirty(Cancel As Integer)
        If Me.Dirtied = False Then DBEngine.BeginTrans
        Me.Dirtied = True
    End Sub
    
    Private Sub Form_Open(Cancel As Integer)
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Set db = CurrentDb
        Set rs = db.OpenRecordset("SELECT * FROM Customers", dbOpenDynaset)
        Set Me.Recordset = rs
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Dim msg As Integer
        If Me.Saved Then
            msg = MsgBox("Do you want to commit all changes?", vbYesNoCancel)
            Select Case msg
                Case vbYes
                    DBEngine.CommitTrans
                Case vbNo
                    DBEngine.Rollback
                Case vbCancel
                    Cancel = True
            End Select
        Else
            If Me.Dirtied Then DBEngine.Rollback
        End If
    End Sub
    
    Public Property Get Dirtied() As Boolean
        Dirtied = boolFrmDirty
    End Property
    
    Public Property Let Dirtied(boolFrmDirtyIn As Boolean)
        boolFrmDirty = boolFrmDirtyIn
    End Property
    
    Public Property Get Saved() As Boolean
        Saved = boolFrmSaved
    End Property
    
    Public Property Let Saved(boolFrmSavedIn As Boolean)
        boolFrmSaved = boolFrmSavedIn
    End Property 
  6. On the Debug menu, click Compile Northwind.


  7. On the File menu, click Save Northwind.


  8. When you are prompted, save the form as frmCustomers.


  9. Open the frmCustomers form in Form view. Note that the form is populated with data.


  10. Make some changes by editing an existing record, by inserting some new records, and by deleting some records.


  11. Close the form. Note that you are prompted if you want to commit all changes.


  12. Click No.


  13. Open the form in Form view. Note that none of the changes that you made in step 9 were saved to the database.


  14. Repeat steps 9 and 10.


  15. When you are prompted to commit all changes, click Yes.


  16. Open the form in Form view. Note that all the changes that you made were saved to the database.



REFERENCES

For additional information about controlling bound form transactions in an Access project (ADP) file, click the article number below to view the article in the Microsoft Knowledge Base:

Q200880 ACC2000: Controlling Bound Form Transactions in Access Projects

Additional query words: inf

Keywords : kbdta
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: January 5, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.