How do I program a command button?

G

Guest

I installed a command button on a form to print the current form being
veiwed. It wants to print my whole database. How do I program the button to
print only the record being veiwed?
 
D

Douglas J. Steele

The OpenReport method allows you to specify a Where condition (as the fourth
argument). For some odd reason, while the Wizard includes a variable
stLinkCriteria in the code to hold this value when you go to open a form, it
doesn't include it when you go to open a report.

Simply create the appropriate condition (it's a WHERE clause without the
word "WHERE" in it), and pass it as the 4th parameter in the OpenReport
method.
 
F

fredg

I installed a command button on a form to print the current form being
veiwed. It wants to print my whole database. How do I program the button to
print only the record being veiwed?

First create a report that will print the data the way you want it.

Your table should have a unique prime key field.
In my example it is named [RecordID].

On the command button's property sheet write
[Event Procedure]
on the Click event line.
Then click on the little button with 3 dots that will appear on that
line.
When the code window opens, the cursor will be flashing between 2
already existing lines of code.
Between those 2 lines write:

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]

The above assumes a [RecordID] field that is a Number Datatype.

If, however, [RecordID] is Text Datatype, then use:

DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

as the Where clause.

For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "
Change [RecordID] to whatever the actual field name is that you are
using.

See VBA Help files for:
Where Clause + Restrict data to a subset of records
 
P

Pieter Wijnen

You shouldn't, Create a report based on the Form & Add this Code (on Click,
Select Event Procedure)

Private Sub Command0_Click()

Access.DoCmd.OpenReport "MyReport", Access.AcView.acViewPreview,
WhereCondition:= "MyIDField=" & Me.MyIDField.Value

' If "MyIDField" is a Text field use
' Access.DoCmd.OpenReport "MyReport", Access.AcView.acViewPreview,
WhereCondition:= "MyIDField='" & Me.MyIDField.Value & "'"
End Sub

Replacing all references with yours, offcourse

HtH

Pieter
 

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