Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding cancel button and formatting textbox controls

Status
Not open for further replies.

NIA2

Technical User
Aug 30, 2006
137
AU
Hi everyone,

I have the following code on a cancel button in a userform that causes a message box to appear warning the user that they'll lose all changes if they press cancel:

Code:
Private Sub cmdCancel_Click()
        Dim result As Integer
        result = MsgBox("Are you sure you want to Cancel? You will lose all changes.", vbYesNo + vbQuestion)
        If result = vbYes Then Unload Me
        frmEnergeticsProposal.Show
        Set frmEnergeticsProposal = Nothing
End Sub

The only problem is that if the user clicks the close button in the top corner of the form dialogue itself, instead of cancel, there will be no message to warn them that they'll lose changes. Is it possible to code the form close button itself similar to what I have on the cancel button?

Also I'm noticing two problems with the textboxes on the userform. The first is that I can't copy and paste text into the boxes. Is there a way to allow this functionality?

Lastly, on single line textboxes, when text is entered I notice that it's not centered vertically within the textbox - it's aligned to the top. Is there a way to have it center vertically?

Thanks again for any help offered.
 
Use UserForm_QueryClose event to trap the close reason (VbQueryClose enumerated type, Cancel=True to keep form instantiated).

combo
 

Also, check your code.

After you Unload Me your Form has to go thru Form_Load event to execute last 2 lines of your code:

frmEnergeticsProposal.Show
Set frmEnergeticsProposal = Nothing

You should not have any lines of code to execute afer Unload Me statement.

Have fun.

---- Andy
 
Thanks for the reply,

Sorry not too familiar with the code. Can you provide the complete code snippet and advise where to put it in my code?

Just a beginner here.
 
Andy, the Unload Me instruction is only executed when the user confirms the cancel operation.
 

PHV, I know :)

What I would do is change the order of lines of code like this:
Code:
Private Sub cmdCancel_Click()

If MsgBox("Are you sure you want to Cancel? You will lose all changes.", vbYesNo + vbQuestion) [blue]= vbYes[/blue] Then
    Set frmEnergeticsProposal = Nothing
    Unload Me
End If

End Sub
So there are no lines to execute after Unload Me.

I just do know why frmEnergeticsProposal is shown and then Set to Nothing in the code....?


Have fun.

---- Andy
 

gwh2,

Combo's solution to disabling the "X" close in the top right corner is correct. Since you're new, the following is the full code to accomplish this:
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

This will disable the "X" in all forms. If you want to selectively disable it remove the "Private" and use the code wherever it is needed.

[glasses]

----------------------------------------------------------------------------------
[small][ponder]"Did you hear about the guy who refused to pay his exorcist?[/small]
He was repossessed." [lol]
 
This will disable the "X" in all forms. If you want to selectively disable it remove the "Private" and use the code wherever it is needed.
Actually, the code leave the [x] button active and works only for one userform (the one that contains the code).
The Sub UserForm_QueryClose is userform's event procedure, it is called when one tries to close the form. With this code it is possible to react depending on the way you try to close the form (esp. code, UI). Cancel=True just cancels the event and leaves the form opened.


combo
 
Thanks for explaining and providing the full code. I was thinking that since the code totally disables the close button on the current userform, would this be downside, ie. would it actually be better to provide the user with this extra method of closing the form (along with the cancel button) but adding the message box as I have on the cancel button? I mean what's usually done in cases like this - just wondered what you guys think about it?

Also can someone possibly answer my other two questions in my original post as follows:

I'm noticing two problems with the textboxes on the userform. The first is that I can't copy and paste text into the boxes. Is there a way to allow this functionality?

Lastly, on single line textboxes, when text is entered I notice that it's not centered vertically within the textbox - it's aligned to the top. Is there a way to have it center vertically?

 
Please read the above carefully. The procedure above traps the 'Close' event and returns the reason (vbFormControlMenu - user interface, i.e. the [x] button). The default 'Cancel' is False, i.e. the form will be closed.

combo
 
I kind of understand your explanation of the procedure, but all I know is that when I insert your provided code:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

I test and then when I try to press the close button, the form won't close. The code is disabling this option for the user.
 
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
  If MsgBox("Are you sure you want to Cancel? You will lose all changes.", vbYesNo + vbQuestion) = vbYes Then
    Cancel = True
  End If
End If
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks - the code you've provided brings up a message box when I press the close button but when I press 'yes', the form doesn't actually close.
 
Code:
If MsgBox("Are you sure you want to Cancel? You will lose all changes.", vbYesNo + vbQuestion) = vb[!]No[/!] Then
    Cancel = True
End If


combo
 
Thanks again - that works great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top