close program problem
close program problem
(OP)
i have a question regarding visual basic close program porblem....
i have this code in the mnu...
a msg will be asked if any changed in file... but how do i link this to the left hand window corner "X- button"...so that, a msg will be asked too when i close the program from the "x-button"??
Public Sub mnuExit_Click()
If FileHasChanged = True Then
Style = vbYesNoCancel + vbQuestion
Response = MsgBox("Do you want to save this file?", Style)
If Response = vbYes Then
Call mnuSaveAs_Click
ElseIf Response = vbNo Then
End
UnloadAllForm
ElseIf Response = vbCancel Then
Exit Sub
End If
Else
End
UnloadAllForm
End If
End Sub
this is what will happen when i close the program from the "x-button"
1.)when i click the "x-button".. the Form that display on the MDIForm will invisible... and a msg "do you want to save the file?" will pop up...
2.)when i select "Yes"....a save dialog will pop up.. but once i select "Cancel" from the save dialog... the MDIForm will invisible...
3.)when i select cancel(from the msg).. the MDIForm will invisible also..
if i close the program from the file menu...it work fine..
hope you can follow..:)
besides, i have another question at here....
why all the comboboxes i have in the program will be highlighted? and how do i prevent this to happen??
i have this code in the mnu...
a msg will be asked if any changed in file... but how do i link this to the left hand window corner "X- button"...so that, a msg will be asked too when i close the program from the "x-button"??
Public Sub mnuExit_Click()
If FileHasChanged = True Then
Style = vbYesNoCancel + vbQuestion
Response = MsgBox("Do you want to save this file?", Style)
If Response = vbYes Then
Call mnuSaveAs_Click
ElseIf Response = vbNo Then
End
UnloadAllForm
ElseIf Response = vbCancel Then
Exit Sub
End If
Else
End
UnloadAllForm
End If
End Sub
this is what will happen when i close the program from the "x-button"
1.)when i click the "x-button".. the Form that display on the MDIForm will invisible... and a msg "do you want to save the file?" will pop up...
2.)when i select "Yes"....a save dialog will pop up.. but once i select "Cancel" from the save dialog... the MDIForm will invisible...
3.)when i select cancel(from the msg).. the MDIForm will invisible also..
if i close the program from the file menu...it work fine..
hope you can follow..:)
besides, i have another question at here....
why all the comboboxes i have in the program will be highlighted? and how do i prevent this to happen??
RE: close program problem
Private Sub MDIForm1_Unload(Cancel As Integer)
mnuExit_Click
End Sub
this link the "x-button" to the mnuExit_Click...
RE: close program problem
I don't know if the visual basic you have is the same as the one in word macros but this should work.
Private Sub UserForm_Terminate()
...Your Code or a Call to your sub (mnuExit_Click)
End Sub
Regards,
RE: close program problem
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
mnuExit_Click
End Sub
coz when i press the "cancel" from either msgbox.. or the save dialog.... the program will invisible...
plz help...:(
RE: close program problem
My you get a lot of stars for asking questions !
Anyway, I've just tried this code (its not mine but microsofts) and it woks ok. Hope it works for you.
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
Try it & See
Regards
RE: close program problem
actually..i put the code in the wrong place...
i should do like this
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If FileHasChanged = True Then
Style = vbYesNoCancel + vbQuestion
Response = MsgBox("Do you want to save this file?", Style)
If Response = vbYes Then
Cancel = True
Call mnuSaveAs_Click
ElseIf Response = vbNo Then
End
UnloadAllForm
ElseIf Response = vbCancel Then
Cancel = True
Exit Sub
End If
Else
End
UnloadAllForm
End If
End Sub
Public Sub mnuExit_Click()
Unload Me
End If
RE: close program problem
"x-Button". This way the user has to exit through your menu.