How to access closing "Red X" event?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
919
Office Version
  1. 365
Platform
  1. Windows
I have a form that has a quit button, but it also has - of course - a typical closing "Red X" button. How do I access that event from VBE? It should drive the "Private Sub btnQuit_Click()" procedure.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
That X is this event:
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
You can add code to that procedure as desired.
 
Upvote 0
Ok, I got to the event. I can now make it like this:

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    btnQuit_Click
    Cancel = 1
End Sub

Code:
Private Sub btnQuit_Click()
    Dim lAnswer As Long
    If pblstrLng = "Eng" Then
        lAnswer = MsgBox(Prompt:="Really exit?", Buttons:=vbYesNo + vbQuestion, Title:="Exit?")
        If lAnswer = vbYes Then
            Unload Me
        Else
            Exit Sub
        End If
    Else
        frmPoistutaanko.Show
    End If
End Sub

It doesn't work. What's wrong?

The idea is to have two closing methods, clicking Quit and clicking that "Red X". It shouldn't matter which one I click. What should matter is the radiobuttons between Finnish and English. The function for clicking that radio button is:

Code:
Function VaihdaKieli(strLng As String) As String
    If strLng = "Fin" Then
        lblINI.Caption = "INI tiedosto:"
        lblSource.Caption = "Lähdetiedosto:"
        lblTemp.Caption = "Väliaikaistiedosto:"
        lblResults.Caption = "Tulostiedosto:"
        btnGet.Caption = "Hae tagit"
        btnNotePad.Caption = "Avaa muistio"
        btnQuit.Caption = "Poistu"
        pblstrLng = "Fin"
    Else
        lblINI.Caption = "Location of INI File:"
        lblSource.Caption = "Location of Source File:"
        lblTemp.Caption = "Location of Temp File:"
        btnGet.Caption = "Get tags"
        btnQuit.Caption = "Quit"
        btnNotePad.Caption = "Open Notepad"
        lblResults.Caption = "Location of Results File:"
        pblstrLng = "Eng"
    End If
    VaihdaKieli = pblstrLng
End Function

So there's 2x2x2 options. Clicking Cancel on msgbox (Or "ei", btnEi on frmPoistutaanko) shouldn't matter, it still should just act like "the Red X" or btnQuit was never pressed.

Right now
1.1.) Pressing btnQuit with radiobutton optEnglish active works right, it displays the msgbox (works correctly).
1.1.1.) Pressing there "No" works correctly.
1.1.2.) Pressing there "Yes" doesn't work right, it displays that "Really Exit"-msgbox once and on the second time it acts like pressing "No".

1.2.) Pressing btnQuit with radiobutton optFinnish active it displays that frmPoistutaanko (works correctly)
1.2.1.) Pressing btnJoo (yes) there works correctly, it exits the program.
1.2.2.) Pressing btnEi (no) there works correctly, it closes the form.

1.1.) Pressing "The Red X" with radiobutton optEnglish active works right, it displays the msgbox (works correctly).
1.1.1.) Pressing there "No" works correctly.
1.1.2.) Pressing there "Yes" doesn't work right, it acts like pressing "No".

2.2.) Pressing "The Red X" with radiobutton optFinnish active it displays that frmPoistutaanko (works correctly)
2.2.1.) Pressing btnJoo (yes) there works correctly, it exits the program.
2.2.2.) Pressing btnEi (no) there works correctly, it closes the form.

So how do I correct that English "Yes"?
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

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