Is there a way to determine that a control is part of an array without using
error checking
thanks Alex
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
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
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
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
>
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>...
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
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>
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
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:
>
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 ...
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 ...
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
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
»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
»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
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
>>
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
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
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>...
LFS