Yes, you can print only one report sheet at a time, as long as you can uniquely identify the
record in question. Primary keys are used in databases to uniquely identify records. The idea
is to open up a report that only contains the information you wish to print (ie. not 14 other
pages).
You can use Docmd.OpenReport in VBA code, and include a WhereCondition argument. Create a new
subroutine. Enter "Docmd.OpenReport" (without the quotes). Select this with your mouse and then
press F1. You should get help on this method.
OpenReport(ReportName, View, FilterName, WhereCondition, WindowMode, OpenArgs)
FilterName Optional Variant. A string expression that's the valid name of a query in the current
database.
WhereCondition Optional Variant. A string expression that's a valid SQL WHERE clause without the
word WHERE.
You can also use the FilterName argument instead of the WhereCondition. Here is a step-by-step
procedure, using the WhereCondition argument, that will hopefully get you going in the right
direction:
1.) Open your form in design view.
2.) Click on View > Toolbox. Make sure the control wizards are activated. The background color
of the control wizards button should be the same as the select objects button.
3.) Click the command button icon and drag a new button onto your form, with your left mouse
button depressed. Release the left mouse button when you have the desired shape of the button.
4.) Select Report Operations and Preview Report in the next screen. Click Next.
5.) Select the name of the report that you wish to use in the next screen. Click Next.
6.) I suggest selecting Text in the next screen, and entering something like: &Print Record
(or &Preview Record)
The ampersand is a "hot key", which will allow you to print the report by pressing the ALT key
and the P key simultaneously. Note: Choose a different letter if you've already used P as a hot
key somewhere else on this form. Click Next.
7.) Give the command button a meaningful name. I strongly encourage you to use a naming
convention, such as lowercase "cmd" (without the quotes) as the prefix for command buttons on
your form. You might name the command button something like: cmdPreviewRecord
8.) Click on Finish to close the wizard. Click on View > Code to open up the code module
associated with this form. You should see VBA code that the wizard just wrote for you. It
should look something like this:
Private Sub cmdPreviewRecord_Click()
On Error GoTo Err_cmdPreviewRecord_Click
Dim stDocName As String
stDocName = "rptYourReportName"
DoCmd.OpenReport stDocName, acPreview
Exit_cmdPreviewRecord_Click:
Exit Sub
Err_cmdPreviewRecord_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewRecord_Click
End Sub
9.) Look at the top of your new code module. Do you see the words "Option Explicit" as the
second line of code? If not, type this line of code in (without the quotes). We need to modify
the line of code that reads
DoCmd.OpenReport stDocName, acPreview
by adding the optional WhereCondition argument.
10.) Determine the fieldname and datatype for the primary key on your form. Make a note of this
information. The WhereCondition argument will be slightly different, depending on whether you
are using a text based primary key or an autonumber (ie. long integer) primary key.
11.) For an autonumber primary key, add the following lines of code:
Dim lngRecNum As Long
lngRecNum = Me![PKFieldName]
For a text based primary key, add the following lines of code:
Dim strRecID As String
strRecID = "[PKFieldName]='" & Me![PKFieldName] & "'"
where PKFieldName is the name of the field that you determined in step 10.
Important note for text based primary key:
For clarity, that's a single quote ( ' ) + double quote ( " ) after the equal sign, and a double
quote ( " ) + single quote ( ' ) + double quote ( " ) after the last ampersand ( & ) symbol.
12.) Now, we will add the optional WhereCondition argument to the line of code that starts with
Docmd.OpenReport
For an autonumber primary key, use:
DoCmd.OpenReport stDocName, acPreview, , "[PKFieldName] = " & lngRecNum & ""
For a text based primary key, use:
DoCmd.OpenReport stDocName, acPreview, , strRecID
13.) Click on Debug > Compile DatabaseName. Hopefully, your code will compile without any
errors. Click on the Save toolbar button. Close the VBE editor.
14.) Open your form in normal preview mode. Test you new command button.
Tom
__________________________________________
I get my report pages to print only ONE entry at a time which I need. I
can get them to pull up and view one at a time just fine also but when
I go to print ONE out, it wants to print them ALL out.
Meaning, all of my "items" are placed on seperate pages (one per page,
this is now I needed to set it up). But when I hit print, the page I am
looking at pulls up just fine in the preview but it not only wants to
print that record it wants to print ALL pages in the report, meaning
ALL my items. Kind of like if you had a 15 page report in Word and you
wanted to pull up page 10 and ONLY print that page.
Is there an easier way to do this rather than having to remember to
switch my print settings to "current page" every time I print? Because
this is how I am always going to print these reports, I only need them
when I create a new entry, and I only need that CURRENT entry.
I could see it causing me a jam if I keep forgetting to change my
printer settings every time.
G.