Open correct report from form

T

tracy.cooperjr

I am writing a database for a university and have been unable to solve
this problem my self. I need to pull a select set of reports according
to a persons Last Name and First Name. I am able to do this by manually
typing in the requested information so I know the report itself works.
What I would like to be able to do is have that information
automatically input by the open form. I have tried building a link
between the form and the requested info but it keeps asking me to input
the information rather than grabbing it from the report.

Background
Report is based off of a query linking Students table to Class Schedule
table

Form is based off of Students Table
 
G

Guest

I assume you want to restrict the report to student in the form's current
record. You could do this in one of two ways:

1. Include references to the form's FirstName and LastName controls as
parameters in the report's underlying query's WHERE clause:

WHERE FirstName = Forms!YourForm!FirstName AND LastName =
Forms!YourForm!LastName

2. Open the report form a button on the form and filter it to the current
student's first and last names by means of the OpenReport method's
WhereCondition argument:

Const conREPORTNAME = "YourReportNameGoesHere"
Dim strCriteria As String

strCriteria = "FirstName = """ & Me.FirstName & _
""" And LastName = """ & Me.LastName & """"

' first make sure form's current record is saved
RunCommand acCmdSaveRecord

' open report in print preview
DoCmd.OpenReport conREPORTNAME, _
View:=acViewPreview, _
WhereCondition:=strCriteria

Names don't make good keys, however, as two students could have the same
names. Its better to use a unique numeric StudentID (e.g. an autonumber) as
the primary key of the Students table (and foreign key of the ClassSchedule
table, but not an autonumber in this case of course). Make sure the former
is included in both the form's and the report's underlying RecordSources.
You can then filter the report on this single column:

Const conREPORTNAME = "YourReportNameGoesHere"
Dim strCriteria As String

strCriteria = "StudentID = " & Me.StudentID

' first make sure form's current record is saved
RunCommand acCmdSaveRecord

' open report in print preview
DoCmd.OpenReport conREPORTNAME, _
View:=acViewPreview, _
WhereCondition:=strCriteria

Ken Sheridan
Stafford, England
 

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