Printing a report based on the open form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created a form that is a Record of Verbal Communication, after the
user enters all the information I want to print the REPORT that will be based
on the same information, not just print the form (I can't get it to look
right) besides the user will only want to print the information that was just
entered usually only after the information is entered, since it may be filed,
sent to the client, or any of another hundred things can be done with it. So
my question is when I put a "Print" button at the bottom of the form, I want
it to open the report and print it based on the infomation that is currently
showing on the form (which would be the current record I guess).

Thanks in advance for any help.
 
Two lines of code is all you need behind the button

me.Refresh ' this saves the data to disk

docmd.OpenReport "myReprot",acViewPreview,,"id = " & me!id

The above opens the report in preview mode, and uses the "where" clause to
restrict the report to the ONE record.

If you don't have a primary key field called "id", then either add one, or
simply substitute 'id' for whatever you used for your key.

And, if you want, after testing, you can remove the "acViewPreview...and no
preview will occur..it will go right to the printer...
 
On the OnClick event of the button, you need to perform to stages

' Save the record
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

'Print the report, based on the key field in the form
Dim MyWhereCond As String
MyWhereCond = "[Key Field Name in the table] = " & Me.[Key Field Name in
the form]
docmd.OpenReport "ReportName",,,MyWhereCond
=========================================
If the key field is string add a single quote
MyWhereCond = "[Key Field Name in the table] = '" & Me.[Key Field Name in
the form] & "'"
 
Hi Larry

This is easy, provided the underlying table has uniquely identifiable
records (i.e. has a primary key)

The fourth argument of the OpenReport method is called WhereCondition, and
it is a string with which you can specify selection criteria for the
record(s) to be included in the report. Usually the code would look
something like this:

Dim strSelection as String
strSelection = "[your primary key name]=" & Me![your primary key name]
DoCmd.OpenReport "your report name", , , strSelection

If the primary key is a text field, then you must delimit the key value with
quote marks:

strSelection = "[key name]=""" & Me![key name] & """"

Finally, if the current record has been modified in the form but not yet
saved, you must save it before opening the report, otherwise the report will
show the old data:

If Me.Dirty then Me.Dirty = False
 

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

Back
Top