Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Changing printers in Access 97 using VBA

Status
Not open for further replies.

stevening

Computer
Nov 10, 2003
1
I'm stuck in my Access application for changing the printer for printing reports. I have found code for Access 2000, but this doesn't work in 97. Can anyone guide me how to change my printer in Access 97?

cheers
Steven
 
Replies continue below

Recommended for you

One way that I've done this is to change the default printer to the desired one before printing the report. I'm curious what code you found for A2K.

The following is some printer code that I've used, and I don't know if it will work in A97, but you're welcome to give it a shot.

One solution that you may want to try, and it's not that difficult, but you do need to include a reference to the Windows Script Host Object Model in your code.

One you setup your combobox, as type ValueList, execute the following from within the form to populate the combobox.
Code:
   Dim lStr_PrinterList   As String
   
   lStr_PrinterList = ListAllPrinters
   cboPrinters.RowSource = lStr_PrinterList
And in a module, add the ListAllPrinters function. You skip every other one because the even numbered entries have the Port names, and you don't have to include those.
Code:
Public Function ListAllPrinters() As String

   Dim lObj_ScriptControl        As IWshNetwork_Class
   Dim lCol_Printers             As IWshCollection_Class
   Dim lStr_PrinterList          As String
   Dim lInt_Idx                  As Integer
   
   Set lObj_ScriptControl = New IWshNetwork_Class
   Set lCol_Printers = lObj_ScriptControl.EnumPrinterConnections

   lStr_PrinterList = vbNullString
   For lInt_Idx = 1 To lCol_Printers.Count - 1 Step 2
      lStr_PrinterList = lStr_PrinterList & lCol_Printers.Item(lInt_Idx) & ";"
   Next lInt_Idx

   If (Right(lStr_PrinterList, 1) = ";") Then
      lStr_PrinterList = Left(lStr_PrinterList, Len(lStr_PrinterList) - 1)
   End If

   Set lObj_ScriptControl = Nothing
   Set lCol_Printers = Nothing
   
   ListAllPrinters = lStr_PrinterList

End Function
Then in the AfterUpdate event of the combobox add the following:
Code:
Private Sub cboPrinters_AfterUpdate()
   SetPrinterAsDefault cboPrinters.Text
End Sub
And in the module, add the SetPrinterAsDefault function
Code:
Public Sub SetPrinterAsDefault(rStr_PrinterName As String)

   Dim lObj_ScriptControl        As IWshNetwork_Class
   
   Set lObj_ScriptControl = New IWshNetwork_Class
   lObj_ScriptControl.SetDefaultPrinter rStr_PrinterName
   Set lObj_ScriptControl = Nothing

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor