Link to home
Start Free TrialLog in
Avatar of SowleMan
SowleManFlag for United States of America

asked on

How to use me.Dirty event

I have a bound form, with a VBA procedure for the form's BeforeUpdate event. The first sentence in the procedure is
If me.dirty = False then
     goto Exit_Procedure
end if

I am unable to detect me.dirty = False, even when click nothing except the Prev, or the Next, or Stop buttons I have provided on the form. The code behind each of these buttons checks for me.dirty then saves the record.  That codes follows:

Private Sub btnStop_Click()
    If Me.Dirty Then
        MsgBox "Dirty Stop"
        DoCmd.Save
    End If
    DoCmd.Close
End Sub

Private Sub btnPrev_Click()
    If Me.Dirty Then
        MsgBox "Dirty Prev"
        DoCmd.Save
    End If
    DoCmd.GoToRecord , , acPrevious
End Sub

Private Sub btnNext_Click()
    If Me.Dirty Then
    MsgBox "Dirty Next"
        DoCmd.Save
    End If
    DoCmd.GoToRecord , , acNext
End Sub

Can you provide reasons these events always decide me.dirty = true? Even when I press no other keys except clicking on the Prev, Next, or Stop buttons?
SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The way you would typically use Dirty is in a Save button - to Save the Current record:

Private Sub btn_Save()
    Me.Dirty = False  ' this saves the record
End Sub

However ... IF .. you happen to have any validation in the Form's BeforeUpdate event - and if the validation fails (per your spec), and you Cancel the BU event ... then you need to take that into account in the Save button code.

Private Sub btn_Save()
    On Error Resume Next ' in case BU event is cancelled
    Me.Dirty = False  ' this saves the record
    On Error Goto 0  ' reset
End Sub
The other reason to use .Dirty is to  make a record dirty is to force the BeforeUpdate event outside of the user.

In the past, I used to flip a control to NULL and then back to force the record dirty.

I had control code in the Before/After update events to control record operations (display of Add, Edit, Save, Cancel Buttons).

Jim.
Avatar of SowleMan

ASKER

Thanks. I have discovered that the following code is causing the Form BeforeUpdate event to trigger:

Private Sub Partner_Enter()
    Me.Partner = Me.Partner.ItemData(0)
End Sub

Partner is a bound ComboBox in the form. My intent with the Partner_Enter event is to display the first item in the drop down list rather than make the operator click the little down arrow (often, the first item in the drop down list is the desired item).

Is there any way to retain the Partner_Enter event code and yet exit the Form BeforeUpdate code if nothing else in the form is changed?  There is a lot of validation code in the Form BeforeUpdate event that I am trying to avoid if the user has made no changes to the form.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That will work. Great alternative.
I've requested that this question be closed as follows:

Accepted answer: 200 points for JDettman's comment #a39953601
Assisted answer: 50 points for DatabaseMX's comment #a39952384
Assisted answer: 0 points for SowleMan's comment #a39953653

for the following reason:

I appreciate that Joe and Jim tried to help me. Jim's answer was better because, simply, it will make my code run the way I want it to.

Thank you.
"I am unable to detect me.dirty = False,"
I believe I explained exactly why that was ... again because the BU event was already be activated since the record was being made Dirty elsewhere, which was clearly the case - via the Enter event.

Further, dropping down a combo box automatically  upon entry really isn't good UI design. If a user is just tabbing through controls, and suddenly the combo drops down - it's very annoying to the user - and potentially a small performance hit - if the list is big.

And also noting that F4 will also drop down the list.

mx
Joe -  You are correct.  The scope of your reply did include the event that I neglected to consider.  In the future, I will view answers that I get with a wider lens.  I agree that you deserved more points. Thanks for your help.