command button help please

G

Guest

I am new to access and tried to get an answer in another discussion group,
and it was above my head, so I was probably not in the correct area, so I am
trying here. I have created a form page in access, to add data to my
database. I want to add a print command button to the form, that will print
ONLY the record that is visible on the screen. The way it is working right
now, is that it prints every single record. How do I change it to only print
the page that I am clicking the print button on?
 
G

Guest

Firstly I'd recommend that rather than printing the form itself, which I
assume is what you are doing, you design a report based on your table (the
report wizard will do most of the work for you). The standard of
presentation of printed output you can achieve in a report is far higher than
by printing a form.

Once you have your report set up you can open it from your form with a
button which restricts the report to the current record. To do this you need
to identify a column or combination of columns in the underlying table or
query on which you form is based which uniquely identifies each record. This
will usually be the primary key of the table, so lets assume that’s a number
field called MyID.

The other thing you need to ensure before printing the report is that the
current record is saved. If its an unsaved new record it won't be printed at
all; if its an existing record you've just changed but not saved then the
report will print the old data. So, putting these two things together, the
code in the button's Click event procedure would go something like this:

' ensure record is saved
RunCommand acCmdSaveRecord
' print report for just current record
' DoCmd.OpenReport "YourReportName", _
WhereCondition:= "MyID = " & Me.MyID

The WhereCondition argument of the OpenReport method is a string expression
which filters the report. In the above example the literal string "MyID = "
is concatenated with the value of the MyID field of the currently selected
record in the form, so if this was MyID number 42 the expression would
evaluate to "MyID = 42" and the report would be printed for just that record.

While I don't recommend it, to print a form for just the current record you
first select it:

DoCmd.RunCommand acCmdSelectRecord
DoCmd.PrintOut acSelection
 

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