Simple report code to "Print Page One Only"

  • Thread starter Thread starter rwrede
  • Start date Start date
R

rwrede

I am not an expert at Access, I simply created a form / report I use at work
as an order form. When I want to print the report (order form) I must be
careful to remember and set print properties to "Page 1 of 1" or I end up
printing all the pages in my report. Is there a code to add to the visual
basic (I'm thinking the report in design view) to print only page 1? Or
better yet, the page I am viewing in the print preview at the time.?
 
rwrede said:
I am not an expert at Access, I simply created a form / report I use at work
as an order form. When I want to print the report (order form) I must be
careful to remember and set print properties to "Page 1 of 1" or I end up
printing all the pages in my report. Is there a code to add to the visual
basic (I'm thinking the report in design view) to print only page 1? Or
better yet, the page I am viewing in the print preview at the time.?


Possibly, there is a way to do that, but a far better idea
is to add a button to the form and use a little code in the
button's Click event procedure to filter the report so it
only displays the current record on the form. If you use
the button wizard it will generate most of the code so all
you have to do is modify it a little so it looks like:

Dim strDocName As String
Dim strWhere As String
strDocName = "name of report"
strWhere = "[key field name] = " & Me.[key field name]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere
 
what do I put in 'key field name'? I want it to print the report I have
created, there are several fields...?

Marshall Barton said:
rwrede said:
I am not an expert at Access, I simply created a form / report I use at
work
as an order form. When I want to print the report (order form) I must be
careful to remember and set print properties to "Page 1 of 1" or I end up
printing all the pages in my report. Is there a code to add to the visual
basic (I'm thinking the report in design view) to print only page 1? Or
better yet, the page I am viewing in the print preview at the time.?


Possibly, there is a way to do that, but a far better idea
is to add a button to the form and use a little code in the
button's Click event procedure to filter the report so it
only displays the current record on the form. If you use
the button wizard it will generate most of the code so all
you have to do is modify it a little so it looks like:

Dim strDocName As String
Dim strWhere As String
strDocName = "name of report"
strWhere = "[key field name] = " & Me.[key field name]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere
 
[key field name] needs to be replaced with the name of the
table's primary key field's name. It is the field that can
be used to uniquely identify a record in the table.

The expression we stuffed into the strWhere string is used
in the OpenReport method's WhereCondition argument to filter
the report to a single record. As long as the key field is
included in both the form's and the report's record source
table/query, this should do what I think you are looking
for.
--
Marsh
MVP [MS Access]

what do I put in 'key field name'? I want it to print the report I have
created, there are several fields...?
I am not an expert at Access, I simply created a form / report I use at
work
as an order form. When I want to print the report (order form) I must be
careful to remember and set print properties to "Page 1 of 1" or I end up
printing all the pages in my report. Is there a code to add to the visual
basic (I'm thinking the report in design view) to print only page 1? Or
better yet, the page I am viewing in the print preview at the time.?

"Marshall Barton" wrote
Possibly, there is a way to do that, but a far better idea
is to add a button to the form and use a little code in the
button's Click event procedure to filter the report so it
only displays the current record on the form. If you use
the button wizard it will generate most of the code so all
you have to do is modify it a little so it looks like:

Dim strDocName As String
Dim strWhere As String
strDocName = "name of report"
strWhere = "[key field name] = " & Me.[key field name]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere
 
I tried it, it doesn't print desired report (page 1 only), it only opens it
in print preview, then I must click print>page 1 of 1 like I did before... I
would like to click it (either in "Form" or "Report") and have it print page
1 only (by default the Report shows the last form entry, and is called page
1)


Marshall Barton said:
[key field name] needs to be replaced with the name of the
table's primary key field's name. It is the field that can
be used to uniquely identify a record in the table.

The expression we stuffed into the strWhere string is used
in the OpenReport method's WhereCondition argument to filter
the report to a single record. As long as the key field is
included in both the form's and the report's record source
table/query, this should do what I think you are looking
for.
--
Marsh
MVP [MS Access]

what do I put in 'key field name'? I want it to print the report I have
created, there are several fields...?
rwrede wrote:
I am not an expert at Access, I simply created a form / report I use at
work
as an order form. When I want to print the report (order form) I must be
careful to remember and set print properties to "Page 1 of 1" or I end
up
printing all the pages in my report. Is there a code to add to the
visual
basic (I'm thinking the report in design view) to print only page 1? Or
better yet, the page I am viewing in the print preview at the time.?

"Marshall Barton" wrote
Possibly, there is a way to do that, but a far better idea
is to add a button to the form and use a little code in the
button's Click event procedure to filter the report so it
only displays the current record on the form. If you use
the button wizard it will generate most of the code so all
you have to do is modify it a little so it looks like:

Dim strDocName As String
Dim strWhere As String
strDocName = "name of report"
strWhere = "[key field name] = " & Me.[key field name]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere
 
Either you have completely lost me, or you are not
navigating to the desired record on the form before not
clicking the report button, or something else???

Please try to provide more specific information about what
you are doing and the result you're getting.
--
Marsh
MVP [MS Access]

I tried it, it doesn't print desired report (page 1 only), it only opens it
in print preview, then I must click print>page 1 of 1 like I did before... I
would like to click it (either in "Form" or "Report") and have it print page
1 only (by default the Report shows the last form entry, and is called page
1)


Marshall Barton said:
[key field name] needs to be replaced with the name of the
table's primary key field's name. It is the field that can
be used to uniquely identify a record in the table.

The expression we stuffed into the strWhere string is used
in the OpenReport method's WhereCondition argument to filter
the report to a single record. As long as the key field is
included in both the form's and the report's record source
table/query, this should do what I think you are looking
for.

what do I put in 'key field name'? I want it to print the report I have
created, there are several fields...?


rwrede wrote:
I am not an expert at Access, I simply created a form / report I use at
work
as an order form. When I want to print the report (order form) I must be
careful to remember and set print properties to "Page 1 of 1" or I end
up
printing all the pages in my report. Is there a code to add to the
visual
basic (I'm thinking the report in design view) to print only page 1? Or
better yet, the page I am viewing in the print preview at the time.?


"Marshall Barton" wrote
Possibly, there is a way to do that, but a far better idea
is to add a button to the form and use a little code in the
button's Click event procedure to filter the report so it
only displays the current record on the form. If you use
the button wizard it will generate most of the code so all
you have to do is modify it a little so it looks like:

Dim strDocName As String
Dim strWhere As String
strDocName = "name of report"
strWhere = "[key field name] = " & Me.[key field name]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere
 
I added a print button to my form... I added this code (replaced) to the
form button in VB,

Dim strDocName As String
Dim strWhere As String
strDocName = "Order Form"
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere

When I click the print button on my form I get an inquiry asking "Which
OrderID" I put in the desired number and when I click "Ok" I get the report
in it's print preview mode... (it doesn't print on it's own and if I print
it manually it prints all the pages of the report, not just the one in view)




Marshall Barton said:
Either you have completely lost me, or you are not
navigating to the desired record on the form before not
clicking the report button, or something else???

Please try to provide more specific information about what
you are doing and the result you're getting.
--
Marsh
MVP [MS Access]

I tried it, it doesn't print desired report (page 1 only), it only opens
it
in print preview, then I must click print>page 1 of 1 like I did before...
I
would like to click it (either in "Form" or "Report") and have it print
page
1 only (by default the Report shows the last form entry, and is called
page
1)


Marshall Barton said:
[key field name] needs to be replaced with the name of the
table's primary key field's name. It is the field that can
be used to uniquely identify a record in the table.

The expression we stuffed into the strWhere string is used
in the OpenReport method's WhereCondition argument to filter
the report to a single record. As long as the key field is
included in both the form's and the report's record source
table/query, this should do what I think you are looking
for.


rwrede wrote:
what do I put in 'key field name'? I want it to print the report I have
created, there are several fields...?


rwrede wrote:
I am not an expert at Access, I simply created a form / report I use
at
work
as an order form. When I want to print the report (order form) I must
be
careful to remember and set print properties to "Page 1 of 1" or I end
up
printing all the pages in my report. Is there a code to add to the
visual
basic (I'm thinking the report in design view) to print only page 1?
Or
better yet, the page I am viewing in the print preview at the time.?


"Marshall Barton" wrote
Possibly, there is a way to do that, but a far better idea
is to add a button to the form and use a little code in the
button's Click event procedure to filter the report so it
only displays the current record on the form. If you use
the button wizard it will generate most of the code so all
you have to do is modify it a little so it looks like:

Dim strDocName As String
Dim strWhere As String
strDocName = "name of report"
strWhere = "[key field name] = " & Me.[key field name]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere
 
I know what my problem is... I don't have the 'primary key field' in my
report, just the form. I can't find the field in the "List of Available
Fields" in the report design view. How do I add a field to an existing
report it isn't listed in "List of Available Fields"


rwrede said:
I added a print button to my form... I added this code (replaced) to the
form button in VB,

Dim strDocName As String
Dim strWhere As String
strDocName = "Order Form"
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere

When I click the print button on my form I get an inquiry asking "Which
OrderID" I put in the desired number and when I click "Ok" I get the
report in it's print preview mode... (it doesn't print on it's own and if
I print it manually it prints all the pages of the report, not just the
one in view)




Marshall Barton said:
Either you have completely lost me, or you are not
navigating to the desired record on the form before not
clicking the report button, or something else???

Please try to provide more specific information about what
you are doing and the result you're getting.
--
Marsh
MVP [MS Access]

I tried it, it doesn't print desired report (page 1 only), it only opens
it
in print preview, then I must click print>page 1 of 1 like I did
before... I
would like to click it (either in "Form" or "Report") and have it print
page
1 only (by default the Report shows the last form entry, and is called
page
1)


[key field name] needs to be replaced with the name of the
table's primary key field's name. It is the field that can
be used to uniquely identify a record in the table.

The expression we stuffed into the strWhere string is used
in the OpenReport method's WhereCondition argument to filter
the report to a single record. As long as the key field is
included in both the form's and the report's record source
table/query, this should do what I think you are looking
for.


rwrede wrote:
what do I put in 'key field name'? I want it to print the report I have
created, there are several fields...?


rwrede wrote:
I am not an expert at Access, I simply created a form / report I use
at
work
as an order form. When I want to print the report (order form) I must
be
careful to remember and set print properties to "Page 1 of 1" or I
end
up
printing all the pages in my report. Is there a code to add to the
visual
basic (I'm thinking the report in design view) to print only page 1?
Or
better yet, the page I am viewing in the print preview at the time.?


"Marshall Barton" wrote
Possibly, there is a way to do that, but a far better idea
is to add a button to the form and use a little code in the
button's Click event procedure to filter the report so it
only displays the current record on the form. If you use
the button wizard it will generate most of the code so all
you have to do is modify it a little so it looks like:

Dim strDocName As String
Dim strWhere As String
strDocName = "name of report"
strWhere = "[key field name] = " & Me.[key field name]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere
 
rwrede said:
I added a print button to my form... I added this code (replaced) to the
form button in VB,

Dim strDocName As String
Dim strWhere As String
strDocName = "Order Form"
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport strDocName, acViewPreview, , strWhere

When I click the print button on my form I get an inquiry asking "Which
OrderID" I put in the desired number and when I click "Ok" I get the report
in it's print preview mode... (it doesn't print on it's own and if I print
it manually it prints all the pages of the report, not just the one in view)


You should not be getting a prompt for the OrderID. I don't
know where it's coming from, but maybe adding that field to
the report's record source query will deal with it???

To include the field in the report's query, open the query
in design view. You should see the base table's field list,
then drag the OrderID field down to the query's field list.
Close and save the query.

Now open the report in design view, you should now see the
OrderID field in the report's field list (View menu - Field
List). Drag the field to somewhere on the report to create
a text box bound to the field. If you do not want to
display the OrderID value in the report, set the text box's
Visible property to No.

Once all that is working, if you open the report from the
database window you should see **all** the orders. But,
when you use the report button on the form, you should,
without any prompt, see only the order that was selected on
the form. Printing that report while previewing it should
just be a matter of clicking on the Print tool bar item. If
you do not want to preview the order and send it directly to
the printer, you can create another button on the form using
the same code except for using acViewNormal instead of
acViewPreview.
 
Back
Top