I have tried some but it will not work as I will

If I press the X button on titel bar then
I will it goes to cmdquit where I have to
choose vbyes or vbno.
The tests I haave done just quit the program
at once without any chance to choose.

If vbyes is true then call cmdquit where
it make some backup files.
If vbno then exit sub

Recommended Answers

All 9 Replies

I think what you is to handle the Unload event. Here the MSDN example of that:

This example demonstrates a simple procedure to close a form while prompting the user with various message boxes. In an actual application, you can add calls to general purpose Sub procedures that emulate the processing of the Exit, Save, and Save As commands on the File menu in Visual Basic. To try this example, paste the code into the Declarations section of a form, and then press F5. Once the form is displayed, press ALT+F4 to close the form.

Private Sub Form_Unload (Cancel As Integer)
   Dim Msg, Response   ' Declare variables.
   Msg = "Save Data before closing?"
   Response = MsgBox(Msg, vbQuestion + vbYesNoCancel, "Save Dialog")
   Select Case Response
      Case vbCancel   ' Don't allow close.
         Cancel = -1
         Msg = "Command has been canceled."
      Case vbYes
      ' Enter code to save data here.
         Msg = "Data saved."'
      Case vbNo
         Msg = "Data not saved."
   End Select
   MsgBox Msg, vbOKOnly, "Confirm"   ' Display message.
End Sub
commented: good post +7

I have tried some but it will not work as I will

If I press the X button on titel bar then
I will it goes to cmdquit where I have to
choose vbyes or vbno.
The tests I haave done just quit the program
at once without any chance to choose.

If vbyes is true then call cmdquit where
it make some backup files.
If vbno then exit sub

As i said in your previous thread (actualy same question)
It much easier to put your backup codes in procedure or function. you can call it in your cmdquit button event or when you press X button or any action when you needed to backup your files.

Hope it helps.

i assume that cmdquit is something event procedure

If I press the X button on titel bar then
I will it goes to cmdquit

when you click on the close button , it will execute the code written inside Unload().

The tests I haave done just quit the program
at once without any chance to choose.

this is because you are not setting Cancel as 1.

so if you really wanna execute the code of cmdquit on the unload() then do following:-

Private Sub Form_Unload(Cancel As Integer)
Cancel = 1
cmdquit
End Sub

now i think you can handle the issue

hope this helps you . . .

No luck

No luck?

How about post your cmdquit codes.
It will help..

Did you follow the working example I gave you?

as i already wrote in my previous post that you have to work on unload() event procedure.

Private Sub Form_Unload(Cancel As Integer)
choice = MsgBox("do you really wanna exit ?", vbYesNo)
If choice = vbYes Then
cmdquit
Cancel = 0
Else
Cancel = 1
End If
End Sub

the above example will ask you to exit the application , if you choose yes then first it will go to cmdquit procedure where you write some code for backup and then the application will close (because cancel=0)

And if you choose no then form will not unload(because cancel=1).

if still having problems , then there are certainly some errorful code in the cmdquit procedure

Hi foks

I have tried them and after a long testing I came over how to fix it

Private sub form_unload(cancel as integer)

cancel = 1

call cmdquit.

and in the last line in cmdquit
END

thats all and it works fine for me

if problem solved , then mark this post as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.