Sei sulla pagina 1di 35

VBA Code Sample #62  An Access 2007 Add-in That

Automates Renaming Controls


Updated 31-Jan-2011
Supports Access 2007-2010
Helen Feddema (hfeddema@hvc.rr.com)

Note on Other Versions of this Add-in


For a version of this add-in that works in Access 2000 through 2003, see Code Sample
#64.

Introduction
Access add-ins come in three types:
 Wizard – a series of forms guiding you through creating a new object such as a form
or report
 Builder – A single form or dialog box that creates a property or expression
 Menu Add-in – A non-context-specific function that runs from the Tools|Add-ins
menu
This document describes the updated version of a set of Access add-ins I wrote to
automate the process of renaming database objects (tables, queries, forms, reports,
macros and modules) and also controls on forms and reports. The Access 2007/2010
version only does control renaming, because the Find And Replace add-in it calls for
object renaming doesn't work reliably in Access 2007 or 2010 (though if you wish to try
it, I have found that the LNC Rename.mda add-in works with FAR v. 9.0g-9.0i, though
you have to accept a warning message about the version when renaming database
objects).
The three types of add-ins (with several subtypes) are listed in Table 1. Note that the
nomenclature isn't too consistent—in real life, any kind of add-in might be called a
Wizard.
Table 1: The Add-in Types

Wizards Builders Menu Add-ins


Description Called when a new table, Lets you set properties in Not context-specific,
query, form, report, or control Design view called from the
is created Add-ins menu
Examples Table Wizards Property Builders
Query Wizards Expression Builders
Form Wizards
Report Wizards
Control Wizards

VB Script Code Sample #62 Page 1 of 35


Add-ins are stored in library databases, which are simply Access databases saved with
the .mda extension (or .accda for Access 2007 add-ins); a library that includes add-ins
also needs a special table, USysRegInfo.

USysRegInfo Table
The USysRegInfo table is a system table that stores information about add-ins which is
written to the Windows Registry when the add-ins are installed. This table has four
fields, containing the data described in Table 2:
Table 2: The USysRegInfo Table

Field Data Type Usage


Subkey Text (255) The name of the Registry subkey where a specific Registry
setting is stored; can be either
HKEY_CURRENT_ACCESS_PROFILE or
HKEY_LOCAL_MACHINE (plus the path to your Office
version – 9.0, 10.0 or 11.0)
Type Number (Long The type of entry to create; can be key (0), string (1) or
Integer) DWORD (4)
ValName Text (255) The name of the Registry value
Value Text (255) The value of the Registry value

The USysRegInfo table for the LNC Rename add-ins library database is shown in Table
3. This table contains several groups of rows, one group for each of the add-in
components (two property builders).
Table 3. USysRegInfo Table Rows for the LNC Control Renaming 2007 Add-in

Subkey Type ValName Value


HKEY_CURRENT_ACCESS_PROFILE\ 0
Wizards\Property Wizards\Name\LNC
Builder
HKEY_CURRENT_ACCESS_PROFILE\ 1 Description LNC Rename Current
Wizards\Property Wizards\Name\LNC Control
Builder
HKEY_CURRENT_ACCESS_PROFILE\ 4 Can Edit
Wizards\Property Wizards\Name\LNC
Builder
HKEY_CURRENT_ACCESS_PROFILE\ 1 Library |ACCDIR\LNC Control
Wizards\Property Wizards\Name\LNC Renaming 2007.accda
Builder
HKEY_CURRENT_ACCESS_PROFILE\ 1 Function LNCBuilder
Wizards\Property Wizards\Name\LNC
Builder
HKEY_CURRENT_ACCESS_PROFILE\ 0
Wizards\Property Wizards\Name\LNC
Builder All
HKEY_CURRENT_ACCESS_PROFILE\ 1 Description LNC Rename All Controls
Wizards\Property Wizards\Name\LNC
Builder All
HKEY_CURRENT_ACCESS_PROFILE\ 4 Can Edit
Wizards\Property Wizards\Name\LNC
Builder All

VB Script Code Sample #62 Page 2 of 35


HKEY_CURRENT_ACCESS_PROFILE\ 1 Library |ACCDIR\LNC Control
Wizards\Property Wizards\Name\LNC Renaming 2007.accda
Builder All
HKEY_CURRENT_ACCESS_PROFILE\ 1 Function LNCBuilderAll
Wizards\Property Wizards\Name\LNC
Builder All

Note: Although |ACCDIR is supposed to reference the folder in which the Access
executable is stored (Microsoft Office\Office12 for Access 2007 and Office14 for Access
2010), because at one time (long ago) that is where add-ins were stored, it still works
with add-ins stored in the Addins folder.
To install the add-ins, copy the files in the zip to the locations indicated in Table 4 below:
Table 4: File Locations

File Name Install to Folder


LNC Control Renaming Add-ins folder – usually C:\Users\User
2007.accda Name\AppData\Roaming\Microsoft\AddIns
Event Management.mdb Any folder (database with no naming convention, for testing)
Northwind.mdb Any folder (database with no naming convention, for testing)

Installing the Add-in


In previous versions of Access, installing an add-in was a simple matter: all you had to
do was to put the add-in file in the Addins folder, and install it using the Addins
Manager. With Access 2007 (or 2010) and Windows Vista or Windows 7, the process is
more complicated, because of increased security. The first step is to delete or move to
another folder any other version of this add-in or the full LNC Rename add-in you may
have in the Addins folder (this is necessary because otherwise there will be errors
because of procedures in the different add-in versions that have the same names). Next,
place the LNC Control Renaming 2007.accda file in your Addins folder, usually
C:\Users\Your Name\AppData\Roaming\Microsoft\AddIns.
The following steps are different depending on the OS and Access version.

Windows Vista/Access 2007


Open an Explorer window, locate the MSACCESS.EXE file, right-click it, and select Run
as Administrator:

VB Script Code Sample #62 Page 3 of 35


When the UAC dialog appears, click Continue.
With Access 2007 running in Administrator mode, there are two ways to install an
Access add-in. The first method (which is both lengthy and complicated) starts with
clicking the large Office button in the upper-left corner of the Access window. I will skip
this method and go right to the quick and easy alternative.
Open any Access database, select the Database Tools Ribbon, and click the Add-ins item
in the Database Tools group, as shown in the next figure:

VB Script Code Sample #62 Page 4 of 35


Select the Add-In Manager item to open the Add-in Manager. The Access 2007/Vista
Add-in Manager is shown below. If you see the LNC add-in in the list of addins, back
out and delete it from the Addins folder, or move it to another folder, then start the
installation process again. (This is necessary since the older add-in contains functions
with the same name as those the new add-in.) The LNC Control Renaming 2007 add-in
should appear in the list; select it and click Install to install it.

VB Script Code Sample #62 Page 5 of 35


Windows 7/Access 2010
Open an Explorer window, locate the MSACCESS.EXE file, right-click it, and select Run
as Administrator:

When the UAC dialog appears, click Continue.


With Access 2010 running in Administrator mode, there are two ways to install an
Access add-in. The first method (which is both lengthy and complicated) starts with
selecting the File menu. I will skip this method and go right to the quick and easy
alternative. Start by opening any Access database, then selecting the Database Tools
Ribbon, and clicking the Add-ins item in the Add-ins group, as shown in the next figure:

VB Script Code Sample #62 Page 6 of 35


The Add-in Manager is listed in the drop-down list; select it and the Add-ins Manager
opens (much the same as in earlier versions of Access), and you can install the LNC
Control Renaming 2007 by selecting it in the list of add-ins and clicking Install.

VB Script Code Sample #62 Page 7 of 35


LNC Rename Property Wizards
LNC Control Renaming 2007 has two Property Wizards that let you rename an
individual control, or all controls on the currently open form or report. For databases
that have the LNC applied already, these property builders are all you need to ensure
that any new objects and controls are given the appropriate LNC tags.
To rename an individual control (perhaps one newly added to a form), select the control
in design view, open its properties sheet, and click the Build button beside the Name
property, as shown below.

The Choose Builder dialog opens, as shown below, where you can see two LNC Rename
Property Wizards (and perhaps others you may have installed).

Note: The Smart Rename builder is installed when you install FAR; since isn't needed, I
recommend deleting its key from the Registry, as shown below

VB Script Code Sample #62 Page 8 of 35


After deleting the FARNameBuilder key, you won't see the Smart Rename (Find and
Replace) item in the Choose Builder dialog.
Select the LNC Rename Current Control builder to automatically rename the current
control with the appropriate LNC tag; you can edit the new name as desired in the
property sheet, if needed. If the Tag property is empty, the old control name will be
automatically saved to that property.

VB Script Code Sample #62 Page 9 of 35


To rename all the controls on a form that is open in design view, select the LNC Rename
All Controls selection in the Choose Builder dialog. You will get two confirmation
dialogs, allowing you to rename all the controls on the form either automatically or with
a confirmation for renaming each control.

Troubleshooting

AutoCorrect
Access 2000 added an AutoCorrect feature that many developers hoped would make it
easier to rename database objects, but unfortunately this feature has several serious
flaws, which make it basically unworkable. Basically, AutoCorrect fixes some (but not
all) references to renamed objects, and none of the references in VBA code, which makes
it pretty much worthless, since you will have to use a utility such as FAR to correct the
missed references. It's easier to just use FAR directly.
Additionally, AutoCorrect has a very annoying feature of "correcting" references that
should not be corrected. For example, if you are working on the design of a form called
frmOrders, and you make a backup copy of the form, called –frmOrders1, you will find
that references to frmOrders (say, in a query criterion) may be changed to –frmOrders1,
resulting in errors.
I recommend turning off AutoCorrect in the General tab of the Access Options dialog
and using LNC Rename and/or FAR to rename database objects as needed.

Debugging
If you have selected the Break on All Errors option in the Options dialog for modules,
you will get numerous error messages. Instead, select Break on Unhandled Errors so the
add-ins error handing will work correctly:

VB Script Code Sample #62 Page 10 of 35


Code
basLNC contains the add-in's code:
Option Compare Database
Option Explicit

Public pctl As Access.Control


Public pfrm As Access.Form
Public pfrmSub As Access.Form
Public prpt As Access.Report

Private blnPrompt As Boolean


Private blnRenameFail As Boolean
Private blnTag As Boolean
Private blnUnbound As Boolean
Private dbsCalling As DAO.Database
Private dbsCode As DAO.Database
Private i As Integer
Private intChoice As Integer
Private intCount As Integer
Private intReturn As Integer
Private lngControlType As Long
Private rst As DAO.Recordset
Private rstSource As DAO.Recordset
Private rstTarget As DAO.Recordset
Private strCallingDb As String
Private strCaption As String
Private strCodeDB As String
Private strControlName As String
Private strControlSource As String
Private strDBName As String
Private strForm As String
Private strFormName As String
Private strNewCtlName As String
Private strNewName As String
Private strObjectsTable As String
Private strObjectType As String
Private strOldCtlName As String
Private strOriginalName As String
Private strPrefix As String
Private strPrompt As String
Private strProposedNewCtlName As String
Private qdf As DAO.QueryDef
Private strQuery As String
Private strReportName As String
Private strSourceObject As String
Private strSQL As String
Private strSubform As String
Private strSystemTable As String
Private strTable As String
Private strTitle As String
Private tdf As DAO.TableDef
Private tdfs As DAO.TableDefs

Public Function LNCBuilder(strObjectName As String, _


strCtlName As String, strCurrentValue As String) As Variant
'Called from USysRegInfo (property wizard)
'A Property Builder that renames an individual control
'on a form or report
'Written by Helen Feddema 4-13-98
'Last modified 30-Sep-2007

VB Script Code Sample #62 Page 11 of 35


On Error GoTo ErrorHandler

strOldCtlName = strCurrentValue
If strOldCtlName = "Detail" Then
strPrompt = "Please select a control to rename"
MsgBox strPrompt, vbExclamation
GoTo ErrorHandlerExit
End If

'Determine whether current object is a form or report


For Each pfrm In Forms
If pfrm.Name = strObjectName Then
GoTo ObjectIsForm
End If
Next pfrm

'strObjectName not found in forms; check whether it is a report


For Each prpt In Reports
If prpt.Name = strObjectName Then
GoTo ObjectIsReport
End If
Next prpt

MsgBox "Current object is not a form or a report; exiting"


GoTo ErrorHandlerExit

ObjectIsForm:
Set pfrm = Forms(strObjectName)
Set pctl = pfrm(strCtlName)
GoTo DetermineControlType

ObjectIsReport:
Set prpt = Reports(strObjectName)
Set pctl = prpt(strCtlName)

DetermineControlType:
lngControlType = pctl.ControlType

'Determine whether the Tag property has a value


strPrompt = "Save the original control name to the control's Tag property?"

'Set blnTag to save control name to Tag only if Tag is empty


If IsNull(pctl.Tag) = True Or pctl.Tag = "" Then
blnTag = True
Else
blnTag = False
End If

'Run code to create suggested control name based on control type


blnUnbound = False

Select Case lngControlType


'Control with control source
Case acTextBox
strPrefix = "txt"
strNewCtlName = ControlCS(pctl, strPrefix, blnTag, False)

Case acComboBox
strPrefix = "cbo"
strNewCtlName = ControlCS(pctl, strPrefix, blnTag, False)

VB Script Code Sample #62 Page 12 of 35


Case acCheckBox
strPrefix = "chk"
strControlSource = pctl.ControlSource
If blnUnbound = False Then
strNewCtlName = ControlCS(pctl, strPrefix, blnTag, False)
Else
strNewCtlName = ControlNA(pctl, strPrefix, blnTag, False)
End If

Case acBoundObjectFrame
strPrefix = "frb"
strNewCtlName = ControlCS(pctl, strPrefix, blnTag, False)

Case acListBox
strPrefix = "lst"
strNewCtlName = ControlCS(pctl, strPrefix, blnTag, False)

Case acAttachment
strPrefix = "att"
Call ControlCS(pctl, strPrefix, blnTag, False)

Case acOptionGroup
strPrefix = "fra"
strNewCtlName = ControlCS(pctl, strPrefix, blnTag, False)

Case acOptionButton
strPrefix = "opt"
strControlSource = pctl.ControlSource
If blnUnbound = False Then
strNewCtlName = ControlCS(pctl, strPrefix, blnTag, False)
Else
strNewCtlName = ControlNA(pctl, strPrefix, blnTag, False)
End If

'Control with caption only


Case acToggleButton
strPrefix = "tgl"
strNewCtlName = ControlCA(pctl, strPrefix, blnTag, False)

Case acLabel
strPrefix = "lbl"
strNewCtlName = ControlCA(pctl, strPrefix, blnTag, False)

Case acCommandButton
strPrefix = "cmd"
strNewCtlName = ControlCA(pctl, strPrefix, blnTag, False)

Case acPage
strPrefix = "pge"
strNewCtlName = ControlCA(pctl, strPrefix, blnTag, False)

'Control with source object only


Case acSubform
strPrefix = "sub"
strNewCtlName = ControlSO(pctl, strPrefix, blnTag, False)

'Control with none of the above


Case acObjectFrame
strPrefix = "fru"
strNewCtlName = ControlNA(pctl, strPrefix, blnTag, False)

Case acImage
strPrefix = "img"

VB Script Code Sample #62 Page 13 of 35


strNewCtlName = ControlNA(pctl, strPrefix, blnTag, False)

Case acTabCtl
strPrefix = "tab"
strNewCtlName = ControlNA(pctl, strPrefix, blnTag, False)

Case acLine
strPrefix = "lin"
strNewCtlName = ControlNA(pctl, strPrefix, blnTag, False)

Case acPageBreak
strPrefix = "brk"
strNewCtlName = ControlNA(pctl, strPrefix, blnTag, False)

Case acRectangle
strPrefix = "shp"
strNewCtlName = ControlNA(pctl, strPrefix, blnTag, False)

End Select

ErrorHandlerExit:
Exit Function

ErrorHandler:
'If an option button or checkbox is unbound, set
'blnUnbound to True so the code uses the NA function instead of CS
Select Case Err.Number
Case 2455
blnUnbound = True
Resume Next
Case Else
MsgBox "Error No: " & Err.Number _
& " in LNCBuilder procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Select

End Function

Public Function LNCBuilderAll(strObjectName As String, _


strCtlName As String, strCurrentValue As String) As Variant
'Called from USysRegInfo (property wizard)
'A Property Builder that renames all the controls on a form
'or report
'Written by Helen Feddema 4-15-98
'Last modified 29-Jul-2007

On Error GoTo ErrorHandler

strOldCtlName = strCurrentValue

'Determine whether original control names should be stored in Tag property


strTitle = "Control Name Backup"
strPrompt = "When processing controls, should the original control " _
& "name be saved to the control's Tag property?"
intReturn = MsgBox(strPrompt, vbYesNo + vbQuestion + _
vbDefaultButton2, strTitle)

If intReturn = vbYes Then


blnTag = True
ElseIf intReturn = vbNo Then
blnTag = False

VB Script Code Sample #62 Page 14 of 35


End If

'Determine whether there should be a prompt for each control


strTitle = "Control Name Prompting"
strPrompt = "When processing controls, should there be a prompt " _
& "to confirm each proposed new control name?"
intReturn = MsgBox(strPrompt, vbYesNo + vbQuestion + _
vbDefaultButton1, strTitle)

If intReturn = vbYes Then


blnPrompt = True
ElseIf intReturn = vbNo Then
blnPrompt = False
End If

'Determine whether current object is a form or report


For Each pfrm In Forms
If pfrm.Name = strObjectName Then
GoTo ObjectIsForm
End If
Next pfrm

'strObjectName not found in forms; check whether it is a report


For Each prpt In Reports
If prpt.Name = strObjectName Then
GoTo ObjectIsReport
End If
Next prpt

MsgBox "Current object is not a form or a report; exiting"


GoTo ErrorHandlerExit

ObjectIsForm:
Set pfrm = Forms(strObjectName)
For Each pctl In pfrm.Controls
strCtlName = pctl.Name
lngControlType = pctl.ControlType
blnUnbound = False

Select Case lngControlType


'Controls with control source
Case acTextBox
strPrefix = "txt"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acComboBox
strPrefix = "cbo"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acCheckBox
strPrefix = "chk"
strControlSource = pctl.ControlSource
If blnUnbound = False Then
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)
Else
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)
End If

Case acBoundObjectFrame
strPrefix = "frb"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acListBox

VB Script Code Sample #62 Page 15 of 35


strPrefix = "lst"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acOptionGroup
strPrefix = "fra"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acAttachment
strPrefix = "att"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acOptionButton
strPrefix = "opt"
strControlSource = pctl.ControlSource
If blnUnbound = False Then
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)
Else
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)
End If

'Controls with caption only


Case acToggleButton
strPrefix = "tgl"
Call ControlCA(pctl, strPrefix, blnTag, blnPrompt)

Case acLabel
strPrefix = "lbl"
Call ControlCA(pctl, strPrefix, blnTag, blnPrompt)

Case acCommandButton
strPrefix = "cmd"
Call ControlCA(pctl, strPrefix, blnTag, blnPrompt)

Case acPage
strPrefix = "pge"
Call ControlCA(pctl, strPrefix, blnTag, blnPrompt)

'Controls with source object only


Case acSubform
strPrefix = "sub"
Call ControlSO(pctl, strPrefix, blnTag, blnPrompt)

'Controls with none of the above


Case acObjectFrame
strPrefix = "fru"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acImage
strPrefix = "img"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acTabCtl
strPrefix = "tab"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acLine
strPrefix = "lin"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acPageBreak
strPrefix = "brk"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

VB Script Code Sample #62 Page 16 of 35


Case acRectangle
strPrefix = "shp"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

End Select

SkipToHereForm:
Next pctl
GoTo Success

ObjectIsReport:
Set prpt = Reports(strObjectName)
For Each pctl In prpt.Controls
strCtlName = pctl.Name
lngControlType = pctl.ControlType
blnUnbound = False

Select Case lngControlType


'Controls with control source
Case acTextBox
strPrefix = "txt"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acComboBox
strPrefix = "cbo"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acCheckBox
strPrefix = "chk"
strControlSource = pctl.ControlSource
If blnUnbound = False Then
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)
Else
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)
End If

Case acBoundObjectFrame
strPrefix = "frb"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acListBox
strPrefix = "lst"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acOptionGroup
strPrefix = "fra"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acAttachment
strPrefix = "att"
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)

Case acOptionButton
strPrefix = "opt"
strControlSource = pctl.ControlSource
If blnUnbound = False Then
Call ControlCS(pctl, strPrefix, blnTag, blnPrompt)
Else
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)
End If

'Controls with caption only


Case acToggleButton

VB Script Code Sample #62 Page 17 of 35


strPrefix = "tgl"
Call ControlCA(pctl, strPrefix, blnTag, blnPrompt)

Case acLabel
strPrefix = "lbl"
Call ControlCA(pctl, strPrefix, blnTag, blnPrompt)

Case acCommandButton
strPrefix = "cmd"
Call ControlCA(pctl, strPrefix, blnTag, blnPrompt)

'Controls with source object only


Case acSubform
strPrefix = "sub"
Call ControlSO(pctl, strPrefix, blnTag, blnPrompt)

'Controls with none of the above


Case acObjectFrame
strPrefix = "fru"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acImage
strPrefix = "img"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acTabCtl
strPrefix = "tab"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acLine
strPrefix = "lin"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acPage
strPrefix = "pge"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acPageBreak
strPrefix = "brk"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

Case acRectangle
strPrefix = "shp"
Call ControlNA(pctl, strPrefix, blnTag, blnPrompt)

End Select

SkipToHereReport:
Next pctl

Success:
MsgBox "All controls renamed!"

ErrorHandlerExit:
Exit Function

ErrorHandler:
'If an option button or checkbox is unbound, set
'blnUnbound to True so the code uses the NA function instead of CS
If Err.Number = 2455 Then
blnUnbound = True
Resume Next
Else

VB Script Code Sample #62 Page 18 of 35


MsgBox "Error No: " & Err.Number _
& " in LNCBuilderAll procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End If

End Function

Public Function StripNonAlphaNumericChars(strText As String) As String


'Strips a variety of non-alphanumeric characters from a text string
'Created by Helen Feddema 10-15-97
'Modified by Ruud H.G. van Tol 6-18-99
'Last modified 22-Jul-2007

On Error GoTo ErrorHandler

Dim strTestString As String


Dim strTestChar As String
Dim lngFound As Long
Dim i As Integer
Dim strStripChars As String

strStripChars = " `~!@#$%^&*()-_=+[{]};:',<.>/?" & Chr$(34) & Chr$(13) &


Chr$(10)
strTestString = strText

i = 1
Do While i <= Len(strTestString)
'Find a strippable character
strTestChar = Mid$(strTestString, i, 1)
lngFound = InStr(strStripChars, strTestChar)
If lngFound > 0 Then
strTestString = Left(strTestString, i - 1) & Mid(strTestString, i + 1)
Else
i = i + 1
End If
Loop

StripNonAlphaNumericChars = strTestString

ErrorHandlerExit:
Exit Function

ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in StripNonAlphaNumericChars procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit

End Function

Public Function ControlCS(ctlSelected As Control, _


strPrefix As String, blnTag As Boolean, _
blnPrompt As Boolean) As Integer
'Called from LNC Rename and LNC Rename All property builders
'Does group renaming of all controls with control sources
'on a form or report
'Created by Helen Feddema 10-15-97
'Last modified 21-Jan-2011

On Error GoTo ErrorHandler

VB Script Code Sample #62 Page 19 of 35


Dim strControlSource As String

strControlSource = Nz(ctlSelected.ControlSource)
strOldCtlName = ctlSelected.ControlName
lngControlType = ctlSelected.ControlType

'Check whether control already is correctly named and also special case
'for controls whose original name starts with "Option" or "Frame"
'(same first three letters as prefix)
If Left(strOldCtlName, 3) = strPrefix And _
Left(strOldCtlName, 6) <> "Option" And _
Left(strOldCtlName, 3) = strPrefix And _
Left(strOldCtlName, 5) <> "Frame" Then
GoTo ErrorHandlerExit

'If the control source is not empty, use it


ElseIf strControlSource <> "" Then
strNewCtlName = strPrefix & _
StripNonAlphaNumericChars(strControlSource)
'Otherwise, use the original control name
Else
strNewCtlName = strPrefix & _
StripNonAlphaNumericChars(strOldCtlName)
End If

'Fix name of "Page x of y" textbox controls on Database Wizard reports


If strNewCtlName = "txtPagePageofPages" Then
strNewCtlName = "txtPages"
End If

AutoRename:
If blnPrompt = True Then
GoTo TestNewName
ElseIf blnPrompt = False Then
If blnTag = True Then
ctlSelected.Tag = ctlSelected.ControlName
End If
ctlSelected.ControlName = strNewCtlName
GoTo ErrorHandlerExit
End If

TestNewName:
'Show the user
' - the original control name
' - the control type
' - control source
' - proposed new name
'and ask if the new name is acceptable.
blnRenameFail = True
Do While blnRenameFail
blnRenameFail = False
strTitle = "Rename control"
strPrompt = "Rename " & ControlTypeName(lngControlType) _
& " control currently named " _
& strOldCtlName & vbCrLf & _
"(control source: " & strControlSource & ") " & _
"to" & vbCrLf & strNewCtlName & "?"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbYesNoCancel + vbQuestion + vbDefaultButton1, _
Title:=strTitle)

'If the user clicks the Yes button, rename the control

VB Script Code Sample #62 Page 20 of 35


If intReturn = vbYes Then
If blnTag = True Then
ctlSelected.Tag = ctlSelected.ControlName
End If
ctlSelected.ControlName = strNewCtlName
ElseIf intReturn = vbNo Then
UserInputName:
'Pop up an input box to edit the name.
strProposedNewCtlName = _
StripNonAlphaNumericChars(InputBox("Modify new control name", _
"Rename control", strNewCtlName))
'Test whether proposed control name is blank, or just
'non-permitted characters
If strProposedNewCtlName = "" Then
'User canceled
GoTo ErrorHandlerExit
Else
strNewCtlName = strProposedNewCtlName
ctlSelected.ControlName = strNewCtlName
GoTo TestNewName
End If
ElseIf intReturn = vbCancel Then
GoTo ErrorHandlerExit
End If
Loop

ErrorHandlerExit:
Exit Function

ErrorHandler:
'If the proposed control name is already in use,
'return to the renaming dialog.
blnRenameFail = True
If Err.Number = 2104 Then
If blnPrompt = True Then
strTitle = "Duplicate Control Name"
strPrompt = "There is another control named " & _
strNewCtlName & "; please try again"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbExclamation + vbOKCancel, _
Title:=strTitle)
If intReturn = vbOK Then
strNewCtlName = strNewCtlName & "1"
ElseIf intReturn = vbCancel Then
GoTo TestNewName
End If
ElseIf blnPrompt = False Then
strNewCtlName = strNewCtlName + CStr(Int((20 - 1) * Rnd + 1))
GoTo AutoRename
End If
ElseIf Err.Number = 2123 Then
strTitle = "Unacceptable Control Name"
strPrompt = "Please enter an acceptable control name"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbExclamation + vbOKOnly, _
Title:=strTitle)
GoTo TestNewName
Else
MsgBox "Error No: " & Err.Number _
& " in ControlCS procedure; " _
& "Description: " & Err.Description
End If

VB Script Code Sample #62 Page 21 of 35


Resume Next

End Function

Public Function ControlCA(ctlSelected As Control, _


strPrefix As String, blnTag As Boolean, _
blnPrompt As Boolean) As Integer
'Called from LNC Rename and LNC Rename All property builders
'Does group renaming of all controls with captions on a form or report
'Created by Helen Feddema 10-15-97
'Last modified 21-Jan-2011

On Error GoTo ErrorHandler

Dim strCaption As String

strOldCtlName = ctlSelected.ControlName
strCaption = ctlSelected.Caption
lngControlType = ctlSelected.ControlType

If Left(strOldCtlName, 3) = strPrefix Then


GoTo ErrorHandlerExit
ElseIf strCaption <> "" Then
If Left(strCaption, 3) = "pfrm" Then
strNewCtlName = strPrefix & _
Mid(StripNonAlphaNumericChars(strCaption), 4)
ElseIf Left(strCaption, 4) = "fsub" Then
strNewCtlName = strPrefix & _
Mid(StripNonAlphaNumericChars(strCaption), 5)
Else
strNewCtlName = strPrefix & _
StripNonAlphaNumericChars(strCaption)
End If
ElseIf strCaption = "" Then
If Left(strOldCtlName, 3) = "pfrm" Then
strNewCtlName = strPrefix & _
Mid(StripNonAlphaNumericChars(strOldCtlName), 4)
ElseIf Left(strOldCtlName, 4) = "fsub" Then
strNewCtlName = strPrefix & _
Mid(StripNonAlphaNumericChars(strOldCtlName), 5)
Else
strNewCtlName = strPrefix & _
StripNonAlphaNumericChars(strOldCtlName)
End If
End If

If Right(strNewCtlName, 12) = "SubformLabel" Then


strNewCtlName = Left(strNewCtlName, Len(strNewCtlName) - 12)
ElseIf Right(strNewCtlName, 5) = "Label" Then
strNewCtlName = Left(strNewCtlName, Len(strNewCtlName) - 5)
End If

AutoRename:
If blnPrompt = True Then
GoTo TestNewName
ElseIf blnPrompt = False Then
If blnTag = True Then
ctlSelected.Tag = ctlSelected.ControlName
End If
ctlSelected.ControlName = strNewCtlName
GoTo ErrorHandlerExit
End If

VB Script Code Sample #62 Page 22 of 35


TestNewName:
'Show the user
' - the original control name
' - the control type
' - control source
' - proposed new name
'and ask if the new name is acceptable.
blnRenameFail = True
Do While blnRenameFail
blnRenameFail = False
strTitle = "Rename control"
strPrompt = "Rename " & ControlTypeName(lngControlType) _
& " control currently named " _
& strOldCtlName & vbCrLf & _
"(control source: " & strControlSource & ") " & _
"to" & vbCrLf & strNewCtlName & "?"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbYesNoCancel + vbQuestion + vbDefaultButton1, _
Title:=strTitle)

'If the user clicks the Yes button, rename the control
If intReturn = vbYes Then
If blnTag = True Then
ctlSelected.Tag = ctlSelected.ControlName
End If
ctlSelected.ControlName = strNewCtlName
ElseIf intReturn = vbNo Then
UserInputName:
'Pop up an input box to edit the name.
strProposedNewCtlName = _
StripNonAlphaNumericChars(InputBox("Modify new control name", _
"Rename control", strNewCtlName))
'Test whether proposed control name is blank, or just
'non-permitted characters
If strProposedNewCtlName = "" Then
'User canceled
GoTo ErrorHandlerExit
Else
strNewCtlName = strProposedNewCtlName
ctlSelected.ControlName = strNewCtlName
GoTo TestNewName
End If
ElseIf intReturn = vbCancel Then
GoTo ErrorHandlerExit
End If
Loop

ErrorHandlerExit:
Exit Function

ErrorHandler:
'If the proposed control name is already in use,
'return to the renaming dialog.
blnRenameFail = True
If Err.Number = 2104 Then
If blnPrompt = True Then
strTitle = "Duplicate Control Name"
strPrompt = "There is another control named " & _
strNewCtlName & "; please try again"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbExclamation + vbOKCancel, _

VB Script Code Sample #62 Page 23 of 35


Title:=strTitle)
If intReturn = vbOK Then
strNewCtlName = strNewCtlName & "1"
ElseIf intReturn = vbCancel Then
GoTo TestNewName
End If
ElseIf blnPrompt = False Then
strNewCtlName = strNewCtlName + CStr(Int((20 - 1) * Rnd + 1))
GoTo AutoRename
End If
ElseIf Err.Number = 2123 Then
strTitle = "Unacceptable Control Name"
strPrompt = "Please enter an acceptable control name"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbExclamation + vbOKOnly, _
Title:=strTitle)
GoTo TestNewName
Else
MsgBox "Error No: " & Err.Number _
& " in ControlCA procedure; " _
& "Description: " & Err.Description
End If

Resume Next

End Function

Public Function ControlSO(ctlSelected As Control, _


strPrefix As String, blnTag As Boolean, _
blnPrompt As Boolean) As Integer
'Called from LNC Rename and LNC Rename All property builders
'Does group renaming of all controls with source objects on a form or report
'Created by Helen Feddema 10-15-97
'Last modified 21-Jan-2011

On Error GoTo ErrorHandler

strOldCtlName = ctlSelected.ControlName
strSourceObject = Nz(ctlSelected.SourceObject)
lngControlType = ctlSelected.ControlType

If Left(strOldCtlName, 3) = strPrefix Then


GoTo ErrorHandlerExit
ElseIf strSourceObject <> "" Then
'Trim off object type prefixes (for recent versions of Access)
If Left(strSourceObject, 5) = "Form." Then
strSourceObject = Mid(strSourceObject, 6)
ElseIf Left(strSourceObject, 7) = "Report." Then
strSourceObject = Mid(strSourceObject, 8)
ElseIf Left(strSourceObject, 6) = "Table." _
Or Left(strSourceObject, 6) = "Query." Then
strSourceObject = Mid(strSourceObject, 7)
End If
If Left(strSourceObject, 3) = "frm" _
Or Left(strSourceObject, 3) = "rpt" _
Or Left(strSourceObject, 3) = "qry" _
Or Left(strSourceObject, 3) = "tbl" Then
strNewCtlName = strPrefix & _
Mid(StripNonAlphaNumericChars(strSourceObject), 4)
ElseIf Left(strSourceObject, 4) = "fsub" _
Or Left(strSourceObject, 4) = "tmak" _
Or Left(strSourceObject, 4) = "tlkp" _

VB Script Code Sample #62 Page 24 of 35


Or Left(strSourceObject, 4) = "fpri" _
Or Left(strSourceObject, 4) = "fsub" _
Or Left(strSourceObject, 4) = "fmnu" _
Or Left(strSourceObject, 4) = "fdlg" _
Or Left(strSourceObject, 4) = "rsub" Then
strNewCtlName = strPrefix & _
Mid(StripNonAlphaNumericChars(strSourceObject), 5)
Else
strNewCtlName = strPrefix & _
StripNonAlphaNumericChars(strSourceObject)
End If
ElseIf strSourceObject = "" Then
strNewCtlName = strPrefix & _
StripNonAlphaNumericChars(strOldCtlName)
End If

If Right(strNewCtlName, 7) = "Subform" Then


strNewCtlName = Left(strNewCtlName, Len(strNewCtlName) - 7)
ElseIf Right(strNewCtlName, 9) = "Subreport" Then
strNewCtlName = Left(strNewCtlName, Len(strNewCtlName) - 9)
End If

AutoRename:
If blnPrompt = True Then
GoTo TestNewName
ElseIf blnPrompt = False Then
If blnTag = True Then
ctlSelected.Tag = ctlSelected.ControlName
End If
ctlSelected.ControlName = strNewCtlName
GoTo ErrorHandlerExit
End If

TestNewName:
'Show the user
' - the original control name
' - the control type
' - control source
' - proposed new name
'and ask if the new name is acceptable.
blnRenameFail = True
Do While blnRenameFail
blnRenameFail = False
strTitle = "Rename control"
strPrompt = "Rename " & ControlTypeName(lngControlType) _
& " control currently named " _
& strOldCtlName & vbCrLf & _
"(control source: " & strControlSource & ") " & _
"to" & vbCrLf & strNewCtlName & "?"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbYesNoCancel + vbQuestion + vbDefaultButton1, _
Title:=strTitle)

'If the user clicks the Yes button, rename the control
If intReturn = vbYes Then
If blnTag = True Then
ctlSelected.Tag = ctlSelected.ControlName
End If
ctlSelected.ControlName = strNewCtlName
ElseIf intReturn = vbNo Then
UserInputName:
'Pop up an input box to edit the name.

VB Script Code Sample #62 Page 25 of 35


strProposedNewCtlName = _
StripNonAlphaNumericChars(InputBox("Modify new control name", _
"Rename control", strNewCtlName))
'Test whether proposed control name is blank, or just
'non-permitted characters
If strProposedNewCtlName = "" Then
'User canceled
GoTo ErrorHandlerExit
Else
strNewCtlName = strProposedNewCtlName
ctlSelected.ControlName = strNewCtlName
GoTo TestNewName
End If
ElseIf intReturn = vbCancel Then
GoTo ErrorHandlerExit
End If
Loop

ErrorHandlerExit:
Exit Function

ErrorHandler:
'If the proposed control name is already in use,
'return to the renaming dialog.
blnRenameFail = True
If Err.Number = 2104 Then
If blnPrompt = True Then
strTitle = "Duplicate Control Name"
strPrompt = "There is another control named " & _
strNewCtlName & "; please try again"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbExclamation + vbOKCancel, _
Title:=strTitle)
If intReturn = vbOK Then
strNewCtlName = strNewCtlName & "1"
ElseIf intReturn = vbCancel Then
GoTo TestNewName
End If
ElseIf blnPrompt = False Then
strNewCtlName = strNewCtlName + CStr(Int((20 - 1) * Rnd + 1))
GoTo AutoRename
End If
ElseIf Err.Number = 2123 Then
strTitle = "Unacceptable Control Name"
strPrompt = "Please enter an acceptable control name"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbExclamation + vbOKOnly, _
Title:=strTitle)
GoTo TestNewName
Else
MsgBox "Error No: " & Err.Number _
& " in ControlSO procedure; " _
& "Description: " & Err.Description
End If

Resume Next

End Function

Public Function ControlNA(ctlSelected As Control, _


strPrefix As String, blnTag As Boolean, _
blnPrompt As Boolean) As Integer

VB Script Code Sample #62 Page 26 of 35


'Called from LNC Rename and LNC Rename All property builders 'Does group renaming of
all controls not fitting the
'other categories on a form or report
'Created by Helen Feddema 10-15-97
'Last modified 21-Jan-2011

On Error GoTo ErrorHandler

strOldCtlName = ctlSelected.ControlName
lngControlType = ctlSelected.ControlType

'Special case for lines whose default name is "Line"


'or "Option" (same first three letters as the standard prefix)
If Left(strOldCtlName, 3) = strPrefix And Left(strOldCtlName, 6) _
<> "Option" And Left(strOldCtlName, 4) <> "Line" Then
GoTo ErrorHandlerExit
Else
strNewCtlName = strPrefix _
& StripNonAlphaNumericChars(strOldCtlName)
End If

AutoRename:
If blnPrompt = True Then
GoTo TestNewName
ElseIf blnPrompt = False Then
If blnTag = True Then
ctlSelected.Tag = ctlSelected.ControlName
End If
ctlSelected.ControlName = strNewCtlName
GoTo ErrorHandlerExit
End If

TestNewName:
'Show the user
' - the original control name
' - the control type
' - control source
' - proposed new name
'and ask if the new name is acceptable.
blnRenameFail = True
Do While blnRenameFail
blnRenameFail = False
strTitle = "Rename control"
strPrompt = "Rename " & ControlTypeName(lngControlType) _
& " control currently named " _
& strOldCtlName & vbCrLf & _
"(control source: " & strControlSource & ") " & _
"to" & vbCrLf & strNewCtlName & "?"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbYesNoCancel + vbQuestion + vbDefaultButton1, _
Title:=strTitle)

'If the user clicks the Yes button, rename the control
If intReturn = vbYes Then
If blnTag = True Then
ctlSelected.Tag = ctlSelected.ControlName
End If
ctlSelected.ControlName = strNewCtlName
ElseIf intReturn = vbNo Then
UserInputName:
'Pop up an input box to edit the name.

VB Script Code Sample #62 Page 27 of 35


strProposedNewCtlName = _
StripNonAlphaNumericChars(InputBox("Modify new control name", _
"Rename control", strNewCtlName))
'Test whether proposed control name is blank, or just
'non-permitted characters
If strProposedNewCtlName = "" Then
'User canceled
GoTo ErrorHandlerExit
Else
strNewCtlName = strProposedNewCtlName
ctlSelected.ControlName = strNewCtlName
GoTo TestNewName
End If
ElseIf intReturn = vbCancel Then
GoTo ErrorHandlerExit
End If
Loop

ErrorHandlerExit:
Exit Function

ErrorHandler:
'If the proposed control name is already in use,
'return to the renaming dialog.
blnRenameFail = True
If Err.Number = 2104 Then
If blnPrompt = True Then
strTitle = "Duplicate Control Name"
strPrompt = "There is another control named " & _
strNewCtlName & "; please try again"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbExclamation + vbOKCancel, _
Title:=strTitle)
If intReturn = vbOK Then
strNewCtlName = strNewCtlName & "1"
ElseIf intReturn = vbCancel Then
GoTo TestNewName
End If
ElseIf blnPrompt = False Then
strNewCtlName = strNewCtlName + CStr(Int((20 - 1) * Rnd + 1))
GoTo AutoRename
End If
ElseIf Err.Number = 2123 Then
strTitle = "Unacceptable Control Name"
strPrompt = "Please enter an acceptable control name"
intReturn = MsgBox(prompt:=strPrompt, _
Buttons:=vbExclamation + vbOKOnly, _
Title:=strTitle)
GoTo TestNewName
Else
MsgBox "Error No: " & Err.Number _
& " in ControlNA procedure; " _
& "Description: " & Err.Description
End If

Resume Next

End Function

Public Sub DeleteAndRecreate(strTable As String, dbsSelected As DAO.Database)


'Created by Helen Feddema 22-Jul-2007
'Modified by Helen Feddema 22-Jul-2007

VB Script Code Sample #62 Page 28 of 35


On Error Resume Next

'Delete old table (if there is one)


DoCmd.SetWarnings False
Set tdfs = dbsSelected.TableDefs
Set tdf = tdfs(strTable)
If Not tdf Is Nothing Then
tdfs.Delete (strTable)
End If

On Error GoTo ErrorHandler

'Create new, blank table to fill with data


strDBName = dbsSelected.Name
DoCmd.CopyObject destinationdatabase:=strDBName, _
newname:=strTable, _
sourceobjecttype:=acTable, _
sourceobjectname:=strTable & "Blank"

ErrorHandlerExit:
Exit Sub

ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in DeleteAndRecreate procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit

End Sub

Public Function ControlTypeName(lngType As Long) As String


'Created by Helen Feddema 24-Jul-2007
'Modified by Helen Feddema 24-Jul-2007

On Error GoTo ErrorHandler

Select Case lngType

Case 100
ControlTypeName = "Label"

Case 101
ControlTypeName = "Rectangle"

Case 102
ControlTypeName = "Line"

Case 103
ControlTypeName = "Image"

Case 104
ControlTypeName = "Command Button"

Case 105
ControlTypeName = "Option Button"

Case 106
ControlTypeName = "Check Box"

Case 107
ControlTypeName = "Option Group"

VB Script Code Sample #62 Page 29 of 35


Case 108
ControlTypeName = "Bound Object Frame"

Case 109
ControlTypeName = "Text Box"

Case 110
ControlTypeName = "List Box"

Case 111
ControlTypeName = "Combo Box"

Case 112
ControlTypeName = "Subform/Subreport"

Case 114
ControlTypeName = "Object Frame"

Case 118
ControlTypeName = "Page Break"

Case 122
ControlTypeName = "Toggle Button"

Case 123
ControlTypeName = "Tab Control"

Case 124
ControlTypeName = "Page"

Case 126
ControlTypeName = "Attachment"

Case Else
ControlTypeName = "Unknown"

End Select

ErrorHandlerExit:
Exit Function

ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in ControlTypeName procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit

End Function

Public Function CreateAndTestQuery(strTestQuery As String, _


strTestSQL As String) As Long
'Created by Helen Feddema 7-28-2002
'Last modified 24-Jul-2007

On Error Resume Next

'Delete old query


Set dbsCode = CodeDb
dbsCode.QueryDefs.Delete strTestQuery

On Error GoTo ErrorHandler

'Create new query

VB Script Code Sample #62 Page 30 of 35


Set qdf = dbsCode.CreateQueryDef(strTestQuery, strTestSQL)

'Test whether there are any records


Set rst = dbsCode.OpenRecordset(strTestQuery)
With rst
.MoveFirst
.MoveLast
CreateAndTestQuery = .RecordCount
End With

ErrorHandlerExit:
Exit Function

ErrorHandler:
If Err.Number = 3021 Then
CreateAndTestQuery = 0
Resume ErrorHandlerExit
Else
MsgBox "Error No: " & Err.Number _
& " in CreateAndTestQuery procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End If

End Function

Public Sub DeleteTable(strTable As String, dbsSelected As DAO.Database)


'Created by Helen Feddema 25-Jul-2007
'Modified by Helen Feddema 25-Jul-2007
On Error Resume Next

'Delete old table (if there is one)


DoCmd.SetWarnings False
Set tdfs = dbsSelected.TableDefs
Set tdf = tdfs(strTable)
If Not tdf Is Nothing Then
tdfs.Delete (strTable)
End If

ErrorHandlerExit:
Exit Sub

ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in DeleteTable procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit

End Sub

Appendix: The Leszynski Naming Convention


(This section is taken from my book, Expert One-on-One Microsoft Access Application
Development, Wiley, ISBN 0-7645-5904-4, with some modifications.)
The Leszynski Naming Convention uses prefixes to indicate the object type, control type
or variable data type, and (optionally) the table field data type. Use of distinctive
prefixes makes your database self-documenting; when you see frmSales in code, you will

VB Script Code Sample #62 Page 31 of 35


know that it is a form, and when you see curSales you will know that it is a Currency
variable. When selecting a query from a drop-down list, you will know that a query
starting with qmak is a make-table query, while one starting with quni is a union query.
This information will allow you to avoid errors such as using the SetFocus method (a
control method) with a field, or trying to open a table.
Hungarian notation (named after Charles Simonyi's native country) breaks down object
names into the following elements:
[prefix(es)][tag]BaseName[Suffix/Qualifier]
The elements of the naming convention are described in the following table.
Component Description Example
Prefix(es) A lowercase letter that adds extra information p for Public variable
to the tag
Tag A three-letter sequence indicating the object tbl for table
type
BaseName A word or two describing the object. If several OutlookContacts – contacts
words are used, each starts with a capital imported from Outlook
letter, and there are no spaces between them
Suffix (RVBA), A word giving more specific information about ByDate – the data is sorted by
Qualifier (LNC) an object date
I use LNC notation for database objects, controls on forms and reports, and variables. I
don't use field prefixes (although some developers do), because giving prefixes only to
controls prevents confusion – if a name (like StreetAddress) has no prefix, I know it is a
field name. Also, using field prefixes would interfere with the operation of my LNC
Rename add-in.
The basic LNC object prefixes are described in the following table.
Object Tag
Incomplete objects, backup objects, or objects _ or – (In Access 2000 and later, dashes sort
that are under development to the beginning of the database object list.)
Hidden system objects zh
Displayed system objects zs
Programmatically created temporary objects zt
Backup copies of objects, for later copying or zz
reuse
The basic LNC variable prefixes are described in the following table.
Variable type Tag
Local variable [no prefix]
Local static variable s
Module-level variable m
Public variable in a form or report module p
Public variable declared in the Declarations section of a standard g
module
The basic LNC database object tags are described in the following table.
Object Tag
Class module cls
Form frm

VB Script Code Sample #62 Page 32 of 35


Form (dialog) fdlg
Form (menu) fmnu
Form (message) fmsg
Form (subform) fsub
Macro mcr
Module bas
Query (any type) qry
Query (append) qapp
Query (crosstab) qxtb
Query (data definition) qddl
Query (delete) qdel
Query (form filter) qflt
Query (lookup) qlkp
Query (make-table) qmak
Query (select) qry (or qsel)
Query (SQL pass-through) qspt
Query (union) quni
Query (update) qupd
Report rpt
Report (subreport) rsub
Table tbl
Table (attached dBASE) tdbf
Table (attached Excel) txls
Table (attached FoxPro) tfox
Table (attached Lotus) twks
Table (attached ODBC) todb
Table (attached Paradox) tpdx
Table (attached SQL Server) tsql
Table (attached text) ttxt
Table (lookup) tlkp
The following table lists the LNC table field tags (I don't use these personally, but I am
including them for completeness).
Object Tag
Autonumber (random non-sequential) idn
Autonumber (replication ID) idr
Autonumber (sequential) ids
Binary bin
Byte byt
Currency cur
Date/Time dtm
Double dbl
Hyperlink hlk
Integer int
Long lng
Memo mem
OLE ole
Single sng
Text (character) chr
Yes/No (Boolean) ysn

VB Script Code Sample #62 Page 33 of 35


The following table lists the LNC tags for VBA variables.
Object Tag
Boolean bln
CommandBar cbr
Control (generic; useful when cycling through ctl
controls on a form or report)
Currency cur
Database dbs
Double dbl
Form frm
Integer int
Long lng
QueryDef qdf
Report rpt
Single sng
Snapshot snp
String str
Table tbl
Type (user-defined) typ
Variant var
The LNC tags for form and report controls are listed in the following table. They may
also be used in code referencing controls.
Object Tag
Attachment att
Bound object frame frb
Chart (graph) cht
Check box chk
Combo box cbo
Command button cmd
Custom control ocx
Frame fra
Hyperlink hlk
Image img
Label lbl
Line lin
List box lst
Option button opt
Option group grp
Page (on a Tab control) pge
Page break brk
Rectangle (shape) shp
Subform/report sub
Tab control tab
Text box txt
Toggle button tgl
Unbound object frame fru
The following table lists some typical LNC names for database objects, controls and
variables.

VB Script Code Sample #62 Page 34 of 35


Object/variable name LNC naming elements Description
tblEmployees tag + base name A table of employee data
qupdSales tag + base name A query that updates sales data
fsubDayMax tag + base name + qualifier A subform that shows the maximum
day
intLines tag + base name An Integer variable to hold a value
representing the number of lines in a
Word table
curSales tag + base name A Currency variable holding a Sales
value
pstrForm prefix + tag + base name A public String variable holding a
form name
zztblContacts prefix + tag + base name A backup Contacts table, for copying
and filling with imported data
In addition to the standard LNC tags, you can create your own as needed. I use the
tmak tag for tables created from make-table queries, with the same base name, so when I
see a table called tmakNewContacts, I know that it was created by a query called
qmakNewContacts. This lets me know that if I want to modify the table, I need to
modify the query that generates it. I also use qtot as a prefix for totals queries –
although totals queries are just a type of select queries, it is useful to know which
queries are totals queries, for use in subforms displaying totals.
The new version of my LNC Rename add-in lets you add your own custom tags to a
table stored in the add-in, so that when you are renaming objects, the ones with your
custom tags (as well as those with standard LNC tags) won't be presented for renaming.

Notes
7/2/99 Added linefeed and carriage return characters (ASCII #13 and #10) to list of
characters to strip out of control names (primarily for multi-line labels)
7/7/01 Updated database to work with Access 2002/FAR 9e
7/26/07 Added dialog for entering custom LNC tags
7/29/07 Added confirmation message to allow automatic renaming of all form or
report controls.
1/21/11 Created this Access 2007 version of the add-in, for control renaming only.

Contents of Zip File


File Name File Type Destination Folder
LNC Control Renaming Access 2007 library database Add-ins folder – usually C:\Users\User
2007.acca (works in Access 2007 through Name\AppData\Roaming\Microsoft\AddIns
2010)
Event Access 2000 database \My Documents (or wherever you want)
Management.mdb
Northwind.mdb Access 2000 database \My Documents (or wherever you want)
VB Script Code Sample Word 2007-2010 document \My Documents (or wherever you want)
#62 -- An Access 2007
Add-in That Automates
Renaming
Controls.docx

VB Script Code Sample #62 Page 35 of 35

Potrebbero piacerti anche