Full Version: Me.Dirty = False not working!
UtterAccess Forums > Microsoft® Access > Access Forms
kbrewster
I have the following code on my form...
CODE
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.Dirty = True Then
    Select Case MsgBox("Want to save or not?", vbYesNo, "Save Changes?")
    Case vbYes
        Me.Dirty = False
            If IsNull(Me.POEnteredBy) Then
                Me.POEnteredBy = Forms!frmHidden!txtFullName
                Me.POEnteredDate = Now
            End If
    Case vbNo
        Me.Undo
    End Select
    End If
End Sub

I keep getting Run-time error 2115. What am I doing wrong?
Willem
Me.POEnteredDate = Now
hould be
Me.POEnteredDate = Now() ?
kbrewster
Well that was not the problem...
When I debug it goes to the line
Me.Dirty = False
niesz
You're trying to save the record in the Before Update event. It cannot do this in this event. I think you just want to cancel the update if they say NO to the save and just let the rest of the event continue if they answer YES.
kbrewster
Huh? What line in the code says I am saving it? All I am trying to do is update two controls if they say yes. I want this code to run no matter what the user does...closes the form, clicks New to add a new record, or anything else. What event could I run this on?
niesz
Setting Me.Dirty = False saves the record. Give me a second and I'll see ifI can put together a sample. You just want to confirm the save, correct?
kbrewster
Yes I just want to confirm the save and if they say no, undo all changes. Thanks!
niesz
Try this:
CODE
Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    If MsgBox("Want to save or not?", vbYesNo, "Save Changes?") = vbYes Then
        If IsNull(Me.POEnteredBy) Then
            Me.POEnteredBy = Forms!frmHidden!txtfullname
            Me.POEnteredDate = Now
        End If
    Else
        Me.Undo
        Cancel = True
    End If
    
End Sub
kbrewster
Awesome...that works great!
Thanks! frown.gif
niesz
Glad to help.
niesz
P.S. To check for empty fields, instead of using:
If IsNull(YourFieldOrControl) Then
...
..use..
If Nz(YourFieldOrControl) = "" Then
...
That way it will check for Nulls and Empty Strings. frown.gif
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.