Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cmd Button to print a selected report from a form??

Status
Not open for further replies.

SyBeerianTyGRRRR

Technical User
Jul 16, 2003
31
0
0
GB
Hi there again,

Can anyone supply me with the code and instructions to enable me to place a command button on a form that on clicking will print the two pages of a report that refer to the currently selected record in the form. The variuos components are names as follows.

The form is called "Playmakers Database"
The report is called "Playmakers Info Sheet"
The records are stored in a table also called "Playmakers Database". Each record is one row in the table.

Each record takes up two pages in the report.

The primary key for the table is called AddressID and simply runs from 1 to 60.

Please help if you can, many thanks indeed.
 
You can attach code simmilar to this to the button:
------------------------------------------------
Dim stDocName as String
Dim stLinkCriteria as String

stDocName = "Playmakers Info Sheet""
stLinkCriteria = "[AddressID] = " & Me.AddressID

DoCmd.OpenForm stDocName, , , stLinkCriteria
--------------------------------------------------

This should print the report for only the records where the address ID matches the one currently displayed in the form.
You can insert an 'acPreview' after the stDocName if you want to view the report on screen instead of printing it.
Though it will still depend on exactly how your report is written as to whether this gets you exaclty what you are looking for, this will limit the recordset the report runs off of to only the records where the address ID matches the one in the form when the button is pressed.

-Chris

 
Thanks for the code, unfortunately i couldn't get it to work. I got various errors. Firstly it reported that the Form Name "Playmakers Info Sheet" was misspelled or missing. which it wasnt. On changing various other elements and trying them it still wouldnt work. This is really bugging me (excuse the pun!1) I really need to get this to work. Am i right in thinking that upon inserting the button i then right click choose Build Event then Code Builder then Paste the code into the created entry point in VBA?? Am i supposed to change any of the code you posted or is it good to go as is??

I would be really grateful if you could offer any more help.

Thanks again.
 
Ooops! You are right. So sorry.
It's not a form, it's a report!
------------------------------------------------
Dim stDocName as String
Dim stLinkCriteria as String

stDocName = "Playmakers Info Sheet""
stLinkCriteria = "[AddressID] = " & Me.AddressID

DoCmd.OpenReport stDocName, , , stLinkCriteria
--------------------------------------------------

-Chris
 
Hi Chris,

Thanks yet again but still no joy i'm afraid!! On clicking the button in Form View having inserted the code and saved the form i get this exact Error message:

Run-time error '2103':

The report name 'Playmakers Info Sheet''' you entered in either the property sheet or macro is misspelled or refers to a report that doesn't exist.

It gives me the option to End or Debug or Use Help. But i cant seem to fathom where the problem is. There are no " " marks in the actual name of Playmakers Info Sheet so i assume you added them as part of the required syntax?? I have tried without these "" quotation marks but it still doesn't work.

Any other suggestions or ideas mate??

Cheers.

Stuart (Plymouth UK)
 
Hmmm. Well, I see I have an extra double quote at the end of the line where I set stDocName equal to my actual report name, so that might do it. Otherwise,my best guess at this point is that it's the spaces. Spaces in object names always seem to play havoc with VBA coding. Try renaming the report to 'rptPlaymakersInfoSheet'.
Once you have it renamed, copy the name from the object directly (As though you were renamomg it, highlight and ctrl+c)and pasting it into the quotes. So your new code looks like this:
------------------------------------

Dim stDocName as String
Dim stLinkCriteria as String

stDocName = "rptPlaymakersInfoSheet"
stLinkCriteria = "[AddressID] = " & Me.AddressID

DoCmd.OpenReport stDocName,acNormal , , stLinkCriteria
-----------------------------------------------------

Hope that helps.
Maybe I don't have any more typos either.

-Chris
 
You absolute star mate, it works perfectly!!

Many, many thanks.

All the best.

Stu
 
ChrisCalvert,

I have used the code here and when i click the print button it immediatly sent print. can you make it like first print options popups?

chao, xtravel


My Site:
 
xxtravel,

If you change the acNormal to acPreview that will bring up the report for viewing. However, if what you are looking for is the window that comes up when you use the file menu and select print, I don't know offhand how to make that come up. Perhaps if you know what options you want to set, myself or someone else can provide a VBA way to set those options before printing.

There may be a DoCmd menu command that does the File-->Print, but I am not familliar with it.

-Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top