Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Prevent form from closing using x-button

Status
Not open for further replies.

pdtit

Technical User
Nov 4, 2001
206
BE
I have a button on my form "STOP", with the following code behind it :

Private Sub Btn_Finish_Click()
On Error GoTo Err_Btn_Finish_Click

Dim Msg, Style, Title, Response, MyString
Msg = "Do you want to terminate the program ?" '
Style = vbYesNo + vbInformation + vbDefaultButton2 '
Title = "Terminate Program"

Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
DoCmd.Quit
Else
End If

Exit_Btn_Finish_Click:
Exit Sub

Err_Btn_Finish_Click:
MsgBox Err.Description
Resume Exit_Btn_Finish_Click

End Sub


When the user clicks the STOP-button, it gives the msgbox; when selecting yes, the program stops; when selecting no, the form stays open.

I copied the whole code for the Form_Close-event. When clicking the "close-button [X] = the normal windows-button)
the msgbox comes up with yes and no. However, even when clicking no, the form still closes.

How can I prevent this ?

Regards,

PdtIt
 
pdtit, I have not tested this but in theory it should work

Hope it helps

zero



Private Sub Btn_Finish_Click()
On Error GoTo Err_Btn_Finish_Click

Dim Msg, Style, Title, Response, MyString
Msg = "Do you want to terminate the program ?" '
Style = vbYesNo + vbInformation + vbDefaultButton2 '
Title = "Terminate Program"
Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
DoCmd.Quit
Else
me.Btn_Finish.setfocus
End If

Exit_Btn_Finish_Click:
Exit Sub

Err_Btn_Finish_Click:
MsgBox Err.Description
Resume Exit_Btn_Finish_Click

End Sub
 
Dear,

I'm sorry to say but this doesn't work.

there's something strange happening when clicking the close button. Instead of doing some "setfocus", I tried sending a msgbox. It gives the messagebox again, but the form still closes.
Another ?!? : with the code setup for the close-event, every time when I switch from normal view to design view, it executes the code ????????

Regards,

pdtit
 
Hi pdtit!

Try this:

Private Sub Btn_Finish_Click()
On Error GoTo Err_Btn_Finish_Click

Dim Msg, Style, Title, Response, MyString
Msg = "Do you want to terminate the program ?" '
Style = vbYesNo + vbInformation + vbDefaultButton2 '
Title = "Terminate Program"
Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
DoCmd.Quit
Else
Cancel = -1
End If

Exit_Btn_Finish_Click:
Exit Sub

Err_Btn_Finish_Click:
MsgBox Err.Description
Resume Exit_Btn_Finish_Click

End Sub

hth Jeff Bridgham
bridgham@purdue.edu
 
Sorry, doesn't work.

Maybe something interesting :
I have a statement in my OnActivate of this form that is DoCmd.maximize

Someone told me that a maximized form ALWAYS shows the closebutton, and there is no option to prevent closing the form from this button.
After this statement, I tried Me.Closebutton = False, and indeed, Access gives an error that this control can't be changed.

Maybe it's just not possible ...

What happens :
a) I hide the database window
b) when you close the form from the X, it closes the form anyway, and gives me a blank access-window.
I'm thinking of solving it this way for the moment :
when the user clicks the X, giving a messagebox informing the user he should close the app using my "Close-Btn" on the form to prevent losing data and thus exiting access anyway.

PdtIt
 
Hi Again!

My mistake, the Cancel thing will work in the Form_Unload event procedure, not in the Form_Close event procedure.

hth Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top