Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I check Control is part of an array

1 view
Skip to first unread message

ALEX

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
I need to check that a control is part of an array before I access the
index property. I thought that the isArray function would work but it does
not, I guess the control is not an array but just part of one.

Is there a way to determine that a control is part of an array without using
error checking
thanks Alex

Bertie Wooster

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
ALEX <al...@kline-uk.com> wrote in message
news:929365699.16842.0...@news.demon.co.uk...

A control array is not the same thing as an array of variables, UDTs or
object references. As you've found, IsArray is not designed to work with
members of a control array.

You can't access the Index property of a control that's not an array, so you
will have to trap the error. This is not the only circumstance in VB where
you have to resort to trapping a possibly-expected error condition.

Bertie

Jeff Ashley

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
Errors are not sins and errors are not crimes - errors are INFORMATION.!

ALEX wrote:

> I need to check that a control is part of an array before I access the
> index property. I thought that the isArray function would work but it does
> not, I guess the control is not an array but just part of one.
>
> Is there a way to determine that a control is part of an array without using
> error checking

> thanks Alex


Rick Rothstein

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
If a control is part of a control array, the TypeName function returns the
string "Object"; otherwise it returns a string specifying the type of
control passed to it. To see what type a control is (if it is a member of a
control array), pass a specific instance of it to TypeName. No error
checking required.

For example, if Text1(0) and Text1(1) are part of a control array and Text2
is a simple text box that is not part of a control array, then

Debug.Print TypeName(Text1) returns "Object"

Debug.Print TypeName(Text1(1) returns "TextBox"

Debug.Print TypeName(Text2) returns "TextBox"

Rick

Bertie Wooster wrote in message
<#WIxPent#GA....@cppssbbsa02.microsoft.com>...


>ALEX <al...@kline-uk.com> wrote in message
>news:929365699.16842.0...@news.demon.co.uk...

>> I need to check that a control is part of an array before I access the
>> index property. I thought that the isArray function would work but it
does
>> not, I guess the control is not an array but just part of one.
>>
>> Is there a way to determine that a control is part of an array without
>using
>> error checking
>

Rick Rothstein

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
True, but not required in this case. See my answer to Bertie.

Rick

Jeff Ashley wrote in message <3765289B...@mediaone.net>...


>Errors are not sins and errors are not crimes - errors are INFORMATION.!
>
>ALEX wrote:
>

>> I need to check that a control is part of an array before I access the
>> index property. I thought that the isArray function would work but it
does
>> not, I guess the control is not an array but just part of one.
>>
>> Is there a way to determine that a control is part of an array without
using
>> error checking

>> thanks Alex
>

Rick Rothstein

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
Use the TypeName function. This is a repeat of my posted response to Bertie
Wooster's response to you.

If a control is part of a control array, the TypeName function returns the
string "Object"; otherwise it returns a string specifying the type of
control passed to it. To see what type a control is (if it is a member of a
control array), pass a specific instance of it to TypeName. No error
checking required.

For example, if Text1(0) and Text1(1) are part of a control array and Text2
is a simple text box that is not part of a control array, then

Debug.Print TypeName(Text1) returns "Object"

Debug.Print TypeName(Text1(1) returns "TextBox"

Debug.Print TypeName(Text2) returns "TextBox"

Rick

ALEX wrote in message
<929365699.16842.0...@news.demon.co.uk>...

Bertie Wooster

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
Rick Rothstein <fnr...@aosi.com> wrote in message
news:e#0KFRot#GA.233@cppssbbsa03...

> If a control is part of a control array, the TypeName function returns the
> string "Object"; otherwise it returns a string specifying the type of
> control passed to it. To see what type a control is (if it is a member of
a
> control array), pass a specific instance of it to TypeName. No error
> checking required.
>
> For example, if Text1(0) and Text1(1) are part of a control array and
Text2
> is a simple text box that is not part of a control array, then
>
> Debug.Print TypeName(Text1) returns "Object"
>
> Debug.Print TypeName(Text1(1) returns "TextBox"
>
> Debug.Print TypeName(Text2) returns "TextBox"

That's interesting, Rick, but is no use when all you have is a control
reference. As far as I know you can't get to the runtime equivalent of
Text1 to give to TypeName() when all you have is a reference to the control
Text1 or Text1(1) and want to distinguish these. If variable c contains the
reference, c.Name doesn't produce the goods.

Bertie

Jeff Ashley

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
I don't get you 100% on this, Bertie, unless you're noting that the answer is
contained in the question . . . This works, but I can't think of a case where I
would find it useful. Definitely not (which I think proceeds from your point)
with regard to the controls collection, where the way you are going to find out
if a control is a member of an array is to reference its index property and trap
the error . . .

Doesn't anybody have real problems any more? At least this isn't one of those
HELP!!! posts that asks if it is possible to write a VB app without using the
letter 'e' . . . <g>

Jeff Ashley

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to

Of course I get you 100%, Bertie, the same proportion, coincidentally, to which
you are correct!

Rick Rothstein

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
I was thinking of a function along this line

Function IsControlArray(ControlIn As Variant) As Boolean
If TypeName(ControlIn) = "Object" Then
IsControlArray = True
Else
IsControlArray = False
End If
End Function

I thought that is what the poster was asking for -- a way to tell if his
control was part of a control array or not. Was (am) I wrong?

Rick

Bertie Wooster wrote in message

<#QYaxLpt#GA...@cppssbbsa02.microsoft.com>...

Bertie Wooster

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
I was just trying to say that TypeName(Text1) and TypeName(Text1(1)) are
resolved at compile time as far as I can see, and there's nothing you can do
with a control reference at runtime to produce the "Object" return value.
You said it better than me!

Bertie

Jeff Ashley <jlas...@mediaone.net> wrote in message
news:37654C77...@mediaone.net...


> I don't get you 100% on this, Bertie, unless you're noting that the answer
is
> contained in the question . . . This works, but I can't think of a case
where I
> would find it useful. Definitely not (which I think proceeds from your
point)
> with regard to the controls collection, where the way you are going to
find out
> if a control is a member of an array is to reference its index property
and trap
> the error . . .
>
> Doesn't anybody have real problems any more? At least this isn't one of
those
> HELP!!! posts that asks if it is possible to write a VB app without using
the
> letter 'e' . . . <g>
>
> Bertie Wooster wrote:
>

Rick Rothstein

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to
Alright, I'm confused. Earlier I posted the following function (which
received no reply)

Function IsControlArray(ControlIn As Variant) As Boolean
If TypeName(ControlIn) = "Object" Then
IsControlArray = True
Else
IsControlArray = False
End If
End Function

As far as I can tell, this works just fine at RUN TIME.
IsControlArray(Text1) returns "Object" and IsControlArray(Text1(1)) returns
"TextBox" as does IsControlArray(Text2). I'm pretty sure the original poster
was looking for something like this (more or less) as an answer to his post.

What am I missing in either the original post or Jeff and your response
threads? Did I trick you into thinking I hadn't checked what I wrote when I
said "I was THINKING of a function along this line"? If I missed something
in my understanding of this problem or your comments, could you please
provide an explanation. I really do want to understand the problem and learn
from it.

Rick

Bertie Wooster wrote in message ...

Luca Faiazza

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
Rick, think about this:

The original question was :


"I need to check that a control is part of an array before I access the
index property. "

What you offer is the function IsControlArray.

As you said before :


>IsControlArray(Text1) returns "Object" and IsControlArray(Text1(1)) returns
>"TextBox"

That's it. IsControlArray(Text1(1)) returns False. So it does not resolve
the original problem.
The original problem was "how do you find out if text1(1) is an item of a
control array".
The IsControlArray-function does NOT tell it. It tells only if the passed
object is a control-array
object or if its a single control.

Got it ?

Luca Faiazza, lu...@basware.fi

Rick Rothstein wrote in message ...

Luca Faiazza

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
IsControlArray does not resolve the original problem,
this one solves (the Bertie's solution)

Function IsItemOfControlArray(ControlIn As object) As Boolean

dim dummy as integer
on error resume next
d = controlln.index
IsItemOfControlArray = (err.number <>0)
err.clear

End Function

Luca Faiazza

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
Rick, your answer was to a question "how do I check if an object is a
control array".
But the original question was not that :)

Luca

Rick Rothstein wrote in message ...

>True, but not required in this case. See my answer to Bertie.
>
>Rick
>
>Jeff Ashley wrote in message <3765289B...@mediaone.net>...
>>Errors are not sins and errors are not crimes - errors are INFORMATION.!
>>
>>ALEX wrote:
>>

>>> I need to check that a control is part of an array before I access the

Paul Clement

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
"ALEX" <al...@kline-uk.com> wrote:

»I need to check that a control is part of an array before I access
the
»index property. I thought that the isArray function would work but it
does
»not, I guess the control is not an array but just part of one.
»
»Is there a way to determine that a control is part of an array
without using
»error checking

You can cycle through the Controls collection and pass the control to
Rick's function, or, you can simply trap the error that occurs when
you reference the index.


Paul
~~~~
pcle...@ameritech.net

Paul Clement

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
"Rick Rothstein" <fnr...@aosi.com> wrote:

»I was thinking of a function along this line
»
» Function IsControlArray(ControlIn As Variant) As Boolean


» If TypeName(ControlIn) = "Object" Then
» IsControlArray = True
» Else
» IsControlArray = False
» End If
» End Function

»
»I thought that is what the poster was asking for -- a way to tell if
his
»control was part of a control array or not. Was (am) I wrong?

I was about to say that you could cycle through the Controls
collection to get a reference to pass to your function but...I can't
reproduce the behavior of your code. The TypeName function always
returns the true class name and not the generic object class.


Paul
~~~~
pcle...@ameritech.net

Rick Rothstein

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Thank you Luca - yes I finally get it.

It took both of your posts before I realized -- in a function type setting,
we don't have access to the Control Array Object itself, only the Elements
of the Control Array.

It is amazing how dense I can be sometimes. I was coming at this problem
from the wrong direction and couldn't for the life of me get it out of my
mind. Whew, I'm glad that is over. Thanks again.

Rick


Luca Faiazza wrote in message ...


>IsControlArray does not resolve the original problem,
>this one solves (the Bertie's solution)
>
>Function IsItemOfControlArray(ControlIn As object) As Boolean
>
>dim dummy as integer
> on error resume next
> d = controlln.index
> IsItemOfControlArray = (err.number <>0)
> err.clear
>
>End Function
>
>Luca Faiazza, lu...@basware.fi

>Rick Rothstein wrote in message ...

>>Alright, I'm confused. Earlier I posted the following function (which
>>received no reply)
>>

>> Function IsControlArray(ControlIn As Variant) As Boolean
>> If TypeName(ControlIn) = "Object" Then
>> IsControlArray = True
>> Else
>> IsControlArray = False
>> End If
>> End Function
>>

Bertie Wooster

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Paul Clement <UseAdddressA...@swspectrum.com> wrote in message
news:376cf827...@msnews.microsoft.com...

At the risk of going over the top on this one, my point is that if you pass
a member of the Controls collection to Rick's function you will just get the
control's class, even if the individual control is a member of a control
array.

A. TypeName("Text1") produces "String"
B. TypeName(Text1) produces "Object" if it's a control array
C. TypeName(Text1(1)) produces "TextBox"

The problem is that there's no way to use format B at runtime unless you
hard-code it, but then you know the answer anyway!

Bertie

Larry Serflaten

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
I wonder why some people have an aversion against error trapping?
If you want to avoid the easy route, you have to provide the needed
code....

There is a method that MAY work if you have no overlapping controls
on your form. The general idea is that you loop through all the known
control arrays comparing each of their screen positions, against the
control in question:
[Example assumes first default control name is the array: Text1(x)]

Private Function IsArrayItem(NewControl) As Boolean
Dim ctl
'Check1 array test
For Each ctl In Check1
If ctl.Top = NewControl.Top And ctl.Left = NewControl.Left Then IsArrayItem = True
Next
'Image1 array test
For Each ctl In Image1
If ctl.Top = NewControl.Top And ctl.Left = NewControl.Left Then IsArrayItem = True
Next
'Label1 array test
For Each ctl In Label1
If ctl.Top = NewControl.Top And ctl.Left = NewControl.Left Then IsArrayItem = True
Next
'Option1 array test
For Each ctl In Option1
If ctl.Top = NewControl.Top And ctl.Left = NewControl.Left Then IsArrayItem = True
Next
'Picture1 array test
For Each ctl In Picture1
If ctl.Top = NewControl.Top And ctl.Left = NewControl.Left Then IsArrayItem = True
Next
'Text1 array test
For Each ctl In Text1
If ctl.Top = NewControl.Top And ctl.Left = NewControl.Left Then IsArrayItem = True
Next
'... ad nausea....
End Function

If you have to use a routine without error trapping, and you expect to
use it often I would advise you change the lines in the above code from:

If ctl.Top = NewControl.Top And ctl.Left = NewControl.Left Then IsArrayItem = True

To:

ctl.Tag = True

Run it once at start up, and from then on look at the controls tag
property to see if it is in an array....

HTH
LFS

ALEX wrote:

> I need to check that a control is part of an array before I access the
> index property. I thought that the isArray function would work but it does
> not, I guess the control is not an array but just part of one.
>
> Is there a way to determine that a control is part of an array without using
> error checking

> thanks Alex

Luca Faiazza

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
>Private Function IsArrayItem(NewControl) As Boolean
>Dim ctl
> 'Check1 array test
> For Each ctl In Check1
> If ctl.Top = NewControl.Top And ctl.Left = NewControl.Left Then
IsArrayItem = True
> Next

Why so complicated ? I would prefer this way :

For Each ctl In Check1

if ctl Is NewControl then
IsArrayItem = True
Exit For
End if
Next

Luca Faiazza

Larry Serflaten wrote in message <376795...@usinternet.com>...

Larry Serflaten

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
I tried that method first, but it doesn't work correctly in all cases.
Try it. It seems the condition of the checkbox (checked or unchecked)
changed the result!

LFS

0 new messages