By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,215 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,215 IT Pros & Developers. It's quick & easy.

Print report to specific printer from combobox containing installed printers

convexcube
P: 47
Hello Everyone,

I have a form setup with a combo box that displays a list of the installed printers using code from this article. On a button called cmdPrint I have placed code in the OnClick event from the same article to print to a specific printer. The specific printer being the one chosen in the combo box. In the line:
Expand|Select|Wrap|Line Numbers
  1. Set rpt.Printer = Application.Printers("Acrobat PFDWriter")
I have replaced "Acrobat PFDWriter" with the name of my combo box - "cbxPrinters" (without the quotes).
Clicking cmdPrint yields this error:
"Run-time error '5': Invalid Call or Argument"
When I type in the name of the printer it works.

I'd be grateful if someone (ADezii?) could help me nut this one out? Its fairly urgent as I am installing the finished product on Monday.

Kind Regards,
Ken Farrawell
Jan 31 '08 #1
Share this Question
Share on Google+
6 Replies


ADezii
Expert 5K+
P: 8,679
Hello Everyone,

I have a form setup with a combo box that displays a list of the installed printers using code from this article. On a button called cmdPrint I have placed code in the OnClick event from the same article to print to a specific printer. The specific printer being the one chosen in the combo box. In the line:
Expand|Select|Wrap|Line Numbers
  1. Set rpt.Printer = Application.Printers("Acrobat PFDWriter")
I have replaced "Acrobat PFDWriter" with the name of my combo box - "cbxPrinters" (without the quotes).
Clicking cmdPrint yields this error:
"Run-time error '5': Invalid Call or Argument"
When I type in the name of the printer it works.

I'd be grateful if someone (ADezii?) could help me nut this one out? Its fairly urgent as I am installing the finished product on Monday.

Kind Regards,
Ken Farrawell
Try this Syntax, and let me know how you make out:
Expand|Select|Wrap|Line Numbers
  1. Dim rpt As Report
  2.  
  3. If IsNull(Me![cbxPrinters]) Then Exit Sub
  4.  
  5. DoCmd.OpenReport "<Your Report Name>", View:=acViewPreview, WindowMode:=acHidden
  6.  
  7. Set rpt = Reports("<Your Report Name>")
  8. Set rpt.Printer = Application.Printers(Me![cbxPrinters])
  9.  
  10. DoCmd.OpenReport "<Your Report Name>", acViewNormal
  11. DoCmd.Close acReport, "<Your Report Name>"
Jan 31 '08 #2

convexcube
P: 47
Try this Syntax, and let me know how you make out:
Expand|Select|Wrap|Line Numbers
  1. Dim rpt As Report
  2.  
  3. If IsNull(Me![cbxPrinters]) Then Exit Sub
  4.  
  5. DoCmd.OpenReport "<Your Report Name>", View:=acViewPreview, WindowMode:=acHidden
  6.  
  7. Set rpt = Reports("<Your Report Name>")
  8. Set rpt.Printer = Application.Printers(Me![cbxPrinters])
  9.  
  10. DoCmd.OpenReport "<Your Report Name>", acViewNormal
  11. DoCmd.Close acReport, "<Your Report Name>"
Thanks for your response Adezii,

I have tried the code you suggested:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "rptComplaint", View:=acViewPreview, WindowMode:=acHidden
  2. Set rpt = Reports("rptComplaint")
  3. Set rpt.Printer = Application.Printers(Me![cbxPrinters])
  4. DoCmd.OpenReport "rptComplaint", acViewNormal
  5. DoCmd.Close acReport, "rptComplaint"
Unfortunately the result is the same error. Any other ideas?

Kind Regards,
Ken Farrawell
Jan 31 '08 #3

convexcube
P: 47
Hi Adezii,

I've got an update. I was able to avoid the error by using the Cstr function:
Expand|Select|Wrap|Line Numbers
  1. Set rpt.Printer = Application.Printers(CStr(Me![cbxPrinters]))
  2.  
Thanks for your efforts though.

Kind Regards,
Ken Farrawell
Jan 31 '08 #4

ADezii
Expert 5K+
P: 8,679
Hi Adezii,

I've got an update. I was able to avoid the error by using the Cstr function:
Expand|Select|Wrap|Line Numbers
  1. Set rpt.Printer = Application.Printers(CStr(Me![cbxPrinters]))
  2.  
Thanks for your efforts though.

Kind Regards,
Ken Farrawell
Nice fix, my next suggestion was something along the lines of:
Expand|Select|Wrap|Line Numbers
  1. Set rpt.Printer = Application.Printers("'" & Me![cbxPrinters] & "'")
Jan 31 '08 #5

convexcube
P: 47
Nice fix, my next suggestion was something along the lines of:
Expand|Select|Wrap|Line Numbers
  1. Set rpt.Printer = Application.Printers("'" & Me![cbxPrinters] & "'")
That was actually the first thing I tried, but it didn't work either & neither did:
Expand|Select|Wrap|Line Numbers
  1. Set rpt.Printer = Application.Printers(Chr(34) & Me![cbxPrinters] & Chr(34))
Kind Regards,
Ken Farrawell
Jan 31 '08 #6

ADezii
Expert 5K+
P: 8,679
That was actually the first thing I tried, but it didn't work either & neither did:
Expand|Select|Wrap|Line Numbers
  1. Set rpt.Printer = Application.Printers(Chr(34) & Me![cbxPrinters] & Chr(34))
Kind Regards,
Ken Farrawell
The only logical assumption is that it must be specifically Data Typed as String. A Variant (Sub Type of String) would be returned by Me![cbxPrinters], but I guess that isn't good enough.
Feb 1 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.