You could try something like this in the cmdSave Click event:
On Error GoTo cmdSave_Click_Error
If Me.Dirty Then Me.Dirty = False
Me.Contatore = fctAnnoNr(Me.DataChiamata)
ProcExit:
End Sub
cmdSave_Click_Error:
If Err.Number <> 2101 Then
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in cmdSave_Click, Form_Table1"
End If
Resume ProcExit
End Sub
Note the error trapping, which eliminates the useless (in this context) "The
setting you entered isn't valid for this property" error.
Then in the form's Before Update event, perform the validation:
If IsNull(Me.NominativoCliente) Or _
IsNull(Me.DataChiamata) Or _
IsNull(Me.Autorizzato) = True Then
MsgBox "Missing Data"
Cancel = True
End If
Or to be more specific:
If IsNull(Me.NominativoCliente) Then
MsgBox "NominativoCliente needs data"
Me.txtNomCliente.SetFocus
Cancel = True
Elseif IsNull(Me.DataChiamata) Then
MsgBox "DataChiamata needs data"
Me.txtDataChiamata.SetFocus
Cancel = True
ElseIf IsNull(Me.Autorizzato) Then
MsgBox "Autorizzato needs data"
Me.txtAutorizzato.SetFocus
Cancel = True
End If
I have suggested you eliminate the built-in Close button on the top right.
You have not responded to that suggestion. Is there a reason you don't want
to eliminate the button? It will be very difficult, at best, to eliminate
the built-in error message. It will be more difficult yet, if it is possible
at all, to choose to save the record at that point.
I don't understand this part:
If Me.NewRecord Or Right(Year(Me.DataChiamata), 4) <> Left(Me.Contatore, 2)
You seem to be performing more validation. If so, you don't need to test for
a new record. Access will try to save the record at some point anyhow. If
you need to check DataChiamata against Contatore before assigning the number
you could do:
If Right(Year(Me.DataChiamata), 4) <> Left(Me.Contatore, 2) Then
Me.Contatore = fctAnnoNr(Me.DataChiamata)
End If
This next part is confusing also:
DoCmd.Close
Forms!Main.Requery
DoCmd.GoToRecord , , acLast
Are you closing the form? If so, why have more code after closing the form?
Also, you could try:
Me.Refresh
instead of requery if necessary to see the new Contatore value. This should
eliminate the need to move to the last record (which you can't do anyhow
because the form is closed).
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:
[quoted text clipped - 12 lines]
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.