Logicwurks Home Page

Links To Excel Code Examples

Tracing VBA Statements
Range/Wkb/Wks Variables
Add Grand Totals Using Ranges
Using Range Offset Property
Using Range Find Method
ConvertCellAddressToRange
Set Conditional Formatting
Union Of Ranges
Parse Range Strings
Delete Duplicate Rows
Delete Rows And Columns
Worksheet Variables
TypeName And TypeOf
Loop Through Worksheets
Loop Through Open Workbooks
Form Button Magic
Command Button Magic
Add Worksheets Dynamically
ImportExternalWorksheets
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Range Copy With Filter
ExcelFileOpenSaveClose
ExcelFileOpenSaveCSV
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Save A Workbook
Save A Workbook Using mso
Clone A Workbook
Test If WEB URL Exists
Parse Using Split Command
Using Classes in Excel
TypeStatementStructures
Color Management
Convert Cell Color To RGB
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Search Using Xlookup Function
Using Find Instead of Vlookup
Remove String Non-Printables
Auto_Open And Auto_Close
Initialize Form At Open
Edit Numerics In UserForm
Load Combo And List Boxes
Floating Sheet Combo Boxes
Advanced User Form Coding
Excel Events
Worksheet Change Events
Binary Search Of Array
Typecast Constants
Excel Error Handling
Handling Optional Parameters
Data Validation Drop Downs
Insert Data Validation Sub
Read A Text File w/Handle
Write A Text File w/Handle
Read a Binary File w/Handle
Update a Binary File w/Handle
Binary File Copy and Update
Read A Text Fiile w/Script
Text File Processing Examples
Test For Exists Or Open
Splash Screen
Dynamically Load Formulas
PaymentStreamsByDate
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Load Arrays Using Evaluate
ChartsAndGraphsVBA
Redim An Array
Reassign Button Action
Timer Functions
Legacy Calendar Control
Excel 2010 Date Picker
Date Picker Alternative
Generate Multiple Worksheets
Read Access Data Into Excel
Send Outlook Email w/Attach
Copy AutoFilters To Sheets
Export A Text File
Get Windows User Name
VBA Format Statement
Manipulate Files via VBA
Dynamically Load Images
Loop Through Worksheet Objects
Loop Through Form Objects
Loop Through Files with DIR
Active-X Checkboxes
Add Forms Checkboxes Dynam
Paste Pictures Into Excel
Copy Pictures Sheet To Sheet
Copy Pictures Sheet To Sheet
Create Forms Buttons With VBA
Extract Filename From Path
Convert R1C1 Format to A1
Special Cells Property
Insert Cell Comments

Links To Access Code Examples

DAO Versus ADODB
SearchVBACodeStrings
Interface Excel With Access
Create Form Manually
Create Recordset With AddNew
Multi-Select List Boxes
Update Field(s) In A Recordset
Update Excel Pivot From Access
Import A Tab Delimited File
Export Excel FileDialog
Create Excel Within Access
Open Excel Within Access
Open Excel OBJ From Access
Format Excel From Access
Control Excel via Access VBA
On Error Best Practices
Import Tab Delim w/WinAPI
Initialize Global Variables
Using TempVars For Globals
Access Error Handling
Loop Through Form Controls
Insert A Calendar Control
Create A Filtered Recordset
Populate Combo Boxes
Bookmarks And Forms
Combo Box Multiple Sources
Passing Form Objects
Create VBA SQL Statements
Create Dynamic Queries
Display File Images On A Form
Manipulate Files via VBA
Manipulate Files via Scripting
Number Subform Records
Reference Subform Objects
Parse Delimited Fields
Parameterized Queries (VBA)
Manipulating QueryDefs In VBA
FindFirst On Combined Keys
Dlookup Command
Dlookup In Form Datasheet
Execute SQL Delete Records
Commit Form To Table
Report With No Data
Reference Form Objects
DSNLess Connections To MySQL
Print Active Form Record
Count Records in Linked Tables
Delete Empty Tables
Open Linked SQL Tables

 

Print Active Form Record

When a print statement is issued on a FORM, all records in the Form's recordset will be printed. To only print the active record in a form requires special code to filter just the record(s) that you want to print. There are two methods to accomplish this: the Recordset and RecordsetClone properties. This page shows 3 examples of methods to accomplish this goal.

Program Code

Private Sub cmdPrintForm_Click1()
' *********************************************************************
' Print Only the Active Record In A Form
' *********************************************************************
Dim rst As DAO.Recordset
Dim lngClientContactID As Long
On Error GoTo cmdPrintClientContact_Click_Err

' *********************************************************************
' Save Primary Key of Current Record
' *********************************************************************
    lngClientContactID = Me.CCClientContactID

' *********************************************************************
' Filter To The One Record I Want To Print on the Form
' (The Current Active Record) - The Record Count on the Form
' Will Now Only Show 1 Record
' *********************************************************************
    Me.Detail.BackColor = 16777215  'White
    Me.Filter = "CCClientContactID = " & lngClientContactID
    Me.FilterOn = True

' *********************************************************************
' Print the One Filtered Form Record
' *********************************************************************
    DoCmd.SelectObject acForm, "frmClientContact", True
    DoCmd.RunCommand acCmdPrint
    DoCmd.SelectObject acForm, Screen.ActiveForm.Name, False
    
' *********************************************************************
' Turn Off the Filter And Show All Records in the Form
' The Record Count Goes Back To Normal
' However, The Position of the Record in the form changes to
' The First Record in the Dataset And Not The Record We Printed
' *********************************************************************
    Me.Detail.BackColor = 14151142  'Green
    Me.Filter = ""
    Me.FilterOn = False

' *********************************************************************
' Using The Form's Recordset, Locate The Printed Record So The
' Form Returns To Its Previous Position
' *********************************************************************
    Set rst = Me.Recordset
    rst.FindFirst "CCClientContactID = " & lngClientContactID
    If rst.NoMatch Then
        MsgBox "Record not found"
    End If

' *********************************************************************
' Note That I can't Close rst because the form will show all ????
' *********************************************************************
cmdPrintClientContact_Click_Exit:
    Exit Sub

cmdPrintClientContact_Click_Err:
    MsgBox Error$
    Resume cmdPrintClientContact_Click_Exit
End Sub



Private Sub cmdPrintForm_Click2()
' *********************************************************************
' Print Only the Active Record In A Form
' *********************************************************************
Dim rst As DAO.Recordset
Dim lngClientContactID As Long
Dim bkmBookmark As Variant

On Error GoTo cmdPrintClientContact_Click_Err

' *********************************************************************
' Using The Form's RecordsetClone, Locate The Printed Record So The
' Form Returns To Its Previous Position After The Filter is Removed
' *********************************************************************
    lngClientContactID = Me.CCClientContactID
    Set rst = Me.RecordsetClone
    rst.FindFirst "CCClientContactID = " & lngClientContactID
    If rst.NoMatch Then
        MsgBox "Record not found"
        rst.Close
        Set rst = Nothing
        Exit Sub
    End If
    bkmBookmark = rst.Bookmark
    
' *********************************************************************
' Save Primary Key of Current Record
' *********************************************************************
    lngClientContactID = Me.CCClientContactID

' *********************************************************************
' Filter To The One Record I Want To Print on the Form
' (The Current Active Record) - The Record Count on the Form
' Will Now Only Show 1 Record
' *********************************************************************
    Me.Detail.BackColor = 16777215  'White
    Me.Filter = "CCClientContactID = " & lngClientContactID
    Me.FilterOn = True

' *********************************************************************
' Print the One Filtered Form Record
' *********************************************************************
    DoCmd.SelectObject acForm, "frmClientContact", True
    DoCmd.RunCommand acCmdPrint
    DoCmd.SelectObject acForm, Screen.ActiveForm.Name, False
    
' *********************************************************************
' Turn Off the Filter And Show All Records in the Form
' The Record Count Goes Back To Normal
' However, The Position of the Record in the form changes to
' The First Record in the Dataset And Not The Record We Printed
' *********************************************************************
    Me.Detail.BackColor = 14151142  'Green
    Me.Filter = ""
    Me.FilterOn = False

' *********************************************************************
' Position the form record to its original active location
' *********************************************************************
    Me.Bookmark = bkmBookmark
    
' *********************************************************************
' Close The Recordset Clone
' *********************************************************************
    rst.Close
    Set rst = Nothing

cmdPrintClientContact_Click_Exit:
    Exit Sub

cmdPrintClientContact_Click_Err:
    MsgBox Error$
    Resume cmdPrintClientContact_Click_Exit
End Sub


Private Sub cmdPrintForm_Click3()
' *********************************************************************
' Print Only the Active Record In A Form
' *********************************************************************
Dim frm As Form
Dim rst As DAO.Recordset

On Error GoTo cmdPrintClientContact_Click_Err
    
' *********************************************************************
' Save Primary Key of Current Record
' *********************************************************************
    Set frm = Screen.ActiveForm
    lngClientContactID = Me.CCClientContactID

' *********************************************************************
' Filter To The One Record I Want To Print on the Form
' (The Current Active Record) - The Record Count on the Form
' Will Now Only Show 1 Record
' *********************************************************************
    frm.Detail.BackColor = 16777215  'White
    frm.Filter = "CCClientContactID = " & lngClientContactID
    frm.FilterOn = True
    
' *********************************************************************
' Print the One Filtered Form Record
' *********************************************************************
    DoCmd.PrintOut acSelection
    DoEvents 'Optional
' *********************************************************************
' Turn Off the Filter And Show All Records in the Form
' The Record Count Goes Back To Normal
' However, The Position of the Record in the form changes to
' The First Record in the Dataset And Not The Record We Printed
' *********************************************************************
    frm.Detail.BackColor = 14151142  'Green
    frm.Filter = ""
    frm.FilterOn = False

' *********************************************************************
' Using The Form's Recordset, Locate The Printed Record So The
' Form Returns To Its Previous Position
' *********************************************************************
    Set rst = Me.Recordset
    rst.FindFirst "CCClientContactID = " & lngClientContactID
    If rst.NoMatch Then
        MsgBox "Record not found"
    End If
' *********************************************************************
' Note That I can't Close rst because the form will show all ????
' *********************************************************************
cmdPrintClientContact_Click_Exit:
    Exit Sub

cmdPrintClientContact_Click_Err:
    MsgBox Error$
    Resume cmdPrintClientContact_Click_Exit
End Sub