I’m imagining what the new code might look like based on the last message I wrote on this topic.
Here is the URL to the previous article: https://www.accessjumpstart.com/custom-tags-for-form-controls-the-case-to-use-them-for/
So given this kind of syntax:
Dim ctlPositions As CustomCtlTags
Set ctlPositions = New CustomCtlTags
ctlPositions.Set ctl, "FromLeft", ctl.Left
Debug.Print ctlPositions.Get(ctl, "FromLeft")
What would that look like in the existing code I’d want to update? Is it really buying me anything? Let’s see the old code first:
'This is the associated code that is setting the ctl.Tag property and populating it with a string
' Now store the original size and location values for each control
For Each ctl In frm.Controls
ctlLeft = ctl.Left
ctlTop = ctl.Top
ctlWidth = ctl.Width
ctlHeight = ctl.Height
Select Case ctl.ControlType
Case acLabel, acCommandButton, acTextBox, acComboBox, acListBox, acTabCtl, acToggleButton
ctlOrigFontSize = ctl.FontSize
Case acSubform
If DoSubForms Then
On Error Resume Next
Set SubForm = ctl.Form
errorNum = Err.Number
On Error GoTo ProcError
If errorNum <> 0 Then GoTo NextTag ' must be a subReport so skip it
SaveControlPositions SubForm ' recursively calling self here
End If
ctlOrigFontSize = 0
Case Else
ctlOrigFontSize = 0
End Select
ctl.Tag = RsBeg & ctlLeft & ":" & ctlTop & ":" & ctlWidth & ":" & ctlHeight & ":" & ctlOrigFontSize & RsEnd
NextTag: Next
'This is the associated code from inside the class that is reading the information using ctl.Tag
' property.
'Split the Tag property into an array
TagStr = Mid(ctl.Tag, InStr(ctl.Tag, RsBeg) + 5, InStr(ctl.Tag, RsEnd) - 6)
tagArray = Split(TagStr, ":")
ctl.Move ScaleFactor * (CDbl(tagArray(CtlPositionTag.FromLeft))), _
ScaleFactor * (CDbl(tagArray(CtlPositionTag.FromTop))), _
ScaleFactor * (CDbl(tagArray(CtlPositionTag.ControlWidth))), _
ScaleFactor * (CDbl(tagArray(CtlPositionTag.ControlHeight)))
And now I will change it to use the CustomCtlTags object to see what this simplifies.
Dim ctlPositions As CustomCtlTags
Set ctlPositions = New CustomCtlTags
'This is the associated code that is setting the ctl.Tag property and populating it with a string
' Now store the original size and location values for each control
For Each ctl In frm.Controls
ctlPositions.Set ctl, "OrigLeft", ctl.Left
ctlPositions.Set ctl, "OrigTop", ctl.Top
ctlPositions.Set ctl, "OrigWidth", ctl.Width
ctlPositions.Set ctl, "OrigHeight", ctl.Height
Select Case ctl.ControlType
Case acLabel, acCommandButton, acTextBox, acComboBox, acListBox, acTabCtl, acToggleButton
ctlPositions.Set ctl, "OrigFontSize", ctl.FontSize
Case acSubform
If DoSubForms Then
On Error Resume Next
Set SubForm = ctl.Form
errorNum = Err.Number
On Error GoTo ProcError
If errorNum <> 0 Then GoTo NextTag ' must be a subReport so skip it
SaveControlPositions SubForm ' recursively calling self here
End If
ctlPositions.Set ctl, "OrigFontSize", 0
Case Else
ctlPositions.Set ctl, "OrigFontSize", 0
End Select
NextTag: Next
'This is the associated code from inside the class that is now retrieving the information
' from a global ctlPostions variable.
ctl.Move ScaleFactor * (CDbl(ctlPositions.Get(ctl, "OrigLeft"))), _
ScaleFactor * (CDbl(ctlPositions.Get(ctl, "OrigTop"))), _
ScaleFactor * (CDbl(ctlPositions.Get(ctl, "OrigWidth"))), _
ScaleFactor * (CDbl(ctlPositions.Get(ctl, "OrigHeight")))
Frankly, it allowed me to remove about 4 lines of processing code, and would also allow me to remove the Enum definition which is another 6 lines. But then I’m adding 2 lines to Dim the class and instantiate it.
So overall, I’ve shortened the code by 8 or so lines and removed some comments. I think maybe it’s a little more readable, but I think I would desire a lot more simplicity out of it. I’ll revisit this later and see what I can come up with in terms of more simplified syntax.