View Full Version : Solved: More efficient, easier to read VBA
cath_hopes
10-08-2007, 07:05 AM
Is there a smarter way of coding a group of identical property changes for different controls? for eg. the following looks long-winded:
Me.mycontrol1.enabled = false
Me.mycontrol2.enabled = false
Me.mycontrol3.enabled = false
Me.mycontrol4.enabled = false
Me.mycontrol5.enabled = false
Thanks!
Well, I'm not entirely sure about the syntax for the "Controls" collection, but I did something similiar once with form controls on a spreadsheet.
Maybe something like this?
Dim x As Integer
For x = 1 To 5
Me.Controls("mycontrol" & x).Enabled = false
Next x
Of course, that depends on how you name the controls...
You could also do a For Each loop with an If to screen for specific control types/names.
cath_hopes
10-08-2007, 07:14 PM
My controls have names that are unrelated to one another. I was wondering whether a control list could be set up (the control list would have to have the same data type of course for eg.
Dim crtllist ...
ctrllist = (controla, controlc, controlf, controlq)
Me.ctrllist.Enabled = False
or even:
...
Me.(controla, controlc, controlf, controlq).Enabled = False
Other programming languages have a similar facility eg. LANSA
Otherwise, I might be able to do something with your 'For Each loop with an If to screen for specific control types/names' suggestion.
Thank you from the other side of the Atlantic!
lalbatros
10-09-2007, 05:40 AM
You could also use a tag to indentify controls of the same sort.
There is a "Tag" property for any control.
Looping through the controls and checking each tag could help you decide what to do for each control.
(from the other side of the channel)
Oorang
10-11-2007, 08:13 AM
I use that one myself so I can change large groups of controls with one function call. Here is an example:
Sub DisableGroup(groupTag As String)
Const lngMatch_c As Long = 0
Dim ctrl As access.Control
Dim strCrntTag As String
Dim lngTagLenB As Long
lngTagLenB = LenB(groupTag)
For Each ctrl In Me.Controls
strCrntTag = ctrl.Tag
If LenB(strCrntTag) = lngTagLenB Then
If StrComp(strCrntTag, groupTag, vbTextCompare) = lngMatch_c Then
ctrl.Enabled = False
End If
End If
Next ctrl
End Sub
cath_hopes
10-11-2007, 09:11 AM
Thanks Aaron!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.