Null fields on report

J

jamccarley

I created a report based on a form. I used to have my associates print from
the form and made it so that if certain fields were null, it would not print
plus give an error. Now that I have moved it to a report, it allows it to
print even if there is a blank field and since it cannot update the data if
there is a null field, the report comes out with all fields blanks. Is there
something I can add to the print button code to either confirm all fields
have data, or to update the form and to cancel the print? Which in turn
should give them the error I had before, I think. My form is named "Hold_tag"
,one of the fields is "Date_found" and the report is "Hold tag report"
 
T

Tom Wickerath

.... and since it cannot update the data if
there is a null field, the report comes out with all fields blanks.

I'm not understanding why a null would cause all [fields] controls on the
report to display blanks. Can you expand on this a bit?
Is there
something I can add to the print button code to either confirm all fields
have data, or to update the form and to cancel the print?

Sure. The easiest way would be to simply set the required value to Yes, in
table design view, for each field that you wish to require data entry. In
addition, set the Allow Zero Lenght property to No for those fields that
include this property. The JET database engine will produce a rather ugly
error message, but it's among the quickest ways of achieving your objective.

Alternatively, you might try setting appropriate Validation Rules with
Validation Text to prompt the user. For example, you can use a validation
rule of: Is Not Null

Although it requires some VBA coding, I prefer to use a form's BeforeUpdate
event procedure to run custom validation code. This allows a higher degree of
validation, and one can set focus to the control that fails validation to
assist the user.

Note: You should use VBA code to save the record when a person clicks on a
button on the form to print (or preview) the record in a report. This way,
your report will include any previously uncommitted changes. Here is an
example, which may seem a bit daunting at first, but it's not that bad if you
work your way through it.

Note: I cut a lot of the code out before pasting into this reply, so
hopefully I didn't leave something out that needs to be there.


Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo ProcError

Dim blnValidate As Boolean

blnValidate = Validate

If blnValidate = False Then
Cancel = True
Err.Number = acDataErrContinue
End If

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Form_BeforeUpdate event procedure..."
Resume ExitProc
End Sub


Private Sub cmdPrint_Click()
On Error GoTo ProcError

'Save any changes first
If Me.Dirty = True Then
If Validate = True Then 'Save the record
Me.Dirty = False
DoCmd.OpenReport "rptActionsIssues", acPreview, _
WhereCondition:="Request_rk = " & Me.Request_rk
End If
Else
If Not Me.NewRecord Then
DoCmd.OpenReport "rptActionsIssues", acPreview, _
WhereCondition:="Request_rk = " & Me.Request_rk
Else
MsgBox "You must enter a new record before attempting to print
it.", _
vbInformation, "No Data To Print..."
End If
End If

ExitProc:
Exit Sub
ProcError:
Select Case Err.Number
Case 2501 'Report open cancelled
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in cmdPrint_Click event procedure ..."
End Select
Resume ExitProc
End Sub

Private Sub cmdClose_Click()
On Error GoTo ProcError

'Save any changes first
If Me.Dirty = True Then
If Validate = True Then
'Save the record and close this form.
Me.Dirty = False
DoCmd.Close acForm, Me.Name
Else
'Record was dirtied, but changes do not pass validation rules, so
bail out.
Exit Sub
End If
Else
DoCmd.Close acForm, Me.Name
End If

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in cmdClose_Click event procedure ..."
Resume ExitProc
End Sub

Private Function Validate() As Boolean
On Error GoTo ProcError

Validate = False 'Initialize return value

' Check for required field entries, if the form is dirty

If IsNull(cboProject) Then
MsgBox "Please Select a Project or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboProject.SetFocus
Exit Function
End If

If IsNull(txtRequestTitle) Then
MsgBox "Please Enter a Title or click on the" & vbCrLf & "Undo button
to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequestTitle.SetFocus
Exit Function
End If

If IsNull(txtRequestNo) Then
MsgBox "Please Enter a Request Number or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequestNo.SetFocus
Exit Function
End If

If IsNull(cboStatus) Then
MsgBox "Please Select a Status or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboStatus.SetFocus
Exit Function
End If

If IsNull(cboPriority) Then
MsgBox "Please Select the Priority Level or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboPriority.SetFocus
Exit Function
End If

If IsNull(cboRequestedBy) Then
MsgBox "Please Select a Requestor or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboRequestedBy.SetFocus
Exit Function
End If

If IsNull(txtRaisedDate) Then
MsgBox "Please Enter the Date Raised or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRaisedDate.SetFocus
Exit Function
Else
If Date < txtRaisedDate Then
MsgBox "You must not enter a future date", vbCritical, "Incorrect
Date Entry..."
txtRaisedDate = Null
txtRaisedDate.SetFocus
Exit Function
End If
End If

If IsNull(cboAssignedTo) Then
MsgBox "Please Select the Assigned Person or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboAssignedTo.SetFocus
Exit Function
End If

If IsNull(txtRequiredDate) Then
MsgBox "Please Enter the Date Required or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequiredDate.SetFocus
Exit Function
Else
If txtRequiredDate < txtRaisedDate Then
MsgBox "The Required Date must occur on or after the Raised Date",
vbCritical, "Incorrect Date Entry..."
txtRequiredDate = Null
txtRequiredDate.SetFocus
Exit Function
End If
End If

If IsNull(cboRequestType) Then
MsgBox "Please Select the Request Type or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboRequestType.SetFocus
Exit Function
End If

If Len(txtRequestDescription) = 0 Then
MsgBox "Please Enter a Description or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequestDescription.SetFocus
Exit Function
End If

Validate = True 'If we get this far, then all validation checks passed.
Me.txtLastUpdate = Date

ExitProc:
Exit Function
ProcError:
Select Case Err.Number
Case 2110
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Validate procedure..."
End Select
Validate = False
Resume ExitProc
End Function


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
J

jamccarley

Since I have all fields as being required on the form, it gives and error if
a field is blank. Therefore it has not saved the data. When they press the
print report button I have the query set to Forms![Hold_tag]![NumberID] so
that it only brings up the one they are working on, and if it did not save,
it is blank. I hope this helps. I will try the code you have written..
Thanks


Tom Wickerath said:
.... and since it cannot update the data if
there is a null field, the report comes out with all fields blanks.

I'm not understanding why a null would cause all [fields] controls on the
report to display blanks. Can you expand on this a bit?
Is there
something I can add to the print button code to either confirm all fields
have data, or to update the form and to cancel the print?

Sure. The easiest way would be to simply set the required value to Yes, in
table design view, for each field that you wish to require data entry. In
addition, set the Allow Zero Lenght property to No for those fields that
include this property. The JET database engine will produce a rather ugly
error message, but it's among the quickest ways of achieving your objective.

Alternatively, you might try setting appropriate Validation Rules with
Validation Text to prompt the user. For example, you can use a validation
rule of: Is Not Null

Although it requires some VBA coding, I prefer to use a form's BeforeUpdate
event procedure to run custom validation code. This allows a higher degree of
validation, and one can set focus to the control that fails validation to
assist the user.

Note: You should use VBA code to save the record when a person clicks on a
button on the form to print (or preview) the record in a report. This way,
your report will include any previously uncommitted changes. Here is an
example, which may seem a bit daunting at first, but it's not that bad if you
work your way through it.

Note: I cut a lot of the code out before pasting into this reply, so
hopefully I didn't leave something out that needs to be there.


Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo ProcError

Dim blnValidate As Boolean

blnValidate = Validate

If blnValidate = False Then
Cancel = True
Err.Number = acDataErrContinue
End If

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Form_BeforeUpdate event procedure..."
Resume ExitProc
End Sub


Private Sub cmdPrint_Click()
On Error GoTo ProcError

'Save any changes first
If Me.Dirty = True Then
If Validate = True Then 'Save the record
Me.Dirty = False
DoCmd.OpenReport "rptActionsIssues", acPreview, _
WhereCondition:="Request_rk = " & Me.Request_rk
End If
Else
If Not Me.NewRecord Then
DoCmd.OpenReport "rptActionsIssues", acPreview, _
WhereCondition:="Request_rk = " & Me.Request_rk
Else
MsgBox "You must enter a new record before attempting to print
it.", _
vbInformation, "No Data To Print..."
End If
End If

ExitProc:
Exit Sub
ProcError:
Select Case Err.Number
Case 2501 'Report open cancelled
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in cmdPrint_Click event procedure ..."
End Select
Resume ExitProc
End Sub

Private Sub cmdClose_Click()
On Error GoTo ProcError

'Save any changes first
If Me.Dirty = True Then
If Validate = True Then
'Save the record and close this form.
Me.Dirty = False
DoCmd.Close acForm, Me.Name
Else
'Record was dirtied, but changes do not pass validation rules, so
bail out.
Exit Sub
End If
Else
DoCmd.Close acForm, Me.Name
End If

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in cmdClose_Click event procedure ..."
Resume ExitProc
End Sub

Private Function Validate() As Boolean
On Error GoTo ProcError

Validate = False 'Initialize return value

' Check for required field entries, if the form is dirty

If IsNull(cboProject) Then
MsgBox "Please Select a Project or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboProject.SetFocus
Exit Function
End If

If IsNull(txtRequestTitle) Then
MsgBox "Please Enter a Title or click on the" & vbCrLf & "Undo button
to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequestTitle.SetFocus
Exit Function
End If

If IsNull(txtRequestNo) Then
MsgBox "Please Enter a Request Number or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequestNo.SetFocus
Exit Function
End If

If IsNull(cboStatus) Then
MsgBox "Please Select a Status or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboStatus.SetFocus
Exit Function
End If

If IsNull(cboPriority) Then
MsgBox "Please Select the Priority Level or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboPriority.SetFocus
Exit Function
End If

If IsNull(cboRequestedBy) Then
MsgBox "Please Select a Requestor or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboRequestedBy.SetFocus
Exit Function
End If

If IsNull(txtRaisedDate) Then
MsgBox "Please Enter the Date Raised or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRaisedDate.SetFocus
Exit Function
Else
If Date < txtRaisedDate Then
MsgBox "You must not enter a future date", vbCritical, "Incorrect
Date Entry..."
txtRaisedDate = Null
txtRaisedDate.SetFocus
Exit Function
End If
End If

If IsNull(cboAssignedTo) Then
MsgBox "Please Select the Assigned Person or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboAssignedTo.SetFocus
Exit Function
End If

If IsNull(txtRequiredDate) Then
MsgBox "Please Enter the Date Required or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequiredDate.SetFocus
Exit Function
Else
If txtRequiredDate < txtRaisedDate Then
MsgBox "The Required Date must occur on or after the Raised Date",
vbCritical, "Incorrect Date Entry..."
txtRequiredDate = Null
txtRequiredDate.SetFocus
Exit Function
End If
End If

If IsNull(cboRequestType) Then
MsgBox "Please Select the Request Type or click on the" & vbCrLf &
"Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboRequestType.SetFocus
Exit Function
End If

If Len(txtRequestDescription) = 0 Then
MsgBox "Please Enter a Description or click on the" & vbCrLf & "Undo
button to discard all changes.", _
vbCritical, "Missing Required Value..."
txtRequestDescription.SetFocus
Exit Function
End If

Validate = True 'If we get this far, then all validation checks passed.
Me.txtLastUpdate = Date

ExitProc:
Exit Function
ProcError:
Select Case Err.Number
Case 2110
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Validate procedure..."
End Select
Validate = False
Resume ExitProc
End Function


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________

jamccarley said:
I created a report based on a form. I used to have my associates print from
the form and made it so that if certain fields were null, it would not print
plus give an error. Now that I have moved it to a report, it allows it to
print even if there is a blank field and since it cannot update the data if
there is a null field, the report comes out with all fields blanks. Is there
something I can add to the print button code to either confirm all fields
have data, or to update the form and to cancel the print? Which in turn
should give them the error I had before, I think. My form is named "Hold_tag"
,one of the fields is "Date_found" and the report is "Hold tag report"
 
T

Tom Wickerath

Since I have all fields as being required on the form, it gives and error if
a field is blank.

Okay. I didn't realize that you had set the required property for every
control on your form.

Looks like I forgot to give you a code snippet that goes in the report's
class module:

Private Sub Report_NoData(Cancel As Integer)
On Error GoTo ProcError

MsgBox "There is no data for the selected criteria.", _
vbInformation, "No Data Available..."
Cancel = True

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure Report_NoData..."
Resume ExitProc
End Sub


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________

jamccarley said:
Since I have all fields as being required on the form, it gives and error if
a field is blank. Therefore it has not saved the data. When they press the
print report button I have the query set to Forms![Hold_tag]![NumberID] so
that it only brings up the one they are working on, and if it did not save,
it is blank. I hope this helps. I will try the code you have written..
Thanks
 

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