give msgbox when no data in report

R

Robert

i have a command button on a form to preview report. i want to have msgbox
when no data in the report and automatically close the report. here are my
codes:

Private Sub cmd201not291_Click()
On Error GoTo Err_cmd201not291_Click

Dim stDocName As String

Select Case Sem
Case 1 'Winter
Select Case Loc
Case 1 'Regular
stDocName = "WI201N291R"
Case 2 'Online
stDocName = "WI201N291O"
Case 3 'South Gate
stDocName = "WI201N291S"
End Select
Case 2 'Spring
Select Case Loc
Case 1 'Regular
stDocName = "SP201N291R"
Case 2 'Online
stDocName = "SP201N291O"
Case 3 'South Gate
stDocName = "SP201N291S"
End Select
Case 3 'Summer
Select Case Loc
Case 1 'Regular
stDocName = "SU201N291R"
Case 2 'Online
stDocName = "SU201N291O"
Case 3 'South Gate
stDocName = "SU201N291S"
End Select
Case 4 'Fall
Select Case Loc
Case 1 'Regular
stDocName = "FA201N291R"
Case 2 'Online
stDocName = "FA201N291O"
Case 3 'South Gate
stDocName = "FA201N291S"
End Select
End Select


DoCmd.OpenReport stDocName, acPreview


Exit_cmd201not291_Click:
Exit Sub

Err_cmd201not291_Click:
MsgBox Err.Description
Resume Exit_cmd201not291_Click

End Sub
 
F

fredg

i have a command button on a form to preview report. i want to have msgbox
when no data in the report and automatically close the report. here are my
codes:

Private Sub cmd201not291_Click()
On Error GoTo Err_cmd201not291_Click

Dim stDocName As String

Select Case Sem
Case 1 'Winter
Select Case Loc
Case 1 'Regular
stDocName = "WI201N291R"
Case 2 'Online
stDocName = "WI201N291O"
Case 3 'South Gate
stDocName = "WI201N291S"
End Select
Case 2 'Spring
Select Case Loc
Case 1 'Regular
stDocName = "SP201N291R"
Case 2 'Online
stDocName = "SP201N291O"
Case 3 'South Gate
stDocName = "SP201N291S"
End Select
Case 3 'Summer
Select Case Loc
Case 1 'Regular
stDocName = "SU201N291R"
Case 2 'Online
stDocName = "SU201N291O"
Case 3 'South Gate
stDocName = "SU201N291S"
End Select
Case 4 'Fall
Select Case Loc
Case 1 'Regular
stDocName = "FA201N291R"
Case 2 'Online
stDocName = "FA201N291O"
Case 3 'South Gate
stDocName = "FA201N291S"
End Select
End Select

DoCmd.OpenReport stDocName, acPreview

Exit_cmd201not291_Click:
Exit Sub

Err_cmd201not291_Click:
MsgBox Err.Description
Resume Exit_cmd201not291_Click

End Sub

1) First, change the error handling of the above code so it reads like
this:

Err_cmd201not291_Click:
If Err = 2501 Then
Else
MsgBox Err.Description
End If
Resume Exit_cmd201not291_Click

2) Code the Report's OnNoData event:

MsgBox "There is no data to be reported on."
Cancel = True

That's all you need.
 

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