Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

close program problem 2

Status
Not open for further replies.

keeyean

Civil/Environmental
Sep 11, 2001
14
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??
 
Replies continue below

Recommended for you

i have this in the form too..

Private Sub MDIForm1_Unload(Cancel As Integer)
mnuExit_Click
End Sub

this link the "x-button" to the mnuExit_Click...
 
Hi,

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,
 
i change it to this...but it doesn't work too....

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...:(
 
Hi Again,

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
 
may i know.. what is the "stars" for?? the more stats mean what???
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

 
I know it may seem like a cheats way out but if this is causing you too much trouble, try just disabling the
"x-Button". This way the user has to exit through your menu.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor