Coding a Cancel button

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
319
Office Version
  1. 365
Platform
  1. Windows
Hi all could someone advise me on a soultion to canceling a userform with data already in it please - bearing in mind my VB knowledge is very basic.

So I have a button on a form 'frm1'. The form has 23 textboxes all of which a user is required to put some data in. Once all the textboxes have been completed the user clicks the 'Save' button and the data is written to the worksheet. (all of this is working fine)

But if a user wants to cancel the form without saving anything, they should simply click the Cancel button.

What I have so far is the Cancel button coded to run a msgbox "are you sure you want to cancel etc etc" and the VB Yes No option set.

The problem I'm having is as all the textboxes on the form require something entered in each of them as soon as I press the cancel button, the code behind the first textbox runs i.e. a msgbox "you must enter a value".

So my question is how do I stop this happening? Do i need to 'disable' the textboxes first or is there a simple way to do this?

Many thanks for any advice offered

Paul
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
all the textboxes on the form require something entered in each of them
Userform controls don't have a property like that so it must be something you coded. What event (or events) are being used?
 
Upvote 0
Hi Rory thanks for the quick reply the example pasted below is one of the textboxes but all of the others are bascially the same.

VBA Code:
Private Sub txtInternet_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    If Trim(txtInternet.Value) = "" And Me.Visible Then
    MsgBox "This box requires a numeric value." & vbNewLine & _
    "If this box is not relevent, please enter 0 (zero).", vbExclamation, "No Value Entered"
    Cancel = True
    frmEventExp.txtInternet.Value = ""
    frmEventExp.txtInternet.SetFocus
    ElseIf Not IsNumeric(txtInternet.Text) Or Trim(txtInternet.Value) = " " Then
        MsgBox "It looks like you entered a text value." & vbNewLine & _
        "Please enter only a numeric value or a 0 (zero).", vbExclamation, "Text Value Entered"
     Cancel = True
    frmEventExp.txtInternet.Value = ""
    frmEventExp.txtInternet.SetFocus
   Else
   frmEventExp.txtInternet.Value = Format(txtInternet.Value, "£0.00")

End If
End Sub

Paul
 
Upvote 0
Personally, I would do the validation in the Save routine, but, given that you have it coded this way already, you could simply add a module level Boolean variable that each validation routine tests and then simply exits if it is true. Then your cancel routine can simply set the variable to true and unload the form.
 
Upvote 0
Thanks Rory I should point out that my knowledge of VBA is very very basic and I'm trying my best to learn usually via Youtube. saying that, I sort off understand what you mean by valdating everything in the save routine but I coded each box on its own so that the user can't move to another box before its criteria has been met rather than having the user entering data on each box that hasn't met its critera at the end of the form - mainly for 'workflow' but also because I'm trying to make data entry on the form as simple as possible so that once the data has been transferred over to the worksheet there can't be any errors. (the main user of this form has very little knowledge of spreadsheets).

What I lossed on is what you mean by 'add a module level Boolean variable' could you give me a basic example of what you mean - although I think I understand what this would be doing - I just don't know how to acheive it in code.

Thanks Paul
 
Upvote 0
At the top of the code module for the userform add:

Code:
Dim Cancelled as Boolean

then in your Cancel button code, you need a line that sets that to True:

Code:
Cancelled = True

and in each of your Exit events you'd need to add a line to check that - e.g.:

Rich (BB code):
Private Sub txtInternet_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Cancelled Then Exit Sub
    If Trim(txtInternet.Value) = "" And Me.Visible Then
    MsgBox "This box requires a numeric value." & vbNewLine & _
    "If this box is not relevent, please enter 0 (zero).", vbExclamation, "No Value Entered"
    Cancel = True
    frmEventExp.txtInternet.Value = ""
    frmEventExp.txtInternet.SetFocus
    ElseIf Not IsNumeric(txtInternet.Text) Or Trim(txtInternet.Value) = " " Then
        MsgBox "It looks like you entered a text value." & vbNewLine & _
        "Please enter only a numeric value or a 0 (zero).", vbExclamation, "Text Value Entered"
     Cancel = True
    frmEventExp.txtInternet.Value = ""
    frmEventExp.txtInternet.SetFocus
   Else
   frmEventExp.txtInternet.Value = Format(txtInternet.Value, "£0.00")

End If
End Sub
 
Upvote 0
Solution
Hi Rory, sorry for the delay in replying - many thanks for the example and explanation. I tested this in a new form and it works perfectly.

Paul
 
Upvote 0
Hi Rory I hope you dont mind me posting a follow up to this thread It but I've hit a problem with the Cancel Button. What is happening is the first text box on the form has the following code behind it...

VBA Code:
Private Sub sDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Cancelled Then Exit Sub

    If sDate.Text = "" Then
    Cancel = True
    MsgBox "You must enter the event start date.", vbExclamation, "Start Date Required"
    sDate.SetFocus
    sDate.BackColor = RGB(3, 252, 136)
    
    Else
    sDate.BackColor = RGB(255, 255, 255)
    
    End If
    
End Sub

All the other boxes on the form are also coded so that something has to be entered

The issue I am getting is as soon as I press the cancel button the msgbox code above runs but after I enter something the cancel button works even though all the the other boxes all require something to be entered.

I tried the cancel button code on 2 different userforms and the same thing happened i.e. the cancel button would'nt work until I put something in the frist text box of each form, after I did, it worked perfectly.

Any Ideas on how to resolve this?

Many thanks Paul
 
Upvote 0
What exactly is the code for your Cancel button?
 
Upvote 0
Its as you kindly suggested above in the thread.

At the top of the form
VBA Code:
Dim Cancelled as Boolean

The Button
VBA Code:
Cancelled = True

In all the various Exit events
VBA Code:
[B]If Cancelled Then Exit Sub[/B]
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top