Link to home
Start Free TrialLog in
Avatar of jdn1640
jdn1640

asked on

Save As and Control Box

When I close an application using the X in the control box, how do I bring up the Save As Common Dialog box?  I can do it with an Exit button, but not with the X.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Look at the QueryUnload event
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Dim lRes As VbMsgBoxResult
        'The [X] button was pressed
        lRes = MsgBox("Really Quit?", vbQuestion Or vbYesNo, "Title")
        If lRes = vbNo Then Cancel = True
    End If
End Sub
Avatar of fibdev
fibdev

Erick37 told you strait, however if you wish to bring up a save box first ... Place a commonDialoge Control on your form and ...


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Dim lRes As VbMsgBoxResult
        'The [X] button was pressed
        lRes = MsgBox("Really Quit?", vbQuestion Or vbYesNo, "Title")
        If lRes = vbNo Then Cancel = True
        If lRes = vbYes then
          CommonDialoge1.Filter = "All Files (*.*) | *.*"
          CommonDialoge1.ShowSave
        End
        End If
    End If
End Sub

Please forgive any syntax errors, it's late here :)

Well, if the cancel is used then it should be:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Dim lRes As VbMsgBoxResult
        'The [X] button was pressed
        lRes = MsgBox("Save Files?", vbQuestion Or vbYesNoCancel, "Title")
        Select Case lRes
        Case vbYes
            'Save your file(s)
            MsgBox "Bring up ""Save As"" box here"
        Case vbCancel
            'Cancel the unload
            Cancel = True
        End Select
    End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of prem_kumar_25
prem_kumar_25

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You want to *share* the same routine for both? Simpler yet. Assume your exit button is called BTN_EXIT then simply code:

Private Sub Form_Unload(Cancel As Integer)
If UnloadMode <> vbFormCode then Call Btn_Exit_Click
End Sub

This way you only need *ONE* copy of your service routine ...

M
rather than call the button from the unload event, simply unload the form in the exit button and the Form_QueryUnload event will be called anyway

Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub Form_Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
{
    'Do exit stuff here
}

Gordon