Is there a way to check if a control is a part of an array? I want to do this at runtime. :)
Printable View
Is there a way to check if a control is a part of an array? I want to do this at runtime. :)
Looping thru the controls on the form, if referencing the Index property does not generate an error, then the control is part of a control array:
VB Code:
Private Sub Command1_Click() Dim objCtl As Control Dim intX As Integer On Error Resume Next For Each objCtl In Form1.Controls intX = objCtl.Index If Err.Number = 0 Then Debug.Print objCtl.Name & " is a control array." Else Debug.Print objCtl.Name & " is NOT a control array." Err.Clear End If Next End Sub
There is a very little documented TypeName() function than does the trick:
VB Code:
If TypeName(Command1) = "Object" Then MsgBox "Control is member of control array" Else MsgBox "Control is not member of control array" End If
EDIT: for in-depth explanations visit Randy's site .
Thanks for the help guys! :wave:
RhinoBull: That was what I was looking for!
Excellent!
Brilliant!! :thumb:Quote:
Originally Posted by RhinoBull
I'd previously used something similar to BruceG's method. It works but not very elegant. If any snippet of code deserves to be in the CodeBank, this is it!
Cheers
My way (which I've modified to fit Bruce's code) is a tiny bit simpler.
VB Code:
Private Sub Command1_Click(Index As Integer) Dim objCtl As Control On Error Resume Next For Each objCtl In Form1.Controls If objCtl.Index > -1 Then Debug.Print objCtl.Name & " is a control array." Else Debug.Print objCtl.Name & " is NOT a control array." Err.Clear End If Next End Sub
BTW, John now that we've helped you, please help us and indicate that you have your answer by editing your first post in this thread and adding then green checkmark.
Thanks. :wave:Quote:
Originally Posted by pnish