Results 1 to 4 of 4

Thread: [RESOLVED] Printing in MS Access

  1. #1

    Thread Starter
    Junior Member OllieVB's Avatar
    Join Date
    Jan 2006
    Location
    England, UK
    Posts
    24

    Resolved [RESOLVED] Printing in MS Access

    Hi there,

    I've got a database that requires some reports to be sent to different printers. As this database is to be run on a couple of different systems with quite different printers installed, I figured the best way to do this was to use the common dialog box to set the printer prior to the report being printed - The proper way to do it! However, I'm new to common dialog boxes and although I can handle the open/save modes with ease, I don't know where to start with the printer mode and currently, althought the correct box is coming up, the report is still printing to the printer that was selected from the menu when I designed it.

    So I need to do the following:

    1. Select the printer from those available on the system
    2. Be able to set the number of copies
    3. Be able to select the quality, paper source, etc. as usual
    4. Disable the page selection (as the reports are preformatted as single pages)
    5. Print the report using the above settings (Didn't need to say that I suppose!)

    Here's my code so far:

    VB Code:
    1. Private Sub Print_profile_Click()
    2. 'On Error GoTo Exit_Print_profile_Click
    3.    
    4.     Dim stDocName As String
    5.    
    6.     'Save any changes made to the record prior to printing
    7.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    8.    
    9.     'Printer options
    10.     CMDialog4.Action = 5
    11.    
    12.     'WHAT DO I PUT HERE?
    13.    
    14.     stDocName = "Client Profile"
    15.     DoCmd.OpenReport stDocName, acNormal
    16.  
    17. Exit_Print_profile_Click:
    18. End Sub

    Can anyone help please?
    Last edited by OllieVB; Feb 5th, 2006 at 08:15 AM.
    Ollie
    Mess with the bull... You get the horns!
    ... Windows XP Home SP2
    ... Visual Basic 6 Professional Edition
    ... Office 2003 SP2 Professional Edition

    I will always leave a good response to helpful postings... They are a lifeline!

  2. #2
    Lively Member SonicBoomAu's Avatar
    Join Date
    Aug 2004
    Posts
    78

    Re: Printing in MS Access

    I found that access report printing is fun to say the least.

    This is what I do.

    VB Code:
    1. ' Need to Select the Printer
    2.         Dim ps As New Printing.PrinterSettings
    3.         PrintDialog1.PrinterSettings = ps
    4.         ' Capture the Original Printer
    5.         strDefaultPrinter = Me.PrintDialog1.PrinterSettings.PrinterName
    6.         ' Need to Select the Printer
    7.         PR = Me.PrintDialog1.ShowDialog()
    8.         ' Capture the required printer
    9.         strSelectedPrinter = Me.PrintDialog1.PrinterSettings.PrinterName


    Change the default printer to the required printer
    VB Code:
    1. ChangeDefaultPrinter(strSelectedPrinter)

    VB Code:
    1. Public Function ChangeDefaultPrinter(ByVal prtName As String) As Boolean
    2.  
    3.         Dim s As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
    4.         Dim m As Microsoft.Win32.RegistryKey
    5.  
    6.         m = s.OpenSubKey("Software")
    7.         m = m.OpenSubKey("Microsoft")
    8.         m = m.OpenSubKey("Windows NT")
    9.         m = m.OpenSubKey("CurrentVersion")
    10.         m = m.OpenSubKey("Windows", True)
    11.         m.SetValue("Device", prtName & ",winspool,FILE:")
    12.  
    13.         m.Flush()
    14.         m.Close()
    15.         s.Close()
    16.  
    17.  
    18.     End Function

    Print the Access Report
    VB Code:
    1. ' Start a new instance of Access for Automation:
    2.         Acc = New Access.ApplicationClass
    3.  
    4.         ' Open the Database in shared mode:
    5.         Acc.OpenCurrentDatabase(strDatabaseName, False)
    6.  
    7.         ' Select the report name in the database window and give focus
    8.         ' to the datbase window.
    9.         Acc.DoCmd.SelectObject(Access.AcObjectType.acReport, strReportName, True)
    10.  
    11.         'Print the report
    12.         Acc.DoCmd.OpenReport(strReportName, Access.AcView.acViewNormal)
    13.  
    14.         ' Quit Access and release object:
    15.         On Error Resume Next
    16.         Acc.Quit(Access.AcQuitOption.acQuitSaveNone)
    17.         System.Runtime.InteropServices.Marshal.ReleaseComObject(Acc)
    18.         Acc = Nothing

    Change the printer back to the default
    VB Code:
    1. ChangeDefaultPrinter(strDefaultPrinter)

    Hope this makes sense and helps.

  3. #3

    Thread Starter
    Junior Member OllieVB's Avatar
    Join Date
    Jan 2006
    Location
    England, UK
    Posts
    24

    Re: Printing in MS Access

    Hmmm. Not quite what I was expecting and it seems rather complicated but it may be that's the way it has to be done. I would have thought there was a simpler way to instruct access to direct the report to the selected printer.

    Anyone else have any ideas?
    Ollie
    Mess with the bull... You get the horns!
    ... Windows XP Home SP2
    ... Visual Basic 6 Professional Edition
    ... Office 2003 SP2 Professional Edition

    I will always leave a good response to helpful postings... They are a lifeline!

  4. #4

    Thread Starter
    Junior Member OllieVB's Avatar
    Join Date
    Jan 2006
    Location
    England, UK
    Posts
    24

    Cool Re: Printing in MS Access

    Finally got this one sorted, thanks to some valuable input from dannymking who gave me the idea and the command to look up in msdn. Here's my code (same in each report)...

    VB Code:
    1. Private Sub Report_Activate()
    2. On Error GoTo Error_Report
    3.  
    4.     Dim P As Boolean
    5.     P = [Forms]![Stationery]![Print?]
    6.  
    7.     If P Then
    8.         DoCmd.RunCommand acCmdPrint
    9.         DoCmd.Close
    10.     End If
    11.    
    12. Error_Report:
    13.     If P Then
    14.         DoCmd.Close
    15.     End If
    16. End Sub
    The reference setting P is just a simple checkbox that's hidden on the form carrying my print and preview buttons and set or cleared by whichever button is clicked. This method means that the system default is not altered and full printing control is given to just the report currently being processed. Hope this may help others.

    Thanks.
    Ollie
    Mess with the bull... You get the horns!
    ... Windows XP Home SP2
    ... Visual Basic 6 Professional Edition
    ... Office 2003 SP2 Professional Edition

    I will always leave a good response to helpful postings... They are a lifeline!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width