views:

47

answers:

3

Hi,

I am using this code to print a record from the form

Private Sub btnPrintRecord_Click()
On Error GoTo Err_btnPrintRecord_Click


    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.PrintOut acSelection

Exit_btnPrintRecord_Click:
    Exit Sub

Err_btnPrintRecord_Click:
    MsgBox Err.Description
    Resume Exit_btnPrintRecord_Click

End Sub

But, this code is not popping up print window to select the printer, it is automatically sending to default printer.

Can anyone help to pop up print window to select printer from that.

A: 

As stated in the documentation, DoCmd.PrintOut does not show the print dialog.

The first solution that comes to my mind is to use the SendKeys-function, send Ctrl+P and open the print dialog that way.

dwo
I am new to Ms access..Can you please include that function here...Thanks
Just type SendKeys into the VBA editor and press F1, then you will see that you have to use SendKeys "^p" !
dwo
SendKeys is almost never a good idea and it will not work on PCs running Vista.
Remou
Send Keys is working for me...thanks DWO
Remou,Then, can you suggest me another way other than sendkeys
@Remou: SendKeys works on Vista!
dwo
@dwo Try this search `vista sendkeys` in Google to see the number of complaints about how unreliable it is and how often it fails.
Remou
SendKeys is A TERRIBLE IDEA. Don't use it.
David-W-Fenton
I KNOW that SendKeys is "bad", but sometimes it's the only way.
dwo
I cannot think of a single case where SendKeys is the only way to accomplish something, unless you've defined "something" completely improperly. There are VBA objects available for manipulating the printer, and those are what should be used, with no need for SendKeys at all.
David-W-Fenton
Think of controlling something outside of Access, where no COM is available, etc. But for the case with the printer you're right.
dwo
I didn't know the context was not limited to Access itself. I'd say that I'd have real doubts putting such a thing into production use. I would probably want to eliminate any non-COM, non-scriptable component of an Access app and seek something better suited in terms of interfaces for interoperatibility with other applications.
David-W-Fenton
+1  A: 

DoMenuItem has been deprecated since at least Access 2000. You can use RunCommand to open the print window.

DoCmd.RunCommand acCmdPrint

It is almost never a good idea to print a record. It does not take long to build a report which can be used with a Where argument. This will give you much more control and give your users a much more pleasant experience.

Remou
We already have reports.. But engineers want a single record for checking errors.. for temporary reference
thanks for that line of code, it is working..perfect
In the print dialog they should select pdf and selected records options ..otherwise this prints whole DB
I tried using a PDF ActiveX control, to avoid this confusion but that control is not working
Can you help me out with this issue?!
You need a report. A single record can be printed to a report using the where argument.
Remou
Forms are not reports. And vice versa.
David-W-Fenton
A: 

Have you looked into the Printer object (it was introduced in A2002)? You can use it to get information about the printers and create your own dialog form to allow the user to pick the printer, then set it on the Printer object and print your report. I've never really used it, so can't give detailed instructions, but that's the proper way to handle this.

And, yes, this is probably harder than it seems it needs to be, but believe me, it's a helluva lot easier than it was before the introduction of the Printer object!

David-W-Fenton