Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing a form

0 views
Skip to first unread message

Sarah at DaVita

unread,
Apr 25, 2008, 8:57:00 PM4/25/08
to
How can I get my print button to just print the form I am looking at?

Tom Wickerath

unread,
Apr 25, 2008, 9:04:00 PM4/25/08
to
Hi Sarah,

Try this tutorial:

How to print only one page of a multipage report
http://www.access.qbuilt.com/html/reports.html#PrintOnePgOfRpt


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

Sarah at DaVita

unread,
Apr 28, 2008, 12:53:03 PM4/28/08
to
Don't know how to change the code - the instructions are for a report and I
am working with a form. I can easily add the dim statement but do not know
where to put the where clause. My code is below.
Dim stDocName As String
Dim MyForm As Form

stDocName = "VendorSiteAddresses_frm"
Set MyForm = Screen.ActiveForm
DoCmd.SelectObject acForm, stDocName, True
DoCmd.PrintOut
DoCmd.SelectObject acForm, MyForm.Name, False

Tom Wickerath

unread,
Apr 28, 2008, 1:03:01 PM4/28/08
to
Hi Sarah,

I do not recommend that you attempt to print a form. Forms are for entering
and viewing data, but they're not intended for printing. For one thing, there
is no way of suppressing a form background from printing. Most forms have
some type of background.

Create a report with the same fields on it as your form. Then use the idea
that I show to open the report with just that one record. You can have a
command button on the form for opening the report. My ImageDemo database
provides an example of being able to print the current record displayed in a
form:

http://www.accessmvp.com/TWickerath/downloads/ImageDemo.zip

Sarah at DaVita

unread,
Apr 28, 2008, 4:08:01 PM4/28/08
to
Ok am trying that - makes good sense. Below is my code but it only prints a
blank report. The report has a field called vendor/site and the form also
contains this field - why whould it not print the data:?
Private Sub PrintformReport_Click()
On Error GoTo Err_PrintformReport_Click

Dim stDocName As String

stDocName = "Vendor Sites and Addresses Report"
DoCmd.OpenReport stDocName, , _
WhereCondition:="[Vendor/Site]" = " & Me.vendor/site"
DoCmd.OpenReport
Exit_PrintformReport_Click:
Exit Sub

Err_PrintformReport_Click:
MsgBox Err.Description
Resume Exit_PrintformReport_Click

End Sub

Thanks for all your help on this.

Tom Wickerath

unread,
Apr 28, 2008, 7:28:01 PM4/28/08
to
Hi Sarah,

Use the field on your form that serves as the primary key, since a primary
key uniquely identifies a given record. Is vendor/site your primary key
field? Also, your current usage indicates that vendor/site is a numeric
field. Is this correct?

> DoCmd.OpenReport stDocName, , _
> WhereCondition:="[Vendor/Site]" = " & Me.vendor/site"

You are using a named argument (WhereCondition), so you should not need the
second comma on the first line shown above. So, try this instead:

DoCmd.OpenReport stDocName, _
WhereCondition:="[pkField]" = " & Me.pkField"

where pkField is a numeric primary key, such as an autonumber. If you are
using a text-based primary key, then you need to include quotes, like this:

DoCmd.OpenReport stDocName, _
WhereCondition:="[pkField]" = '" & Me.pkField & "'"

If your primary key field includes spaces or special characters in it's
name, then you'll need to use square brackets as well, like this:

Numeric pk
DoCmd.OpenReport stDocName, _
WhereCondition:="[pkField]" = " & Me.[pkField]"

Text pk
DoCmd.OpenReport stDocName, _
WhereCondition:="[pkField]" = '" & Me.[pkField] & "'"

You will eliminate future headaches if you avoid the use of spaces and other
special characters, and reserved words, in anything that you assign a name to
within Access. So, for example, the vendor/site field would be better named
VendorSite. This way, Access will never interpret the forward slash as
division. The report named "Vendor Sites and Addresses Report" would be
better named "VendorSitesAndAddressesReport" (no spaces), or if you want to
use standard naming conventions "rptVendorSitesAndAddresses". Here are some
references on this topic:

Special characters that you must avoid when you work with Access databases
http://support.microsoft.com/?id=826763

Problem names and reserved words in Access
http://allenbrowne.com/AppIssueBadWord.html

Sarah at DaVita

unread,
Apr 28, 2008, 7:36:01 PM4/28/08
to
Thank you!!! I did finally figure out that I needed to make my vendor/site a
literal. All you experts who help us out are great!!

Tom Wickerath

unread,
Apr 29, 2008, 12:25:00 AM4/29/08
to
Glad to hear that you got it working!
I still encourage you to avoid the use of special characters (spaces,
forward slashes, etc.), along with any reserved words, in anything that you
assign a name to within Access (fields, tables, queries, forms, reports,
macros, modules, along with controls on forms and reports).

Tom Wickerath

unread,
Apr 29, 2008, 11:49:00 PM4/29/08
to
Hi Sarah,

I just realized that I forgot to mention that you should force a save of the
record, if you are using a bound form (a bound form is one that has a table
or query specified as the Record Source property). This way, your report will
always show the latest data changes. Using your procedure:

Private Sub PrintformReport_Click()
On Error GoTo Err_PrintformReport_Click

Dim stDocName As String

stDocName = "Vendor Sites and Addresses Report"

:
<rest of code here>
:
End Sub


Add the test for Me.Dirty to force a save of the record before opening the
report:

Private Sub PrintformReport_Click()
On Error GoTo Err_PrintformReport_Click

Dim stDocName As String

stDocName = "Vendor Sites and Addresses Report"

If Me.Dirty = True Then
Me.Dirty = False
End If

:
<rest of code here>
:
End Sub


This way, if you have made a change to the data in the form, without first
having saved it (ie. pencil symbol is shown in record selector, if you are
displaying the record selector), the report will include these changes since
you are forcing a save of the record first.

0 new messages