Hey everybody!
I've created 2 buttons that I've placed on all my forms: a Save button & a Cancel button.
The cancel button I have a question about.
I used the wizard to create the button and I selected the Record Operations/Undo Record option. I edited the vba code to have the button also close the form after it undoes any changes. Here is the actual code for the OnClick event for the cancel button: - Private Sub cmdCancel_Click()
-
On Error GoTo errHandle
-
-
Dim intProcced As Integer
-
-
'Warn the user that any information entered will be lost, then if the user responds that it is
-
'ok, erase the record and close the form
-
-
intProcced = MsgBox("Warning: Any changes made or any information entered will not be saved.", vbOKCancel, "Warning")
-
If intProcced = 2 Then
-
Exit Sub
-
End If
-
-
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer1X
-
-
DoCmd.Close
-
-
Exit Sub
-
-
errHandle:
-
If Err.Number = 2046 Then
-
DoCmd.Close
-
Exit Sub
-
Else
-
'Print the error message and send an email to Administrator
-
SendErrorMsg "cmdCancel_Click on frmEnterNewFeeder", Err.Number, Err.Description
-
MsgBox Err.Description
-
End If
-
-
Exit Sub
-
-
End Sub
The problem I am having is when someone gets into the form and makes NO changes (doesn't type anything at all). If they click the Cancel button at that point, this message pops up:
"The command or action Undo isn't available now."
Which is true, nothing was done to "undo". What I want the button to do, is undo only if a change has been made. How can I (in the vba code) check first that a change has been made on the form and only execute the undo if a change has been made, otherwise I just want to close the form.
The Error checking and check for the error condition and ignore it code that I have there doesn't work!
Thank so much for your help!
5 9868 Rabbit 12,516
Recognized Expert Moderator MVP
The error trapping should work if that's the right error number. You could also try checking to see if it's dirty first.
for some reason, the error trapping isn't working correctly, is there something else I could try with the errhandle? That onDirty command you mentioned...how would I do that?
Minion 108
Recognized Expert New Member
Try altering the top half of your code to read the following: -
Private Sub cmdCancel_Click()
-
On Error GoTo errHandle
-
-
Dim intProcced As Integer
-
-
'Warn the user that any information entered will be lost, then if the user responds that it is
-
'ok, erase the record and close the form
-
-
'------------------------------------
-
' Add this piece here
-
' -----------------------------------
-
If Me.Dirty = True Then
-
DoCmd.Close acForm
-
Exit Sub
-
End IF
-
' ==================
-
-
-
-
intProcced = MsgBox("Warning: Any changes made or any information entered will not be saved.", vbOKCancel, "Warning")
-
If intProcced = 2 Then
-
Exit Sub
-
End If
-
See if that does the trick for you.
- Minion -
for some reason, the error trapping isn't working correctly, is there something else I could try with the errhandle? That onDirty command you mentioned...how would I do that?
Hey thanks Minion,
Your tip worked beautifully. I ended up modifying my code to look like this... - Private Sub cmdCancel_Click()
-
On Error GoTo errHandle
-
-
Dim intProcced As Integer
-
-
'Warn the user that any information entered will be lost, then if the user responds that it is
-
'ok, erase the record and close the form
-
-
intProcced = MsgBox("Warning: Any changes made or any information entered will not be saved.", vbOKCancel, "Warning")
-
If intProcced = 2 Then
-
Exit Sub
-
End If
-
-
If Me.Dirty = True Then
-
DoCmd.SetWarnings False
-
DoCmd.OpenQuery "qryMoveFeedersFalse"
-
DoCmd.SetWarnings True
-
DoCmd.Close
-
Exit Sub
-
Else
-
DoCmd.SetWarnings False
-
DoCmd.OpenQuery "qryMoveFeedersFalse"
-
DoCmd.SetWarnings True
-
DoCmd.Close
-
Exit Sub
-
End If
-
-
errHandle:
-
If Err.Number = 2046 Then
-
DoCmd.Close
-
Exit Sub
-
Else
-
'Print the error message and send an email to Administrator
-
SendErrorMsg "cmdCancel_Click on frmEnterNewFeeder", Err.Number, Err.Description
-
MsgBox Err.Description
-
End If
-
-
Exit Sub
-
-
End Sub
Minion 108
Recognized Expert New Member
I'm glad to hear that it got you moving in the right direction. Sometimes all we need to find the answer is a nudge.
- Minion -
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Corrine |
last post by:
Is there a way to cancel a change in an option group? The option group still
changes to the option clicked on when clicking on the NO button with the
following code in the BeforeUpdate event of the option group.
If MsgBox("Change?", vbYesNo) = vbNo Then
Cancel = True
End If
Thanks!
|
by: babylon |
last post by:
any facilities in csharp that can help me implmenting undo/redo in my
application?
thx
|
by: martin |
last post by:
Hello,
Is there a way to make a kind of "cancel" button on a form?
Suppose you accidently changed or overwrote some data in a form, then I'd
like to leave this form at once and cancel any change made in this form.
Hopefully someone has a clue for me.
thanks a lot!
|
by: Matuag |
last post by:
Hi All,
I want to create following command buttons on a Form which users can
edit.
Save ( Save Changes made)
Cancel ( Undo data changes)
Exit ( Close form)
I am using Macros for each of these commands and they work perfect.
|
by: inadsad |
last post by:
Greetings Group:
I got a form with multiple controls that are bound to a dataset, with
two buttons & next/previous. Simply, add button adds a new record and
cancel button undo changes.
On add click event button:
Me.BindingContext(ds1.Tables("MyTable")).EndCurrentEdit()
Me.BindingContext(ds1.Tables("MyTable ")).AddNew()
On Cancel
| |
by: call_me_anything |
last post by:
Hi,
I am looking for a good algorithm (in C/C++) for implementing undo/
redo on a data structure.
The data structure is basically a n-children tree somewhat of the
form :
class /* or struct */ node {
char *info1;
|
by: anupam roy |
last post by:
Hi All,
I want to perform basic Edit menu functionalities on my custom design surface.
While all the Cut/Copy/Paste/Deelete/Select functionalities working fine with code below,Undo/Redo standard commands are not working for me.
IMenuCommandService menusrv= HostControl.HostSurface.GetService(typeof(IMenuCommandService)) as IMenuCommandService;
if (menusrv != null)
{
switch (editstring)
...
|
by: Paul |
last post by:
How can you cancel a row insert programmatically? I'm not talking
about the Form_BeforeInsert(Cancel As Integer). I have a continuous
form. A new row is started. Code for the continuous form captures a
KeyDown event (Ctrl+D) to delete the row. I can tell if the row is
new. I just don't know how to cancel the new row.
|
by: pelicanstuff |
last post by:
Below is my code. Me.Confirmed is a checkbox. Sometimes the cancel at line 37 works when the user clicks "no", someetimes it doesn't.
Is this my fault or someting else?
Private Sub Confirmed_BeforeUpdate(Cancel As Integer)
If gcfHandleErrors Then On Error GoTo PROC_ERR
PushCallStack "ConfirmDeployment"
Dim strfield As String
strfield = Me.ConsultantID & ""
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |