How to prevent form from closing

M

McHammer

Hello everybody,
I use a modal popup form to enter data in a db on which there a 2 buttons
"save and close" and "cancel". I'd like to prevent the from from closing
through the "X" button on the top left corner with unwanted/incomplete data.
Do do that I trap the use of these buttons in before update event so in the
else case I put a warning msgbox (vbOKonly) and cancel = true to prevent any
other event. So far so good. After this event an extra message of Access
advises me that the record can't be saved and if I want to close anyway the
form or not. I want to avoid this message and of course the closure of the
form. Why cancel = true is not enough to do that and what else I could do?

Thks in advance
 
M

McHammer

BruceM via AccessMonster.com said:
It usually helps to post the actual code when there is a question about the
code. Why show them the X button (top right corner, not left, but I assume
you meant that) at all? You should be able to use the form's Property Sheet
to set Control Box to No (or set it to Yes, with MinMax buttons set to Yes
and Close Button to No if you want to keep the minimize and maximize buttons),
or you could set Border Style to None.

It may be possible, from what I can tell, to put something together to get
rid of the error message when the X button is clicked, but it would be a lot
of work for something that doesn't work as well as what you could have with
your own command buttons.

--
Message posted via AccessMonster.com


.
Yes, "X" on the right...
Form is modal popup type, no min/max button, dialog, ctrl box yes.
Here below the code

Public MyAction As Variant

I've added MyAction = "save" to Save button and MyAction = "Cancel" to
Cancel button On click events

Private Sub Form_Current()
MyAction = Null
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MyAction
Case "Cancel"
'do nothing
Case "Save"
'do nothing
Case Else
MsgBox "Push Save or Cancel", vbOKOnly
Cancel = True
Exit Sub
End Select
End Sub

In this way, if a new record is entered it's possible to "save & close" the
form or "cancel" a partial unwanted data and then close the form with "X"
button. If you try to close the form with unwanted data through "X" button,
Cancel = True should stop any other event, or at least it was my target.
 
M

McHammer

BruceM via AccessMonster.com said:
Why fight the built-in Close button? Just set Close Button and/or Control
Box to No, and make your own button to close the form:

Me.Dirty = False
DoCmd.Close acForm, Me.Name

How are you performing the validation? It doesn't seem to be in the form's
Before Update event, which is the usual place for form-level validation. Or
are you validating at the table level (i.e. setting the Required property of
some fields to Yes)?

I assume the Cancel button has something like:
MyAction = Cancel
Me.Form.Undo

How about the Save button?
Here below my button on clickcommands. Some words are in Italian.

Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click
If IsNull(Me.NominativoCliente) Or IsNull(Me.DataChiamata) Or
IsNull(Me.Autorizzato) = True Then
Exit Sub
End If
'do nothing if the minimum importand data are filled in. I could complete
the others later in the main form.

MyAction = "Save"
If Me.NewRecord Or Right(Year(Me.DataChiamata), 4) <> Left(Me.Contatore, 2)
Then
DoCmd.RunCommand acCmdSaveRecord
Me.Contatore = fctAnnoNr(Me.DataChiamata)
End If
'this is to create an autonumber for new records (e.g format 20100001)
through a module
DoCmd.Close
Forms!Main.Requery
DoCmd.GoToRecord , , acLast
Exit_cmdSave_Click:
Exit Sub
Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click
End Sub

Private Sub cmdCancel_Click()
On Error GoTo Err_cmdCancel_Click
MyAction = "Cancel"
If MsgBox("Vuoi annullare il record?", vbYesNo, "Annullare?") = vbYes Then
Me.Undo
Cancel = True
Me.Requery
End If
Exit_cmdCancel_Click:
Exit Sub
Err_cmdCancel_Click:
MsgBox Err.Description
Resume Exit_cmdCancel_Click
End Sub

It would be easier to use an Exit button on the form. I tryed this solution
just to have the same style of all other forms that hasn't got an Exit
button. I'm quite a newbie in Access so I'll accept any advice to make the
job easier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top