Using Me.Dirty on form code -SOLVED

User avatar
Michael Abrams
4StarLounger
Posts: 573
Joined: 10 Feb 2010, 17:32

Using Me.Dirty on form code -SOLVED

Post by Michael Abrams »

Simple code on my form:

Code: Select all

Private Sub cmdEXIT_Click()

If MsgBox("Did you click the SAVE BUTTON?", _
                vbQuestion + vbYesNo, "CONTINUE?") = vbNo Then
                
  'IF USER SELECTS NO - THEN ALLOW TO HIT SAVE
  
        DoCmd.CancelEvent
        
Else

  'IF USER SELECTS YES - EXIT
  
        DoCmd.Close
      
End If

End Sub
Obviously if the user makes no changes to a record or decides to Exit before entering any data, the Msgbox still pops up.
I would like add the following pseudocode:
If no changes are made to this record, DoCmd Close.

I tried adding this to the beginning of the above code:

Code: Select all

If Me.Dirty = False Then
DoCmd.Close
End If
I receive an error message "You entered an expression that has an invalid reference to the property Dirty"

Can you help me with the proper syntax?

Thank you !

Michael
Last edited by Michael Abrams on 04 Sep 2014, 15:54, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using Me.Dirty on form code

Post by HansV »

Is your form bound to a record source? If the form is unbound (i.e. its Record Source property is blank), Dirty is not applicable.
Best wishes,
Hans

User avatar
Michael Abrams
4StarLounger
Posts: 573
Joined: 10 Feb 2010, 17:32

Re: Using Me.Dirty on form code

Post by Michael Abrams »

I swear I don't know how you do it HansV, but yep, it is unbound.

Is there another way to do this?
(sounds like I will need to do: if isnull(field) and isnull (field) etc... then docmd.close)

Michael

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using Me.Dirty on form code

Post by HansV »

Yes, you'll have to check the individual controls, or loop through the controls:

Code: Select all

Private Sub cmdExit_Click()
    Dim ctl As Control
    Dim f As Boolean
    For Each ctl In Me.Controls
        Select Case TypeName(ctl)
            Case "TextBox", "ComboBox"
                If Not IsNull(ctl.Value) Then
                    f = True
                    Exit For
                End If
            Case "ListBox"
                If ctl.ListIndex >= 0 Then
                    f = True
                    Exit For
                End If
        End Select
    Next ctl
    If f = False Then
        DoCmd.Close
    End If
End Sub
Best wishes,
Hans

User avatar
Michael Abrams
4StarLounger
Posts: 573
Joined: 10 Feb 2010, 17:32

Re: Using Me.Dirty on form code

Post by Michael Abrams »

Awesome !

Thanks so much !

Michael