How to access closing "Red X" event?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
868
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.
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
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.
 

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
868
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"?
 

Watch MrExcel Video

Forum statistics

Threads
1,123,535
Messages
5,602,217
Members
414,513
Latest member
junbuggle

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
Top