Prevent printing if required field is blank

S

stacie.2410

I have a form that has multiple required fields (4 total). The form has
command buttons for saving and printing. What I need is some code to
prevent saving or printing if a required field is blank.

What I'd like the process for saving/printing to be is as follows:

1.) User clicks Save or Print button

2.) a.) If a required field is left blank, a custom error message displays
informing them that one of the required fields was left blank (if I could
even get it to tell them exactly which field, that would be best), and that
their form cannot be saved or printed until all required fields contain data.
User then clicks "OK" and it returns to the form so they can input their
data.

2.) b.) If all required fields contain data, a custom message displays
saying "Record save successful!"

OR FOR PRINTING

2.) b.) If all required fields contain data, the printer dialog box
displays, and when printed, only the current record displayed in the form is
printed, not the entire record set.

Does anyone have any suggestions?
 
F

fredg

I have a form that has multiple required fields (4 total). The form has
command buttons for saving and printing. What I need is some code to
prevent saving or printing if a required field is blank.

What I'd like the process for saving/printing to be is as follows:

1.) User clicks Save or Print button

2.) a.) If a required field is left blank, a custom error message displays
informing them that one of the required fields was left blank (if I could
even get it to tell them exactly which field, that would be best), and that
their form cannot be saved or printed until all required fields contain data.
User then clicks "OK" and it returns to the form so they can input their
data.

You can use the Form's BeforeUpdate event to tell the user that a
particular control (field) is null.

If IsNull([ControlA]) then
MsgBox "ControlA is not filled in."
Cancel = True
Me.[ControlA].SetFocus
ElseIf IsNull([ControlB]) then
MsgBox "ControlB is not filled in."
Cancel = True
Me.[ControlB].SetFocus
ElseIf IsNull(... etc. for Control's C and D
Else
MsgBox "This record has been saved"
End If

Change [ControlA], [ControlB], etc. to whatever the actual control
names are.
2.) b.) If all required fields contain data, a custom message displays
saying "Record save successful!"

OR FOR PRINTING

2.) b.) If all required fields contain data, the printer dialog box
displays, and when printed, only the current record displayed in the form is
printed, not the entire record set.

You are printing a Report aren't you, not the Form itself?
Reports are for printing, not forms.
What do you need the printer dialog for?

Code the Print Command Button's Click event:

If Not IsNull([ControlA]) and Not IsNull([ControlB]) And Not
IsNull([ControlC]) and Not IsNull([ControlD]) Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acPreview, , "[RecordID] = " &
Me.[RecordID]
End If


The above assumes there is a Prime Key field named [RecordID] that
uniquely identifies each record.
Change [RecordID] to whatever the actual name of that field is.
Also assumed is that [RecordID] is a Number datatype.
If in fact, [RecordID] is a Text datatype, then use:

"[RecordID] = '" & Me.[RecordID] & "'"

After in testing, this correctly displays in Preview the record you
wish to print, change acPreview to acNormal to print directly without
previewing the report.
 
S

stacie.2410

That worked! Thanks for your help!

fredg said:
I have a form that has multiple required fields (4 total). The form has
command buttons for saving and printing. What I need is some code to
prevent saving or printing if a required field is blank.

What I'd like the process for saving/printing to be is as follows:

1.) User clicks Save or Print button

2.) a.) If a required field is left blank, a custom error message displays
informing them that one of the required fields was left blank (if I could
even get it to tell them exactly which field, that would be best), and that
their form cannot be saved or printed until all required fields contain data.
User then clicks "OK" and it returns to the form so they can input their
data.

You can use the Form's BeforeUpdate event to tell the user that a
particular control (field) is null.

If IsNull([ControlA]) then
MsgBox "ControlA is not filled in."
Cancel = True
Me.[ControlA].SetFocus
ElseIf IsNull([ControlB]) then
MsgBox "ControlB is not filled in."
Cancel = True
Me.[ControlB].SetFocus
ElseIf IsNull(... etc. for Control's C and D
Else
MsgBox "This record has been saved"
End If

Change [ControlA], [ControlB], etc. to whatever the actual control
names are.
2.) b.) If all required fields contain data, a custom message displays
saying "Record save successful!"

OR FOR PRINTING

2.) b.) If all required fields contain data, the printer dialog box
displays, and when printed, only the current record displayed in the form is
printed, not the entire record set.

You are printing a Report aren't you, not the Form itself?
Reports are for printing, not forms.
What do you need the printer dialog for?

Code the Print Command Button's Click event:

If Not IsNull([ControlA]) and Not IsNull([ControlB]) And Not
IsNull([ControlC]) and Not IsNull([ControlD]) Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acPreview, , "[RecordID] = " &
Me.[RecordID]
End If


The above assumes there is a Prime Key field named [RecordID] that
uniquely identifies each record.
Change [RecordID] to whatever the actual name of that field is.
Also assumed is that [RecordID] is a Number datatype.
If in fact, [RecordID] is a Text datatype, then use:

"[RecordID] = '" & Me.[RecordID] & "'"

After in testing, this correctly displays in Preview the record you
wish to print, change acPreview to acNormal to print directly without
previewing the report.
Does anyone have any suggestions?
 

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