Hello all,
I am trying to handle a scenario where a user clicks the 'X' close control on a form window border. I want to force the user to use the form's 'save' button to write the updated record. (If the form is not dirty, the 'X' control will just close it silently.)
In the BeforeUpdate event I ask the user if they want to discard their changes (this code is skipped if the user *actually* pressed the save button). If the user answers 'No', I want the form to remain open for editing and not actually close.
When I set Cancel = True in the BeforeUpdate sub and exit, Access pops up a yes/no dialog window stating: "You can't save this record at this time. The application may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to close the database object anyway?"
In this case (cancel = true) I don't want the object (form) closed. Is there a way to keep the form open as the default behavior AND prevent Access from popping up the nuisance question window?
Using: Intel P4 / Win 2K / Access 2003 / VBA
Thank you!
Ed
8 66821
Hello all,
I am trying to handle a scenario where a user clicks the 'X' close control on a form window border. I want to force the user to use the form's 'save' button to write the updated record. (If the form is not dirty, the 'X' control will just close it silently.)
In the BeforeUpdate event I ask the user if they want to discard their changes (this code is skipped if the user *actually* pressed the save button). If the user answers 'No', I want the form to remain open for editing and not actually close.
When I set Cancel = True in the BeforeUpdate sub and exit, Access pops up a yes/no dialog window stating: "You can't save this record at this time. The application may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to close the database object anyway?"
In this case (cancel = true) I don't want the object (form) closed. Is there a way to keep the form open as the default behavior AND prevent Access from popping up the nuisance question window?
Using: Intel P4 / Win 2K / Access 2003 / VBA
Thank you!
Ed
Try this -
Private Sub Form_Unload(Cancel As Integer)
-
If MsgBox("Would you like to discard changes?", vbYesNo) = vbNo Then
-
Cancel = True
-
End If
-
End Sub
-
Originally Posted by evn678
Hello all,
I am trying to handle a scenario where a user clicks the 'X' close control on a form window border. I want to force the user to use the form's 'save' button to write the updated record. (If the form is not dirty, the 'X' control will just close it silently.)
In the BeforeUpdate event I ask the user if they want to discard their changes (this code is skipped if the user *actually* pressed the save button). If the user answers 'No', I want the form to remain open for editing and not actually close.
When I set Cancel = True in the BeforeUpdate sub and exit, Access pops up a yes/no dialog window stating: "You can't save this record at this time. The application may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to close the database object anyway?"
In this case (cancel = true) I don't want the object (form) closed. Is there a way to keep the form open as the default behavior AND prevent Access from popping up the nuisance question window?
Using: Intel P4 / Win 2K / Access 2003 / VBA
Thank you!
Ed
Try this
Code: ( text )
1. Private Sub Form_Unload(Can cel As Integer)
2. If MsgBox("Would you like to discard changes?", vbYesNo) = vbNo Then
3. Cancel = True
4. End If
5. End Sub
Thanks, but the unload does not occur when the update is canceled. Access still pops up the nuisance message.
Denburt 1,356
Recognized Expert Top Contributor
I think something along these lines will accomplish what you are seeking. - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Save_Response = MsgBox("You Are About To Save This Record. Do You Want To Continue?", vbQuestion + vbApplicationModal + vbYesNo, "Save Record?")
-
If Save_Response <> vbYes Then
-
Cancel = True
-
Me.Undo
-
End If
-
End Sub
-
Private Sub Form_Error(DataErr As Integer, Response As Integer)
-
Select Case DataErr
-
Case 2169
-
Response = acDataErrContinue
-
Case Else
-
Response = acDataErrDisplay
-
End Select
-
End Sub
-
Private Sub Form_Unload(Cancel As Integer)
-
Cancel = True
-
End Sub
-
Thanks, but the unload does not occur when the update is canceled. Access still pops up the nuisance message.
Don't use the before update event. Just use the unload event.
I think something along these lines will accomplish what you are seeking. - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Save_Response = MsgBox("You Are About To Save This Record. Do You Want To Continue?", vbQuestion + vbApplicationModal + vbYesNo, "Save Record?")
-
If Save_Response <> vbYes Then
-
Cancel = True
-
Me.Undo
-
End If
-
End Sub
-
Private Sub Form_Error(DataErr As Integer, Response As Integer)
-
Select Case DataErr
-
Case 2169
-
Response = acDataErrContinue
-
Case Else
-
Response = acDataErrDisplay
-
End Select
-
End Sub
-
Private Sub Form_Unload(Cancel As Integer)
-
Cancel = True
-
End Sub
-
Thank you !! This is the closest thing to answering my question "Is there a way to prevent Access from popping up the nuisance question window?"
I had not considered using the form error event to trap and suppress the error. I already had all the necessary logic in beforeupdate and unload. The acDataErrContin ue prevents the Access pop up.
But there is one small problem - after the acDataErrContin ue, the form loses all data updates as if a Me.Undo was executed. I suspect that using the acDataErrContin ue is equivalent to answering 'Yes' to the suppressed dialog, which results in the form being reset to its original data. [Without the form error handler] After canceling the update, Access pops the question: "You can't save this record at this time. The application may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to close the database object anyway?"
My original goal in all this was to preserve those changes so the user would press the 'Save' button and not the frame control box 'X'. Without the form error trap, I was just selecting 'No' on the access dialog, but this was redundant because my VB code in beforeupdate already asked the user this question and set cancel = true based on the user's 'No' answer. The unload code also correctly traps for this condition and executes a cancel.
The acDataErrContin ue suppresses the dialog but doesn't preserve the form data. Any idea on how I can accomplish that?
Thanks very much for the help! You have made my app much better!
Ed
Denburt 1,356
Recognized Expert Top Contributor
Tried and tested copy/paste solution: - Option Compare Database
-
Option Explicit
-
Dim BoolDirty As Boolean
-
-
Private Sub Command0_Click()
-
On Error Resume Next
-
DoCmd.RunCommand acCmdSelectRecord
-
DoCmd.RunCommand acCmdSaveRecord
-
If BoolDirty = True Then
-
DoCmd.Close acForm, Me.Name
-
End If
-
End Sub
-
-
Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Dim Save_Response As Integer
-
Save_Response = MsgBox("You Are About To Save This Record. Do You Want To Continue?", vbQuestion + vbApplicationModal + vbYesNo, "Save Record?")
-
If Save_Response <> vbYes Then
-
Cancel = True
-
Me.Undo
-
Else
-
If Not Me.Dirty Then
-
DoCmd.Close acForm, Me.Name
-
Else
-
BoolDirty = True
-
End If
-
End If
-
End Sub
-
Private Sub Form_Error(DataErr As Integer, Response As Integer)
-
'Call myBad
-
Select Case DataErr
-
Case 3314
-
DoCmd.Beep
-
MsgBox "A required field is blank.", vbCritical
-
Response = acDataErrContinue
-
Case 2113
-
DoCmd.Beep
-
MsgBox "Wrong Data Type. " & vbCr & _
-
"Example: You may have entered text in a date field.", vbCritical
-
Response = acDataErrContinue
-
Case 2169
-
If MsgBox("Record is incomplete. Your changes will not be saved." & vbCr & _
-
"Do you want to review your changes?", vbYesNo, "Record Error") = vbYes Then
-
Response = acDataErrContinue
-
Else
-
Response = acDataErrContinue
-
End If
-
Case Else
-
Response = acDataErrDisplay
-
End Select
-
Debug.Print Err.Number & " " & Err.Description
-
End Sub
Denburt 1,356
Recognized Expert Top Contributor
Tried and tested copy/paste solution: - Option Compare Database
-
Option Explicit
-
Dim BoolDirty As Boolean
-
-
Private Sub Command0_Click()
-
On Error Resume Next
-
DoCmd.RunCommand acCmdSelectRecord
-
DoCmd.RunCommand acCmdSaveRecord
-
If BoolDirty = True Then
-
DoCmd.Close acForm, Me.Name
-
End If
-
End Sub
-
-
Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Dim Save_Response As Integer
-
Save_Response = MsgBox("You Are About To Save This Record. Do You Want To Continue?", vbQuestion + vbApplicationModal + vbYesNo, "Save Record?")
-
If Save_Response <> vbYes Then
-
Cancel = True
-
Me.Undo
-
Else
-
If Not Me.Dirty Then
-
DoCmd.Close acForm, Me.Name
-
Else
-
BoolDirty = True
-
End If
-
End If
-
End Sub
-
Private Sub Form_Error(DataErr As Integer, Response As Integer)
-
'Call myBad
-
Select Case DataErr
-
Case 3314
-
DoCmd.Beep
-
MsgBox "A required field is blank.", vbCritical
-
Response = acDataErrContinue
-
Case 2113
-
DoCmd.Beep
-
MsgBox "Wrong Data Type. " & vbCr & _
-
"Example: You may have entered text in a date field.", vbCritical
-
Response = acDataErrContinue
-
Case 2169
-
If MsgBox("Record is incomplete. Your changes will not be saved." & vbCr & _
-
"Do you want to review your changes?", vbYesNo, "Record Error") = vbYes Then
-
Response = acDataErrContinue
-
Else
-
Response = acDataErrContinue
-
End If
-
Case Else
-
Response = acDataErrDisplay
-
End Select
-
Debug.Print Err.Number & " " & Err.Description
-
End Sub
Good luck let us know how it goes.
Thanks for the help!
My final solution was to save the values of all the controls before exiting the beforeupdate sub and then restore all the values before exiting the unload sub. It may not be pretty, but it works. Here is the relevant code (there is a lot of other code in the form that I have omitted).
Ed - Option Compare Database
-
Option Explicit
-
Private saveBtnClicked As Boolean
-
Private cancelBtnClicked As Boolean
-
Private formCancel As Boolean
-
Private editSaveData As New Collection 'a collection of parameters or values, all strings
-
Const SAVEFORM As Byte = 1
-
Const FILLFORM As Byte = 2
-
-
Private Sub Form_BeforeUpdate(Cancel As Integer)
-
-
Dim Response As VbMsgBoxResult
-
Dim Msg, Title As String
-
Dim Style As VbMsgBoxStyle
-
-
On Error GoTo Err_Form_BeforeUpdate
-
-
' User clicked the 'X' control on the window border
-
If (Not saveBtnClicked) And (Not cancelBtnClicked) Then
-
Msg = "Are you sure you want to undo your changes?" ' Define message.
-
' Define buttons.
-
Style = vbYesNo + vbApplicationModal + vbDefaultButton2 + vbQuestion
-
Title = "Cancel Edit Ticket" ' Define title.
-
' Display message.
-
Response = msgBox(Msg, Style, Title)
-
Cancel = True
-
-
If Response = vbNo Then ' User does not want to loose changes
-
formCancel = False ' Cancels Unload, otherwise Unload is allowed
-
procFormData SAVEFORM ' Save the form data to be restored in Unload
-
Else
-
formCancel = True ' Allows Unload, otherwise Unload is canceled
-
End If
-
End If
-
-
Exit_Form_BeforeUpdate:
-
Exit Sub
-
-
Err_Form_BeforeUpdate:
-
msgBox "FrmTT_Edit_One:Form_BeforeUpdate Error- " & Err.Description, _
-
vbExclamation, "Error No: " & Err.Number
-
Resume Exit_Form_BeforeUpdate
-
-
End Sub
-
-
Private Sub Form_Error(DataErr As Integer, Response As Integer)
-
-
Select Case DataErr
-
Case 2169
-
Response = acDataErrContinue
-
Case Else
-
Response = acDataErrDisplay
-
End Select
-
End Sub
-
-
Private Sub Form_Open(Cancel As Integer)
-
saveBtnClicked = False
-
cancelBtnClicked = False
-
formCancel = True
-
End Sub
-
-
Private Sub Form_Unload(Cancel As Integer)
-
-
If (Not saveBtnClicked) And (Not cancelBtnClicked) And (Not formCancel) Then
-
Cancel = True
-
procFormData FILLFORM ' Restore form data
-
End If
-
-
End Sub
-
-
Private Sub frmSaveData(ByVal strParmName As String, ByVal varValue As Variant)
-
-
On Error GoTo Err_frmSaveData
-
-
editSaveData.Remove strParmName ' if doesn't exist, no error
-
editSaveData.Add Item:=varValue, Key:=strParmName ' replaces prior value
-
-
Exit_frmSaveData:
-
Exit Sub
-
-
Err_frmSaveData:
-
Select Case Err.Number
-
Case 5
-
Resume Next ' no value was already set for the parm
-
Case Else
-
msgBox "FrmTT_Edit_One:frmSaveData Error- " & Err.Description, _
-
vbExclamation, "Error No: " & Err.Number
-
End Select
-
Resume Exit_frmSaveData
-
-
End Sub
-
-
Public Function frmGetData(ByVal strParmName As String) As Variant
-
-
On Error GoTo Err_frmGetData
-
-
frmGetData = editSaveData.Item(strParmName)
-
-
Exit_frmGetData:
-
Exit Function
-
-
Err_frmGetData:
-
Select Case Err.Number
-
Case 5 ' null
-
frmGetData = Null
-
Resume Exit_frmGetData
-
Case Else
-
msgBox "FrmTT_Edit_One:frmGetData Error- " & Err.Description, _
-
vbExclamation, "Error No: " & Err.Number
-
End Select
-
Resume Exit_frmGetData
-
-
End Function
-
-
Private Sub procFormData(action As Byte)
-
-
Dim ctl As Access.Control
-
Dim savName As String
-
-
For Each ctl In Me.Controls
-
If (ctl.ControlType = acTextBox) Or (ctl.ControlType = acComboBox) Then
-
savName = "SAV" & ctl.Name
-
Select Case action
-
Case SAVEFORM
-
frmSaveData savName, ctl.Value
-
Case FILLFORM
-
ctl.Value = frmGetData(savName)
-
End Select
-
End If
-
Next ctl
-
-
End Sub
-
This question is closed. The thread may be closed.
Thanks again!
Ed
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: R Duncan |
last post by:
Hi,
I'm considering moving a payroll application out of Microsoft Access to some
web-based solution. It is getting way to big for Access and the system is
growing unstable, so I'm learning PHP and MySQL and things are looking very
promising.
There is one piece of this I am having problems with. The attendance module
shows users all their...
|
by: InvisibleDuncan |
last post by:
I have a ListView that populates some fields whenever the user selects an item. However, if they change the data in the fields and then select a new item without saving, I want to display a message box that warns that their changes will be lost, and asks them to confirm whether to continue. Sounds simple
Unfortunately, the ListView's...
|
by: Steve V |
last post by:
I'm using Access 2000 to build a budgeting/tracking database.
Can I make a validation rule (using VBA) that checks the data as if
the record has already been added?
I've got 5 tables (only the necessary fields are shown here):
tblBudgetCategories
catName:Text
catTaxTrack:Yes/No
tblBudgetPeriods
|
by: todholt |
last post by:
Hello,
I am trying to bypass a form's automatic update, and instead call a
stored procedure in sql server. I am using continuous forms. The
problem I am having is with returning to the next selected record once
I have performed the update.
i.e. user edits record A and then clicks on record B. Record A is
updated, and selection moves...
|
by: Deano |
last post by:
My beforeupdate event on the main form fires when a change has been made and
the record has not been saved (2 variables are used to track this).
Works great but if they make a change in my subform which is in one tab of a
tab control, and then they select a different tab, the beforeupdate event
fires.
So they get prompted as to whether...
| |
by: MLH |
last post by:
Would like to examine the value entered into a textbox
on an A97 form during the BeforeUpdate event. The
textbox may or may not have had an earlier entry in it
prior to the latest value that is now in the process of
being entered. What's the best way to refer to the
value just typed that is about to update the textbox?
|
by: lorirobn |
last post by:
Hi,
I have a form with a continuous subform. I am working on putting
validations in for the subform's required fields. Being somewhat new
to Access (or rather, an antiquated mainframe programmer), I finally
figured out that the place to put the validations is in
Form_BeforeUpdate.
I have 2 questions:
1) once I determine there is an...
|
by: adolph |
last post by:
Hi,
I have a beforeupdate evbent that tests to see that a customer chosen
before the user can enter an amount. If the customer has not been
chosen yet, then I would like to popup a message informing the user to
first choose a customer. After the user has clicked on OK, I want the
code to undo the amount field and set focus to a field in a...
|
by: raamay |
last post by:
Hi Experts, here i come again and wanna ask how we can perform record update in PHP & MySQL without passing the id in the URL for the $_GET method to be used in the processing page. I mean I have records of tenders displayed in a page and each of the records are hyper linked to edit and delete links. And at present what i am doing is embedding id...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |