View Single Post
  #5  
Old May 19th, 2010, 09:48 AM posted to microsoft.public.access.forms
McHammer
external usenet poster
 
Posts: 4
Default How to prevent form from closing



"BruceM via AccessMonster.com" wrote:

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.