Populating a combo box with the names of installed printers

  • Thread starter Thread starter Richard Krupa
  • Start date Start date
R

Richard Krupa

Hi all,

How can i populate a combo box with all installed system printers so i can
select which printer to use? I dont want have to choice which printer
everytime i print a document, rather i would like to have an application
setup option that allows me to select the document printer.

Hope people get what i mean

Regards,
Richard
 
In Access 2002 and later, you could read the names of the printers by
looping through the Printers collection.

If your combo's RowSourceType is Value List, you could then assign the
delimited string to the RowSource property, as in the function below.

For older versions, life is not so easy. See:
http://www.mvps.org/access/reports/rpt0009.htm

Function ShowPrinters()
Dim strPrinters As String
Dim lngLen As Long

Dim prn As Printer
For Each prn In Application.Printers
strPrinters = strPrinters & prn.DeviceName & ";"
Next

lngLen = Len(strPrinters) - 1 'without trailing ";".
If lngLen > 0 Then
strPrinters = Left(strPrinters, lngLen)
Forms!Form1!cboPrinter.RowSource = strPrinters
Else
MsgBox "No printers found."
End If

Set prn = Nothing
End Function
 
Thanks Allen,

Ive got it working so that i can retrieve the names of available printers
but how do i tell the report to print to the selected printer using code in
and MDE?

Regards,
Richard
 
In Access 97, the Printers collection is not available, so you have to use
the code in the linked articles.

From memory, I think that PrtMip etc do pose a problem with MDE/runtime, so
I'm not sure what else to suggest. Haven't tried it, but Peter Walker has a
link at his site that looks interesting:
http://www.papwalker.com/public/rptutil.zip
It gives you a sample A97 mdb, but I don't know if it works as an MDE.
 
Hey Allen,

So if i upgrade to A2K the Printers Collection will alow me to change a
reports printer on the fly using code in and mde?

At the monent sites have to stick to a printer naming code, ie their must b
a printer called "Reports", "Labels", "Statements" etc... and the reports
point to the relvant printer. What i want to do is have a settings file that
stores the name of the Reports printer, the name of the labels printer
etc... so that sites can name their printers anything they like and it does
not matter.

So if they select the Reports printer to be "Epson 700" and the Statement
printer to be "HP LaserJet 5MP" or whatever then any reports that use the
"reports" printer will print to the epson. Obviously printer names will
change from site to site hence why im after the extra flexability.

I hope you get what i mean

Regards,
Richard
 
You need to go to A2002 to get the Printers collection.

Then you can easily reassign the default printer for the Access application,
even in an MDE. (Just tested again to be certain.)

This example assumes a form with:
- a text box named txtOriginalPrinter (for storing the original printer),
- a combo box named cboPrinter.
Set the combo's RowSourceType to Value List.

Then paste in the code below into the form's module:
------------------------code starts-------------------------
Option Compare Database
Option Explicit

Private Sub cboPrinter_AfterUpdate()
Call AssignPrinter(Me.cboPrinter.Value)
End Sub

Private Sub Form_Close()
Call AssignPrinter(Me.txtOriginalPrinter)
End Sub

Private Sub Form_Open(Cancel As Integer)
Dim prn As Printer
Dim strPrinter As String

If Application.Printers.Count > 0 Then
'Create delimited list of printers for Value List of combo.
For Each prn In Application.Printers
strPrinter = strPrinter & """" & prn.DeviceName & """;"
Next
Me.cboPrinter.RowSource = Left(strPrinter, Len(strPrinter) - 1)

'Remember the original printer, and assign it as the current
default.
strPrinter = Application.Printer.DeviceName
Me.txtOriginalPrinter = strPrinter
Me.cboPrinter = strPrinter
Else
MsgBox "No printers found", vbInformation
End If

Set prn = Nothing
End Sub

Private Sub AssignPrinter(varPrinterName)
Dim prn As Printer

If Not IsNull(varPrinterName) Then
'Loop through the printers collection, and assign the one with
matching name.
For Each prn In Application.Printers
If prn.DeviceName = varPrinterName Then
Set Application.Printer = prn
Debug.Print "Printer set to " & varPrinterName
Exit For
End If
Next
If prn Is Nothing Then
MsgBox "Unable to find printer: " & varPrinterName
End If
End If

Set prn = Nothing
End Sub
-------------------------code ends--------------------------
 
Thanks for all your help Allen but we are Access 97 so i guess there is no
solution for me.

Regards,
Richard
 
Back
Top