Need Help with Error Handling

  • Thread starter Thread starter CSDunn
  • Start date Start date
C

CSDunn

Hello,
I have an Access 2000 ADP report that contains a number of bound text boxes.
The report shows student report card marks. If the value of any text box
contains an 'X', then the BackColor of the textbox is shaded black.
Otherwise, the value of the textbox appears.

The following code is used in the OnFormat event of the report header:

Private Sub FormHeader_Format(Cancel As Integer, FormatCount As Integer)
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Value = "X" Then
ctl.BackColor = 0
Else
ctl.BackColor = ctl.Tag
End If
End If
Next ctl
End Sub

This works fine until a user tries to view a report card for a student where
no report card data has been input. When a user tries to preview such a
report, the following error message appears when the user clicks a button
from a form to preview the report:
****************************
Run-time error "2427";

You entered an expression that has no value.
****************************
When I debug, the error points to line six of the above code;
'If ctl.Value = "X" Then'

I suppose that this message is being generated becasue there is no data for
the report. How can I handle this error so that the user can receive a
friendly message like, "There is no report card data for this student."?

Thanks for your help!

CSDunn
 
CS,

Try putting this at the beginning of your procedure:

On Error GoTo Err_FormHeader_Format

And this at the end of your procedure:

Exit_FormHeader_Format:
Exit Sub

Err_FormHeader_Format:
If Err = 2427 Then
MsgBox "There is no report card data for this student.", vbCritical
Cancel = True

Else:
MsgBox Err.Description
Resume Exit_FormHeader_Format
End If

-Andy
 
I would suggest using the NZ function so you don't stop
the report in cases where there was some data that should
be displayed.
i.e.
If NZ(ctl.Value,"") = "X" Then....

Still use the error handling though as Andy illustrated.
 
Back
Top