Print Preview an Object

This example will accept the name of an object and display it in the Preview window.

'***************** Code Start *******************
'This code was originally written by Terry Wickenden.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.

Sub PreviewPrint(strName As String, Optional strItem As String = "Report")
'Expects the name of the item to be previewed = strName
'Also looks for type of object to be previewed
'Defaults to Report - other valid entries are Form, Table, Query, Module
'Note:- These are all case sensitive
  Dim intType As Integer
  Dim strMsg As String
  
  On Error GoTo ErrPreview
  Select Case strItem
    Case "Report"
      intType = acReport
    Case "Query"
      intType = acQuery
    Case "Form"
      intType = acForm
    Case "Table"
      intType = acTable
    Case "Module"
      intType = acModule
    Case Else
      MsgBox "Invalid object type", vbCritical, "Entry Error"
      Exit Sub
  End Select
  
  DoCmd.SelectObject intType, strName, True
  DoCmd.RunCommand acCmdPrintPreview
  Exit Sub
ErrPreview:
  Select Case Err
    Case 2544
    'Invalid object name
      strMsg = "There is no " & strItem & " called " & strName & "."
      MsgBox strMsg, vbCritical, "Entry Error"
      Exit Sub
    Case Else
      MsgBox Err & vbCrLf & vbCrLf & Err.Description, vbCritical, "Error Message"
      Exit Sub
  End Select
  
End Sub

'****************** Code End ********************

Return to Example List