In Access check for valid record ID before printing report?

D

Duke

How do I stop a Access report form showing no data when an incorrect record
ID # is entered into a parameter textbox on an input popup form?
 
E

Evi

Put a MsgBox in the On No Date Event of the Report

Private Sub Report_NoData(Cancel As Integer)
Dim Msg As String
Msg = "You haven't entered any figures for this period yet"
'put your own message here
MsgBox Msg
DoCmd.CancelEvent
End Sub
 
L

Larry Linson

Duke said:
How do I stop a Access report form showing no data when an incorrect record
ID # is entered into a parameter textbox on an input popup form?

A better approach is to create a ComboBox with its RowSource being a Query
that extracts the record ID #s from the RecordSource and Limit to List, so
the user can NOT "enter an incorrect record ID". It's little things like
this which make an application "user-friendly" and keep your clients/users
happy.

What do you mean "keep it from showing 'no data'"? You can cancel the
Report in the OnNoData event in recent versions of Access and manipulate the
properties to minimize the system messages that are displayed. Or, you can
write VBA code behind the Form to check that the value in the TextBox is a
valid Record ID and never call the report if it is not.

Caution: "Air Code", untested

Public Sub()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDB()
Set rs = db.OpenRecordset ("nameofyourRecordSourcehere")
If Not (rs.BOF and rs.EOF) Then
rs.MoveFirst
rs.FindFirst "[ID #] = " & Me.txtID#
If Not rs.NoMatch Then
DoCmd.OpenReport "nameofyourReporthere", . . ., "[ID #] = " &
Me.txtID#
Else
MsgBox "No such Record, please try again"
End If
Else
MsgBox "There are no records in the Record Source"
End If
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Sub
End

The above uses [ID #] for your Field name (replace with your own field name)
and assumes the Field ID # is numeric. You'll need extra quotes if it is
Text.

Larry Linson
Microsoft Office Access MVP
 

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