Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Dirty Property


Message #1 by simonds@m... on Fri, 19 Apr 2002 16:29:55
I am attempting to add some validation to a form in Access 97, employing 
the Dirty property. Essentially, when the user updates (or edits) an 
existing record and moves on (either to another record or closes the form, 
etc.), I want a prompt box to come up to allow the user to save, not save 
or go back and check the record with the updated info.

I modelled this after the built-in Help file on the Dirty property. It is 
not working.

_____________________________________________________

Public Function UndoEdits()
Dim Prompt4Edits
Dim ctlC As Control
Dim frm As Form
Prompt4Edits = MsgBox("This record has been changed. Do you want to save 
the Edits?", _
                                vbYesNoCancel + vbExclamation, "Approve 
Edits")

    Set frm = Screen.ActiveForm
    
    If frm.Dirty = True Then
        If Prompt4Edits = vbYes Then    ' User chose Yes; carry on with 
the earlier action.
            DoCmd.RunCommand acCmdSaveRecord
        ElseIf Prompt4Edits = vbNo Then  ' User chose No; carry on with 
the earlier action.
            ' For each control.
            For Each ctlC In frm.Controls
                If ctlC.ControlType = acTextBox Or ctlC.ControlType = 
acCheckBox _
                   Or ctlC.ControlType = acComboBox Then  ' cover all the 
different types of controls on this form
                    ' Restore Old Value.
                    ctlC.Value = ctlC.OldValue
                End If
            Next ctlC
        Else    ' User chose Cancel, stay and review the current record 
for changes.
            DoCmd.CancelEvent
        End If
    End If
    
End Function
________________________________________________________________


I call the UndoEdits function from the form's AfterUpdate event. So far it 
is the vbNo and the vbCancel parts that are not working.


In the archives I found 
http://p2p.wrox.com/archive/vbpro_databases/2001-03/8.asp
which discusses the use of the Dirty property in a way that I am not sure 
how to invoke here.

Any help would be greatly appreciated.

Regards,
Eric
Message #2 by "John Ruff" <papparuff@c...> on Fri, 19 Apr 2002 09:14:19 -0700
This is a multi-part message in MIME format.

------=_NextPart_000_0007_01C1E782.970A6490
Content-Type: text/plain;
	charset="US-ASCII"
Content-Transfer-Encoding: 7bit

If you are going to use a form's event to call the UndoEdit function,
you need to use the BeforeUpdate Event.  If you use the AfterUpdate
event, the record has already been saved and the form's dirty property
has already been set to false. 

In actually, the UndoEdit function is never processed.  You think it is
when you select yes, but the record was saved before then and, again the
form's Dirty property is set to false before the UndoEvents procedure is
run.

To test this;
1 Place a stop command just before the line If frm.Dirty = True Then
2 Call the call to the UndoEdit procedure from the form's AfterUpdate
event.
3 Open the form and change any field in the record that is displayed.
4 Move to the next record.
5 Select either Yes or No when the messagebox appears.

The VBE is opened and you are at the Stop command.  If you place your
cursor over the frm.Dirty statement, you will notice that its value is
false.  This is because the record was already saved and the form's
Dirty property reset to false.

Now
1 Place the call to the UndoEdit procedure in the form's BeforeUpdate
event
2 Open the form and change a value in any field in the record that is
displayed.
3 Move to the next record.
4 Select No when the messagebox appears.

The VBE opens and you are again at the Stop command.  If you place the
cursor over the frm.Dirty statement, you will now notice that its value
is true.  This is because the record has yet to be saved and the form's
Dirty property as not been reset to false.

5 Press the F5 key so the UndoEdit procedure will process.
6 Go back to the form and move to the previous record.  You will see
that the original value is still intact.

I've changed your UndoEdit procedure a little, so if you want to cut and
paste it into yours, feel free to do so.  I placed a stop statement in
for procedure (it is remarked out) and I changed the if..then statement
to a Select Case statement for the ctlC.ControlType

    Dim Prompt4Edits
    Dim ctlC As Control
    Dim frm As Form
    Dim strMsg As String
   
    strMsg = "This record has been changed. " & _
            "Do you want to save the Edits?"
   
    Prompt4Edits = MsgBox(strMsg, vbYesNoCancel + vbExclamation,
"Approve Edits")
   
        Set frm = Screen.ActiveForm
'Stop
        If frm.Dirty Then
            ' User chose Yes; carry on with the earlier action.
            If Prompt4Edits = vbYes Then
                DoCmd.RunCommand acCmdSaveRecord
            ' User chose No; carry on with the earlier action.
            ElseIf Prompt4Edits = vbNo Then
                ' For each control.
                For Each ctlC In frm.Controls
                    Select Case ctlC.ControlType
                        ' Cover all the different types of controls on
this form
                        Case acTextBox, acCheckBox, acComboBox
                            ' Restore Old Value.
                            ctlC.Value = ctlC.OldValue
                    End Select
                Next ctlC
            ' User chose Cancel, stay and review the current record for
changes.
            Else
                DoCmd.CancelEvent
            End If
        End If



John Ruff - The Eternal Optimist :-) 
Always looking for Contract Opportunities 
  
9306 Farwest Dr SW 
Lakewood, WA 98498 
papparuff@c... 



-----Original Message-----
From: simonds@m... [mailto:simonds@m...]
Sent: Friday, April 19, 2002 4:30 PM
To: Access
Subject: [access] Dirty Property



I am attempting to add some validation to a form in Access 97, employing
the Dirty property. Essentially, when the user updates (or edits) an
existing record and moves on (either to another record or closes the
form,
etc.), I want a prompt box to come up to allow the user to save, not
save
or go back and check the record with the updated info.

I modelled this after the built-in Help file on the Dirty property. It
is
not working.

_____________________________________________________

Public Function UndoEdits()
Dim Prompt4Edits
Dim ctlC As Control
Dim frm As Form
Prompt4Edits = MsgBox("This record has been changed. Do you want to save
the Edits?", _
                                vbYesNoCancel + vbExclamation, "Approve
Edits")

    Set frm = Screen.ActiveForm
   
    If frm.Dirty = True Then
        If Prompt4Edits = vbYes Then    ' User chose Yes; carry on with
the earlier action.
            DoCmd.RunCommand acCmdSaveRecord
        ElseIf Prompt4Edits = vbNo Then  ' User chose No; carry on with
the earlier action.
            ' For each control.
            For Each ctlC In frm.Controls
                If ctlC.ControlType = acTextBox Or ctlC.ControlType 
acCheckBox _
                   Or ctlC.ControlType = acComboBox Then  ' cover all
the
different types of controls on this form
                    ' Restore Old Value.
                    ctlC.Value = ctlC.OldValue
                End If
            Next ctlC
        Else    ' User chose Cancel, stay and review the current record
for changes.
            DoCmd.CancelEvent
        End If
    End If
   
End Function
________________________________________________________________


I call the UndoEdits function from the form's AfterUpdate event. So far
it
is the vbNo and the vbCancel parts that are not working.


In the archives I found
http://p2p.wrox.com/archive/vbpro_databases/2001-03/8.asp
which discusses the use of the Dirty property in a way that I am not
sure
how to invoke here.

Any help would be greatly appreciated.

Regards,
Eric





  Return to Index