adizukerman
IS-IT--Management
Before I state my problem, I want to let you to know that I am not an access programmer so please take that into consideration when writing your replies. I’m volunteering my time to assist a non-profit organization in the creation of a database to track their students, donors and orders. Any help would be appreciated.
The problem that I am running into is that I am trying to create an undo feature on a form with a sub-form. I know how to undo the main form (me.undo) and, thanks to Access 20002 Desktop Developer’s Handbook, I can undo a sub-form. But I can’t have them work perfectly together. The bug appears in the following situation.
1) Edit the main form (form becomes dirty)
2) I click on the sub-form
3) The main form automatically saves the record (now the me.Undo won’t help)
4) I make changes to a record on the sub-form
5) I click on either another sub-form record or the main form causing the sub-form to save changes to the record
5) I click the undo button which reserves the changes to the sub-form
6) The main form does NOT undo its changes <- PROBLEM
I need the main form to save records because the sub-form records use the primary key from the main form in its own records.
Below is the code I’m using based on the Access 20002 Desktop Developer’s Handbook (great book). Can someone please suggest a change to allow the undo button to undo ALL changes to both the form and sub-form records made since the user began editing either the form and/or sub-form?
In the sub-form
Private Sub Form_Dirty(Cancel As Integer)
If isSubForm(Me) Then
Me.Parent.cmdUndo.Enabled = True
End If
End sub
Private Sub Form_Undo(Cancel As Integer)
If isSubForm(Me) Then
Me.Parent.cmdUndo.Enabled = False
End If
End Sub
In the Main form
In form module header:
Private mwks As DAO.workspace
Private mblnInTrans As Boolean
‘ Query to link the main form to the sub-form
Private Const adhcSource As String = "qryMainLink"
Private Sub ResetData()
Dim db As DAO.database
Dim qdf As DAO.querydef
Dim rst As DAO.Recordset
Dim prm As DAO.Parameter
Dim strSource As String
' Note the intentional lack of error handling.
' This one's just an example
' Assume user wants to commit changes in subform,
' if they haven't canceled them
If mblnInTrans then
mwks.CommitTrans
End if
' Create and assign filtered recordset for subform
Set mwks = DBEngine.CreateWorkspace("mwks", "admin", ""
Set db = mwks.opendatabase(CurrentDb.Name)
Set qdf = db.queryDefs(adhcSource)
' This will fail if the query has any parameters
' that aren't based on form values that it can resolve
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Me.Painting = False
Set rst = qdf.OpenRecordset
rst.LockEdits = False
Set frmSub.Form.Recordset = rst
Me.Painting = True
'Start a new subform transaction
mwks.BeginTrans
mblnInTrans = True
cmdUndo.Enabled = False
End Sub
Private Sub Form_Current()
Call ResetData
End Sub
Private Sub cmdUndo_Click()
Me.Undo
If mblnInTrans Then
mwks.Rollback
mblnInTrans = False
' shift focus, so button can be disabled
main_text.SetFocus
Call ResetData
End If
End sub
Again, thank you for taking the time to look at this.
- Adi Zukerman
adizukerman@hotmail.com
The problem that I am running into is that I am trying to create an undo feature on a form with a sub-form. I know how to undo the main form (me.undo) and, thanks to Access 20002 Desktop Developer’s Handbook, I can undo a sub-form. But I can’t have them work perfectly together. The bug appears in the following situation.
1) Edit the main form (form becomes dirty)
2) I click on the sub-form
3) The main form automatically saves the record (now the me.Undo won’t help)
4) I make changes to a record on the sub-form
5) I click on either another sub-form record or the main form causing the sub-form to save changes to the record
5) I click the undo button which reserves the changes to the sub-form
6) The main form does NOT undo its changes <- PROBLEM
I need the main form to save records because the sub-form records use the primary key from the main form in its own records.
Below is the code I’m using based on the Access 20002 Desktop Developer’s Handbook (great book). Can someone please suggest a change to allow the undo button to undo ALL changes to both the form and sub-form records made since the user began editing either the form and/or sub-form?
In the sub-form
Private Sub Form_Dirty(Cancel As Integer)
If isSubForm(Me) Then
Me.Parent.cmdUndo.Enabled = True
End If
End sub
Private Sub Form_Undo(Cancel As Integer)
If isSubForm(Me) Then
Me.Parent.cmdUndo.Enabled = False
End If
End Sub
In the Main form
In form module header:
Private mwks As DAO.workspace
Private mblnInTrans As Boolean
‘ Query to link the main form to the sub-form
Private Const adhcSource As String = "qryMainLink"
Private Sub ResetData()
Dim db As DAO.database
Dim qdf As DAO.querydef
Dim rst As DAO.Recordset
Dim prm As DAO.Parameter
Dim strSource As String
' Note the intentional lack of error handling.
' This one's just an example
' Assume user wants to commit changes in subform,
' if they haven't canceled them
If mblnInTrans then
mwks.CommitTrans
End if
' Create and assign filtered recordset for subform
Set mwks = DBEngine.CreateWorkspace("mwks", "admin", ""
Set db = mwks.opendatabase(CurrentDb.Name)
Set qdf = db.queryDefs(adhcSource)
' This will fail if the query has any parameters
' that aren't based on form values that it can resolve
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Me.Painting = False
Set rst = qdf.OpenRecordset
rst.LockEdits = False
Set frmSub.Form.Recordset = rst
Me.Painting = True
'Start a new subform transaction
mwks.BeginTrans
mblnInTrans = True
cmdUndo.Enabled = False
End Sub
Private Sub Form_Current()
Call ResetData
End Sub
Private Sub cmdUndo_Click()
Me.Undo
If mblnInTrans Then
mwks.Rollback
mblnInTrans = False
' shift focus, so button can be disabled
main_text.SetFocus
Call ResetData
End If
End sub
Again, thank you for taking the time to look at this.
- Adi Zukerman
adizukerman@hotmail.com