beginning og PowerPoint Slide Index Ms Access Gurus

If you are helped, please return the favor and help support this site, thank you.

Draw Shortcut Keys in Access

Create a report with shortcut keys for Access and VBA. Write text using the Print command. Draw rectangles using the Line command. Logic section with screenshots and explanations. VBA with comments. Reference links.

Use an Access report to draw shortcut keys

Quick Jump

Goto Top  


Download

Download database with tables and report: Draw_ShortCuts_s4p__ACCDB.zip

If you have trouble with the download, you may need to unblock the ZIP file, aka remove Mark of the Web, before extracting the BAS file. Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm

Goto Top  

Results

Open the report as a PDF:

Shortcuts_AccessVBA_s4p.pdf

Goto Top  

Logic

Relationships

Relationships diagram for shortcuts

Relationships diagram for shortcuts

Goto Top  

Shortcutz table

The Shortcutz table has a recognizable word for a key or key combinations. SID is the primary key, and a foreign key in the ShortApp and ShortKey tables. This is stuff that the report draws.

Datasheet View of the Shortcutz table

Goto Top  

Keyz table

The Keyz table has a list of keys usually found on a keyboard. The primary key is KeyID.

Datasheet View of the Keyz table

Goto Top  

ShortKey table

The ShortKey table is a cross reference between Shortcutz (SID) and Keyz (KeyID). It sets the order and also stores literal values when necessary.

Datasheet View of the ShortKey table

Goto Top  

Appz table

The Appz table lists applications.

Datasheet View of the Appz table

Goto Top  

Catz table

The Catz table lists categories.

Datasheet View of the Catz table

Goto Top  

ShortApp table

The ShortApp table is a cross-reference between shortcuts and applications. Access is AppID=1, and VBA is AppID=5. On the report, information is sorted by the description within a category if no other order is specified.

Datasheet View of the ShortApp table

Goto Top  

Design View of Report

Shortcut key report design view

Goto Top  

Query for report

The qShortcutz_Report query lines up data for the report

SELECT ShortAccess.SID
, ShortAccess.AppID
, ShortAccess.Descr1
, ShortAccess.Descr2
, ShortAccess.CatID
, Catz.Cat
, Catz.Ordr AS OrdrCat
, ShortAccess.Ordr AS OrdrShort
, Appz.AppName
FROM (
Catz 
RIGHT JOIN 
(SELECT SA.* FROM ShortApp SA WHERE SA.AppID=1 or SA.AppID=5)
  AS ShortAccess ON Catz.CatID = ShortAccess.CatID
) 
INNER JOIN Appz ON ShortAccess.AppID = Appz.AppID;

Goto Top  

VBA - ReportHeader_Format

Set module variables for properties such as font, draw width, spacing, and initial coordinates.

VBA - ReportDetail_Format

For each detail section, the format event opens a recordet with the keys and literal values for the shortcut.

If there is a key, the width of the text is calculated, spacing is added around the text, a box is drawn, and then the text is printed. If there is a literal, the text is printed without drawing any box.

The variable that keeps track of where to start writing horizontally again is mX1.

Goto Top  

VBA

This sets module variables in the ReportHeader_Format event. In the Detail_Format event, DrawShortcut is called

Option Compare Database 
Option Explicit 
'*************** Code Start *****************************************************
' code behind report: r_Shortcutz
'     fyi: detail Height set to 0.3 inches (432 twips)
'     -- code could change but currently does not
'        since report would take so much longer to render
'-------------------------------------------------------------------------------
' Purpose  : VBA to use PRINT to write text
'              and LINE to draw boxes
' Author   : crystal (strive4peace), 221024
' Code List: www.msaccessgurus.com/code.htm
' This code: https://msaccessgurus.com/VBA/Draw_Shortcuts.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Use at your own risk.
' NEEDS
'  QUERY: qShortcutz_Report
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           module variables
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim db As DAO.Database _ 
   ,rs As DAO.Recordset 
   
   'space is area between text and box
Dim mXSpace As Double _ 
   ,mYSpace As Double _ 
   ,mYHeightText As Double _ 
   ,mnColorText As Long _ 
   ,mnColorLine As Long 
   
   'mX1,mY1 is upper-left coordinate
   'mX2, mY2 is lower-right coordinate
   'mXWidth is width of text to draw at that moment
Dim msSQL As String _ 
   ,sText As String _ 
   ,mX1 As Double,mY1 As Double _ 
   ,mX2 As Double,mY2 As Double _ 
   ,mXWidth As Double 
   
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           Report_Close
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Report_Close() 
'221008 s4p release objects
   On Error Resume Next 
   If Not rs Is Nothing Then 
      rs.Close 
      Set rs = Nothing 
   End If 
   Set db = Nothing 
End Sub 

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           ReportHeader_Format
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub ReportHeader_Format(Cancel As Integer,FormatCount As Integer) 
'210808 s4p
   'set the report font/drawing properties
   'note: if you change, also adjust Me.Detail.Height
   '     currently = 0.3 inches = 432 twip
   
   With Me 
      .FontName =  "Arial"
      .FontSize =  "18"
      .FontItalic = False 
      .FontBold = True 
      
      .DrawWidth = 2  'px
      
      ' spacing between things written hozontally
      '  relative to FontSize
      mXSpace = .FontSize * 6 
      'font size is measured in points
      '  Point = 20 twip.
      ' Adjust HeightText to be shorter
      '  so report looks better to you
      '           -- CUSTOMIZE
      '     note: adjust ratio for personal taste
      mYHeightText = .FontSize * 20 * 0.75 
      
      ' Vertical spacing between box and text
      mYSpace = .FontSize 
      
      'although the physical position on the page changes,
      '  the relative position for each detail is constant
      ' set upper Y coordinate for box around text for Key
      mY1 = 48 
      
      'bottom Y coordinate for box
      mY2 = mY1 + mYHeightText + mYSpace 

   End With  'Me
   

   mnColorText = RGB(0,0,0)  'black
   mnColorLine = RGB(100,100,100)  'dark gray
End Sub 
   
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           Detail_Format
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Detail_Format(Cancel As Integer,FormatCount As Integer) 
'210808 s4p, 221008
   Call DrawShortcut(Me,Me.SID)  ', mXleft, mYtop)
End Sub 

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           DrawShortcut
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'use PRINT and LINE to draw shortcut keys
Sub DrawShortcut(pReport As Report _ 
   ,pnSID As Long _ 
   ) 
   
   If db Is Nothing Then 
      'reuse database object
      Set db = CurrentDb 
   End If 
   
   'get data for keys and literals for SID
   msSQL =  "SELECT K.KName, SK.Lit" _ 
      &  " FROM Keyz AS K " _ 
      &  " RIGHT JOIN ShortKey AS SK ON K.KeyID = SK.KeyID" _ 
      &  " WHERE(SK.SID  = " & pnSID &  ")" _ 
      &  " ORDER BY SK.Ordr;"

   'use dbOpenSnapshot so all records loaded first
   Set rs = db.OpenRecordset(msSQL,dbOpenSnapshot) 
   
   'initialize starting X
   mX1 = 72  '221023 s4p 0.05 * 1440
   
   '------------------------------------ Report
   With pReport 
   
      'loop through each record with key and/or literal
      Do While Not rs.EOF 

         '------------------ KEY
         sText = Nz(rs!KName, "") 
         If sText <>  "" Then 
            'width of text to write
            mXWidth = .TextWidth(sText) 
            
            'ending coordinate for box
            'mXSpace defined in Report Header
            'multiply by 2 for space before and after text
            mX2 = mX1 + mXWidth + mXSpace * 2 
            
            'draw box
            'Line (Step (x1, y1) - Step (mX2, mY2), Color, BF)
            pReport.Line (mX1,mY1)-(mX2,mY2),mnColorLine,B 
                        
            '----- text
            'add spacing between box and text
            'to starting positions
            .CurrentX = mX1 + mXSpace 
            .CurrentY = mY1 + mYSpace 
            .ForeColor = mnColorText 

            'write text
            .Print sText 
            
            'set mX1 position for next key or literal
            mX1 = mX2 + mXSpace / 2 

         End If 
      
         '------------------ LITERAL
         sText = Nz(rs!Lit, "") 
         If sText <>  "" Then 
            .CurrentX = mX1  '+ mXSpace / 2
            .CurrentY = mY1 + mYSpace / 2 
            .ForeColor = mnColorText 
            .Print sText 
         
            'set mX1 position for next key or literal
            mX1 = .CurrentX + mXSpace / 2 
          End If 
         'next record
         rs.MoveNext 
      Loop  'rs
   End With  'Me
   
   rs.Close 
   
End Sub 

'*************** Code End *******************************************************
' Code was generated with colors using the free Color Code add-in for Access.

Goto Top  

Reference

Drawing Reference on MsAccessGurus

Report Draw Reference for VBA syntax and help for drawing on Access reports.

Print method

Line method

CurrentX property

CurrentY property

TextWidth method

Goto Top  

Backstory

I love shortcut keys! It's nice be be able to draw them using an Access report. Not only is this a cool technique, but you get a handy shortcut key report too!

If you like this page, please let me know, thank you. Donations are much appreciated

Share with others

Here's the link for this page in case you want to copy it and share it with someone:

https://msaccessgurus.com/VBA/Draw_Shortcuts.htm

or in old browsers:
http://www.msaccessgurus.com/VBA/Draw_Shortcuts.htm

Get Help with Access and drawing

Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.

While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. I'll give you lots of links to good resources.

Do you want your reports to be more creative and visual? I'd love to help you. Email me at training@msAccessGurus.com

~ crystal

the simplest way is best, but usually the hardest to see

Goto Top