not going to before update

Crash1hd

Registered CyberGeek
Local time
Today, 04:38
Joined
Jan 11, 2004
Messages
143
When I use the following code it does not run the before update

Form_KeyPress
PHP:
Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyEscape Then
        DoCmd.Close
    End If
End Sub

but when I have it run through this code

CloseForm_Click
PHP:
Private Sub CloseForm_Click() 'Button
    On Error GoTo Err_CloseForm_Click
    DoCmd.Close

Exit_CloseForm_Click:
    Exit Sub

Err_CloseForm_Click:
    MsgBox Err.Description
    Resume Exit_CloseForm_Click
    
End Sub

it works fine if you are wondering this is what is in my before_update

Before_Update
PHP:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    Checkme = True
End Sub

and then it goes to onunload

Form_Unload
PHP:
Sub Form_Unload(Cancel As Integer)
On Error GoTo Form_Unload_Err

    Call Save_Check

Form_Unload_Exit:
    Exit Sub

Form_Unload_Err:
    MsgBox Error$
    Resume Form_Unload_Exit

End Sub

Save_Check
PHP:
Private Sub Save_Check()
If Checkme = True Then
Dim strMsg As String
    strMsg = "Data has changed."
    strMsg = strMsg & " Do you wish to save the changes?" & Chr(13)
    strMsg = strMsg & "Click 'Yes' to Save or 'No' to Discard the changes."

Select Case MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
    Case Is = vbYes
        Checkme = False
        If Nxt = True Then
            DoCmd.GoToRecord , , acNext
            Nxt = False
        End If
        If Prv = True Then
            DoCmd.GoToRecord , , acPrevious
            Prv = False
        End If
        'MsgBox "Yes"
        'do nothing
    Case Is = vbNo
        'MsgBox "No"
        DoCmd.RunCommand acCmdUndo
        CancelYes = False
        Checkme = False
        If Nxt = True Then
            DoCmd.GoToRecord , , acNext
            Nxt = False
        End If
        If Prv = True Then
            DoCmd.GoToRecord , , acPrevious
            Prv = False
        End If
    Case Is = vbCancel
        'MsgBox "Cancel"
        CancelYes = True
End Select
End If
If CancelYes = True Then
    'MsgBox ("This will stop it from closing")
    DoCmd.CancelEvent
    CancelYes = False
End If
If Checkme <> True Then
        If Nxt = True Then
            DoCmd.GoToRecord , , acNext
            Nxt = False
        End If
        If Prv = True Then
            DoCmd.GoToRecord , , acPrevious
            Prv = False
        End If
End If
End Sub
 
Last edited:
Code:
Private Sub Form_KeyPress(KeyAscii As Integer) 
    If KeyAscii = vbKeyEscape Then 
        Checkme = True         
        DoCmd.Close 
    End If 
End Sub


???
kh
 
If I put checkme = true on the command like that even if data has not changed it will ask I only want it to ask when data has changed? reason for the before update
 
Maybe you can use the 'Dirty' property. VBA help documents it pretty good.

???
kh
 
I recommend using the before update property of the controls on the form instead of the form before update property.

What you are doing is fine, but if you press esc without tabbing out of the field you are changing data in then there is no before update event.

If you want people to be able to esc out of the form then you should change focus so that an update event actually occurs.
 
Last edited:
That seems to work however when i use the esc key it undos the change and doesnt work right but I changed it to the spacebar just to test and it works great why is the esc key being a problem?
 
When you are working in a table (for example) and you start to type then hit esc it is treated as an undo and takes you out of the edit mode for that record.

It is one of the built in key functions (maybe wrong terminology) in access. There are keys that already have commands attached to them and esc is one of them.

So you may have to do away with esc as a means to exit a form all together. I'm sure there is a way to circumvent the key command, but I don't know how to do it.

You may be able to use on key press for the controls on the form so that you can force it to change focus before the esc undo event occurs.
 
Thankyou for the info if anyone knows a way to turn of the default for undo with the esc key please dont hesitate to post it
 
Why would you want to when any Windows user would automatically use the Escape key to back out of a programme, undo document changes etc? :confused:
 
the other program we use at work when the esc key is pressed it closes the window so I was trying to make them the same
 

Users who are viewing this thread

Back
Top Bottom