I'm wondering if it's possible to check if a control with a specified name exists on a form, perhaps something along the lines of a DLookup but for controls?
Some Context..
I've got multiple buttons on a form that belong to different groups.
For example, lets say I'm giving someone the option of 1, 2, 3, 4, or 5 of an item (this is purely for the sake of an example, don't read too much into it)
If they click the button for '2' that button's color darkens to give confirmation that it has been selected. Then, if they change their mind and click the button for '3', that button will darken, and the button for '2' will return to its original color.
I'm trying to do this for multiple different groups of buttons without having to write code for each group individually. All the button groups are labeled similarly, such as "btnTE1", "btnTE2", "btnTE3" for one group, "btnCON1", "btnCON2" for another, so on and so forth.
This is what I have so far.
Code:
Public Sub btnFilClr(btnPrefix As String)
Dim clr1 As Long, clr2 As Long, btnName As String
clr1 = RGB(122, 197, 205)
clr2 = RGB(152, 245, 255)
For i = 1 To 24
btnName = btnPrefix & i
If btnName = Screen.ActiveControl.Name Then
Me.Controls(btnName).BackColor = clr1 ' --- This sets the control that was clicked to a darker color
ElseIf *control with name exists on form* Then
Me.Controls(btnName).BackColor = clr2 ' --- This sets all the other controls in that group to their original color
End If
Next
Exit Sub
End Sub
btnPrefix would be something like "btnTE" or "btnCON", and i places the numbers in front of it. Also, i goes to 24 because that's the highest number of buttons in one group. The issue here is if a group doesn't have 24 buttons then a name is generated that won't exist on the form.
There may be a simpler way of doing this, I have a tendency to overthink / over complicate things
Hopefully I explained that well enough. If any additional information is needed please let me know. Any help is always appreciated!