Create Report by using the current record in a form

E

ember

hello all,

i have created a report where i fill the parameters in the parameter
query so i can print it. but now i need to create a report where the
record to be printed is the current record displayed on the form (i.e,
the form is open and record #23 is showing and i want that record to be
printed in the report; but if i open record #14 in the same form, i
want to print that record (#14).

is it possible? what should i do??

thanks in advance,

ember
 
A

Arvin Meyer

Use the wizard to create a button to open the report. Then change the report
open line in the code so it looks like this:

Private Sub cmdPrint_Click()
On Error GoTo Err_cmdPrint_Click

Dim stDocName As String

stDocName = "YourReportName"
DoCmd.OpenReport stDocName, acNormal, , "ID=" & Me.txtID

Exit_cmdPrint_Click:
Exit Sub

Err_cmdPrint_Click:
MsgBox Err.Description
Resume Exit_cmdPrint_Click

End Sub

Where ID is the Primary Key name in your table and txtID is the name of that
field's textbox control in your form.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access
 
G

Guest

You can do it one of two ways. One would be to use a reference to form as a
parameter in the report's underlying query, e.g.

SELECT *
From MyTable
WHERE MyID = Forms!MyForm!MyID;

On the form you can then have a button to print the report with code along
these lines in its Click event procedure. Note that its important to make
sure the record is saved first:

RunCommand acCmdSaveRecord
DoCmd.OpenReport "MyReport"

The other is to omit any parameters from the report's query, so it will
print all records if opened from the database window, and filter it when you
open it:

RunCommand acCmdSaveRecord
DoCmd.OpenReport "MyReport", WhereCondition:= "MyID = " & Me.MyID

This assumes that MyID, the primary key of the table, is a number data type.
If it were text you'd wrap the value in quotes:

RunCommand acCmdSaveRecord
DoCmd.OpenReport "MyReport", WhereCondition:= "MyID = """ & Me.MyID & """"
 

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