Required Fields Multiple

B

BMaerhofer

'Save Form with required fields

Private Sub SAVEForm_Click()
On Error GoTo Err_CloseForm_Click
If [Oil_Filter Change] Or [Air Filter Change] Or [Other Maintenance] =
True And [Mileage] Is Not Null And [Date] Is Not Null Then
DoCmd.Close
Else
MsgBox "Required Fields must not be blank before saving!"
End If

Exit_CloseForm_Click:
Exit Sub

Err_CloseForm_Click:
MsgBox Err.Description
Resume Exit_CloseForm_Click

Here is the problem: I need for the message to prompt the user if
[Mileage], [Date] and one of the following (Oil, Air, Other) is not filled in
before saving the record.

Example of choices:
1 [Oil_Filter Change], [Mileage], and [Date] required
2 [Air_Filter Change], [Mileage], and [Date] required
3 [Other Maintenance], [Mileage], and [Date] required
4 [Oil_Filter Change], [Air_Filter Change], [Mileage], and [Date] required

Summary: 3 minimum required fields (Mileage, Date and Oil or Air or Other)

It seems easy but for some reason I am struggling with this. Either it
closes and doesn't give you the opportunity to correct the required fields or
it give you an object dialog box.


Thanks,
 
A

Allen Browne

Simple solution: Open the table in design view, and set the Required
property to Yes.

If you want to use VBA, use the BeforeUpdate event of the *form*. Access
runs this event before saving.

Your save button then needs just to foce the save and close:
If Me.Dirty Then Me.Dirty = False
DoCmd.Close acForm, Me.Name
with error handling in case the save fails.
 
B

BMaerhofer

Well I have changed it to this but still having problems...

If IsNull(Vehicle) Then
MsgBox "Must Select Vehicle for Entry before saving", vbOKOnly,
"Invalid Input"
End If

If IsNull(Date) Then
MsgBox "Must Enter Date for Entry before saving", vbOKOnly, "Invalid
Input"
End If

If IsNull(Mileage) Then
MsgBox "Must Enter Mileage for Entry before saving", vbOKOnly,
"Invalid Input"
End If

If IsNull(Oil_Filter_Change) Or (Air_Filter_Change) Or
(Other_Maintenance) Then
MsgBox "Please Checkmark at least one of the following: Oil/Filter
Change, Air Filter Change, or Other Maintenance", vbOKOnly, "Invalid Input"
End If
DoCmd.Close

The Oil/Air/Maintenance is not working properly. I need just one or
multiple to be checked to validate it. Now it just closes the window.

Thanks,
Brian



--
BWM


Allen Browne said:
Simple solution: Open the table in design view, and set the Required
property to Yes.

If you want to use VBA, use the BeforeUpdate event of the *form*. Access
runs this event before saving.

Your save button then needs just to foce the save and close:
If Me.Dirty Then Me.Dirty = False
DoCmd.Close acForm, Me.Name
with error handling in case the save fails.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

BMaerhofer said:
'Save Form with required fields

Private Sub SAVEForm_Click()
On Error GoTo Err_CloseForm_Click
If [Oil_Filter Change] Or [Air Filter Change] Or [Other Maintenance] =
True And [Mileage] Is Not Null And [Date] Is Not Null Then
DoCmd.Close
Else
MsgBox "Required Fields must not be blank before saving!"
End If

Exit_CloseForm_Click:
Exit Sub

Err_CloseForm_Click:
MsgBox Err.Description
Resume Exit_CloseForm_Click

Here is the problem: I need for the message to prompt the user if
[Mileage], [Date] and one of the following (Oil, Air, Other) is not filled
in
before saving the record.

Example of choices:
1 [Oil_Filter Change], [Mileage], and [Date] required
2 [Air_Filter Change], [Mileage], and [Date] required
3 [Other Maintenance], [Mileage], and [Date] required
4 [Oil_Filter Change], [Air_Filter Change], [Mileage], and [Date]
required

Summary: 3 minimum required fields (Mileage, Date and Oil or Air or Other)

It seems easy but for some reason I am struggling with this. Either it
closes and doesn't give you the opportunity to correct the required fields
or
it give you an object dialog box.


Thanks,
 
A

Allen Browne

Your code just shows the MsgBox. It doesn't cancel the event.

Whereever needed add:
Cancel = True

This assumes you are using Form_BeforeUpdate.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

BMaerhofer said:
Well I have changed it to this but still having problems...

If IsNull(Vehicle) Then
MsgBox "Must Select Vehicle for Entry before saving", vbOKOnly,
"Invalid Input"
End If
....
 

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