Save and Close a form with Required Fields with a button

hardhitter06

Registered User.
Local time
Today, 13:59
Joined
Dec 21, 2006
Messages
600
Hi All,

I'm using Access 2003.

I have an input form with 5 fields. All 5 fields are required or the record will not save. Currently, I have a Save button from the Macro toolbar on this form. When it is clicked and if any of the fields are not filled out, a null message displays. The thing I want it to do is Save the record and also exit out of it if all the fields are entered. Obviously, if they are not filled out, i would like the button to give the null message and allow the user to fill in whatever they might have missed and then reclick the save button which will exit out of the form after the record has been saved.

Here is the code for this Macro button now, I don't know if I need more code or if there is an obvious fix.

Private Sub Command8_Click()


On Error GoTo Err_Command8_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Command8_Click:
Exit Sub

Err_Command8_Click:
MsgBox Err.Description
Resume Exit_Command8_Click

End Sub


Please let me know and thank you in advance.

Josh
 
The problem with the "Save" button concept is that the user can simply move to another record or close the form and, in most cases, the record will be saved anyway! You can keep your "Save" button, butcode to prevent saving an incomplete record needs to be in the Form_BeforeUpdate event. Assuming all controls are textboxes:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctrl As Control

For Each ctrl In Me.Controls
 If ctrl.ControlType = acTextBox Then
  If IsNull(ctrl) Then
    MsgBox ctrl.Name & " Cannot Be Left Empty!"
    Cancel = True
    ctrl.SetFocus
    Exit Sub
  End If
 End If
Next

End Sub

If you also had, say, a checkbox control, you'd modify

If ctrl.ControlType = acTextBox Then

to

If ctrl.ControlType = acTextBox Or ctrl.ControlType = acCheckBox Then
 
I'm really not sure what I need to edit in that set of code. Do I need to declare each of my fields, forexample, my first field is FedID?

But also, by having the fields required, it doesn't allow me save without all the fields being completed. The main purpose of the "Save button" was to allow the user to double check their work and if they didn't enter all the fields, to correct the problem.

Which is what this does, but I just want to add the ability for this to happen and then the form to closed with the record being saved if their were no errors.
 
Ok, that code works, but hitting the save button still doesnt exit me out of the form??
 

Users who are viewing this thread

Back
Top Bottom