Preveiw a report showing only the current record information

A

Alain

I have another problem with my form.
On it, I have a command button to "print preview" a report. I would like this report to use only the current information on the active form.
Currently, it will show all the records, starting with the first one on the list. Here is the code behind that button:

Private Sub PreviewReport_Click()
On Error GoTo Err_PreviewReport_Click

Dim stDocName As String

stDocName = "All Tenants Info Sorted by Unit No"
DoCmd.OpenReport stDocName, acPreview

Exit_PreviewReport_Click:
Exit Sub

Err_PreviewReport_Click:
MsgBox Err.Description
Resume Exit_PreviewReport_Click

End Sub

The name of the report (as shown above) is "All Tenants Info Sorted by Unit No"

What command should be added to show (or print) the current active record that is shown on my form.

Thank you for helping a beginner,
Alain
 
D

Douglas J. Steele

You need pass a Where condition with your OpenReport method. Something like:


Private Sub PreviewReport_Click()
On Error GoTo Err_PreviewReport_Click

Dim stDocName As String
Dim stCond As String

stDocName = "All Tenants Info Sorted by Unit No"
stCond = "TenantId = " & Me.txtTenant
DoCmd.OpenReport stDocName, acPreview, , stCond

Exit_PreviewReport_Click:
Exit Sub

Err_PreviewReport_Click:
MsgBox Err.Description
Resume Exit_PreviewReport_Click

End Sub

Replace TenantId and Me.txtTenant with the appropriate names. If TenantId is
text, that would need to be

stCond = "TenantId = '" & Me.txtTenant & "'"


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


I have another problem with my form.
On it, I have a command button to "print preview" a report. I would like
this report to use only the current information on the active form.
Currently, it will show all the records, starting with the first one on the
list. Here is the code behind that button:

Private Sub PreviewReport_Click()
On Error GoTo Err_PreviewReport_Click

Dim stDocName As String

stDocName = "All Tenants Info Sorted by Unit No"
DoCmd.OpenReport stDocName, acPreview

Exit_PreviewReport_Click:
Exit Sub

Err_PreviewReport_Click:
MsgBox Err.Description
Resume Exit_PreviewReport_Click

End Sub

The name of the report (as shown above) is "All Tenants Info Sorted by Unit
No"

What command should be added to show (or print) the current active record
that is shown on my form.

Thank you for helping a beginner,
Alain
 
F

fredg

I have another problem with my form.
On it, I have a command button to "print preview" a report. I would like this report to use only the current information on the active form.
Currently, it will show all the records, starting with the first one on the list. Here is the code behind that button:

Private Sub PreviewReport_Click()
On Error GoTo Err_PreviewReport_Click

Dim stDocName As String

stDocName = "All Tenants Info Sorted by Unit No"
DoCmd.OpenReport stDocName, acPreview

Exit_PreviewReport_Click:
Exit Sub

Err_PreviewReport_Click:
MsgBox Err.Description
Resume Exit_PreviewReport_Click

End Sub

The name of the report (as shown above) is "All Tenants Info Sorted by Unit No"

What command should be added to show (or print) the current active record that is shown on my form.

Thank you for helping a beginner,
Alain

Your table should have a unique prime key field.
In my example it is named [RecordID].

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]

The above assumes a [RecordID] field that is a Number Datatype.

If, however, [RecordID] is Text Datatype, then use:

DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

as the Where clause.

For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "

Change ReportName and [RecordID] to whatever the actual table and
field name is that you are using.

See VBA Help files for:
Where Clause + Restrict data to a subset of records'
 
A

Alain

Thank you very much for the help.
This worked flawlessly and exactly as I wanted. I much appreciate the fact
that you are sharing your knowledge with the Access community at large.
Alain
 
A

Alain

Fred,
Thank you for the quick response. The solution passed by Doug worked
well.
Yours is very similar and would achieve the same results.
Thanks again for your time in providing a response.
Alain

fredg said:
I have another problem with my form.
On it, I have a command button to "print preview" a report. I would like
this report to use only the current information on the active form.
Currently, it will show all the records, starting with the first one on
the list. Here is the code behind that button:

Private Sub PreviewReport_Click()
On Error GoTo Err_PreviewReport_Click

Dim stDocName As String

stDocName = "All Tenants Info Sorted by Unit No"
DoCmd.OpenReport stDocName, acPreview

Exit_PreviewReport_Click:
Exit Sub

Err_PreviewReport_Click:
MsgBox Err.Description
Resume Exit_PreviewReport_Click

End Sub

The name of the report (as shown above) is "All Tenants Info Sorted by
Unit No"

What command should be added to show (or print) the current active record
that is shown on my form.

Thank you for helping a beginner,
Alain

Your table should have a unique prime key field.
In my example it is named [RecordID].

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]

The above assumes a [RecordID] field that is a Number Datatype.

If, however, [RecordID] is Text Datatype, then use:

DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

as the Where clause.

For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "

Change ReportName and [RecordID] to whatever the actual table and
field name is that you are using.

See VBA Help files for:
Where Clause + Restrict data to a subset of records'
 

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