check if element of a control array exists
hi.
i'd like to know, is there a way of checking, if an element of a control array exists, before i perform a Load or Unload.
i know, i can set On Error - but i guess that's not very elegant solution.
the thing is, that i'd like to create a control array in runtime, but the indexes, are not going up constantly.
for example i create an control array like this:
Load ArrayCtl(1)
Load ArrayCtl(68)
Load ArrayCtl(23)
Load ArrayCtl(45)
what i want is to check if element with given index (23 for example) exists, before i do anything with it.
so? what do you think?
Re: check if element of a control array exists
I know you're looking for something else, but I would use On Error. You don't have to just resume next if you feel that it is an inelegant solution.
You can On Error Resume Next and then access a property of a control that may or not be in the array, and then check to see if err.number<>0 - that would tell you that the item does not exist. You could then clear the error & go on to the next one. If err.number=0 then the control exists & you can perform your operation.
Obviously if you're only performing one operation & don't need to track whether the control exists or not, it would be simpler just to protect from errors and run through them all without regard to whether or not the operation did anything.
Re: check if element of a control array exists
Code:
If (Not ArrayCtl(x) Is Nothing) Then
Re: check if element of a control array exists
to bad... it dosent rise an error, but it always returns the same value, no matter if the control exists or not... :(
Re: check if element of a control array exists
try to create the object.. if it gives you an error then an object exists in that location.
you would use "On Error Resume Next" and "On Error Goto 0".
Code:
On Error Resume Next
Load ArrayCtl(23)
If (Err.Number <> 0) Then
'Clear the error
Err.Clear
Resume
'Do what ever you would want if the object did not exist
...
...
Else
'Do what ever you would want if the object did exist
...
...
End If
On Error Goto 0
You can also simply access the object instead of attempting to create it. The same approach can be used for both.
Re: check if element of a control array exists
Quote:
Originally Posted by penagate
Code:
If (Not ArrayCtl(x) Is Nothing) Then
That really looks like it would either work, or raise an error, but it does neither.
My question is why can't you load and unload in numeric order? That would really solve the problem without using On Error, which I avoid also.
Re: check if element of a control array exists
Quote:
Originally Posted by WizBang
That really looks like it would either work, or raise an error, but it does neither.
How terribly apathetic.
I tried various other things, but none of them were able to distinguish between loaded and unloaded array elements. Strange.
Re: check if element of a control array exists
I even tried ObjPtr(), and was surprised to see it returning a value for non-existant control array elements.
Re: check if element of a control array exists
Quote:
Originally Posted by WizBang
I even tried ObjPtr(), and was surprised to see it returning a value for non-existant control array elements.
I tried that too! And VarPtr(). It seems the array is redimensioned and the objects instantiated, but they don't go into the proper "loaded" state. Very strange.
1 Attachment(s)
Re: check if element of a control array exists
Using Vartype(Check1(6)) <> 9 seems to work without the need of error handling. When the array element exists it returns apparently the type of the default property of the control while when it does not exist returns 9 (vbObject) without raising a error. But yet very weird.
Re: check if element of a control array exists
Using VarType will work IF the control has a default member and that default member returns value other than vbObject. If the control doesn't have a default member, it will always return false.
One solution is to enumerate the items in the control array and compare the Index of each item.
Code:
Function CheckCtrlIndex(ctrlArray As Object, ByVal idx As Integer) As Boolean
Dim result As Boolean, ctrl As Control
For Each ctrl In ctrlArray
If ctrl.Index = idx Then
result = True
Exit For
End If
Next ctrl
CheckCtrlIndex = result
End Function
Hope it will help you :)
Re: check if element of a control array exists
Since the question was asked about 4 years ago I doubt the poster is still looking for an answer.
Re: check if element of a control array exists
What the ....????? :eek: I should have checked the OP, not the latest post :confused:
Re: check if element of a control array exists
Yes, my code really don't worked in some situations..
Hey, the topic has a very functional answer now that helps a lot.. :)
thanks man