Code:
'---------------------------------------------------------------------------------------
' File : basLabelTuner
' Author : davegri
' Date : 7/3/2021
' Purpose: Change default form label names created by Access
' Routine will modify label captions following "Camel" conventions.
' Example: if label caption is LastName it will be changed to Last Name
' ZipPostalCode will be changed to Zip Postal Code
' Essentially every Capitol letter will be replaced by Space & Capital Letter (except the first).
' Double spaces (and greater) in label captions will be reduced to one space.
' Captions that contain "ID", "PK" or "FK" will not have a space added to center or prefix.
'---------------------------------------------------------------------------------------
Option Compare Database
Option Explicit
'---------------------------------------------------------------------------------------
' Method : fcnFormatAllLabels
' Author : davegri
' Date : 6/29/2021
' Purpose: For existing form
'---------------------------------------------------------------------------------------
Public Function fcnFormatAllLabels(frmName As String)
Dim Ctl As Control, frm As Form
Dim C As String, i As Integer, QED As String
DoCmd.Close acForm, frmName
DoCmd.OpenForm frmName, acDesign
Set frm = Forms(frmName)
For Each Ctl In frm.Detail.Controls
If Ctl.ControlType = acLabel Then
C = Ctl.Caption
For i = 65 To 90 'Cap A to cap Z
C = fcnReplaceAll(C, Chr$(i), " " & Chr$(i))
C = fcnReplaceAll(C, " I D", "ID")
C = fcnReplaceAll(C, " P K", "PK")
C = fcnReplaceAll(C, " F K", "FK")
C = fcnReplaceAll(C, Space(2), Space(1))
Next i
Ctl.Caption = Trim(C)
End If
Next Ctl
DoCmd.Close acForm, frm.Name, acSaveYes
End Function
'---------------------------------------------------------------------------------------
' Method : fcnFormatSingleLabel
' Author : davegri
' Date : 6/29/2021
' Purpose: Called by code while creating new form programatically.
' arg is the field name (which normally becomes the textbox's label caption).
'---------------------------------------------------------------------------------------
Public Function fcnFormatSingleLabel(arg As String)
Dim C As String, i As Integer
C = arg
For i = 65 To 90
C = fcnReplaceAll(C, Chr$(i), " " & Chr$(i))
C = fcnReplaceAll(C, " I D", "ID")
C = fcnReplaceAll(C, " P K", "PK")
C = fcnReplaceAll(C, " F K", "FK")
C = fcnReplaceAll(C, Space(2), Space(1))
Next i
C = Trim(C)
fcnFormatSingleLabel = C
End Function
Function fcnReplaceAll(ByVal Target As String, ByVal arg As String, ByVal NewArg As String) As String
If InStr(1, NewArg, arg, vbBinaryCompare) > 0 Then
Target = fcnReplaceAllOnce(Target, arg, NewArg)
Else
Do While InStr(1, Target, arg, vbBinaryCompare) > 0
Target = fcnReplaceAllOnce(Target, arg, NewArg)
Loop
End If
fcnReplaceAll = Target
End Function
Function fcnReplaceAllOnce(ByVal Target As String, ByVal arg As String, ByVal NewArg As String) As String
Dim i As Long
If fcnIsNothing(arg) Then
fcnReplaceAllOnce = Target
Else
If StrComp(arg, NewArg, vbBinaryCompare) = 0 Then
fcnReplaceAllOnce = Target
Else
i = InStr(1, Target, arg, vbBinaryCompare)
Do While i > 0
Target = Left(Target, i - 1) & NewArg & Mid(Target, i + Len(arg))
i = i + Len(NewArg)
i = InStr(i, Target, arg, vbBinaryCompare)
Loop
fcnReplaceAllOnce = Target
End If
End If
End Function
Function fcnIsNothing(ByVal Target As String) As Boolean
If Target & "" = "" Then
fcnIsNothing = True
Else
fcnIsNothing = False
End If
End Function