Is an index in a Control array?

eric0213
eric0213 used Ask the Experts™
on
Say you have a textbox control array:
  Text1(0)
  Text1(1)
  Text1(3)

I need to know if a certain number is in the array.  In this case, it would give
  x(0) = True
  x(2) = False

I'd rather not check for error 340 (control array object doesn't exist).

Thanks,
Eric
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Consultant
Top Expert 2016
Commented:
you need to trap the error to know if a particular index exist or not.
Alternatively you could maintain a string with delimeters whenever you create the textboxes (don't forget to put those that are created in design mode into the string).  Put the indexes in a string like "1,2,4,8" then do an instring on the string looking for number and comma (i.e. "3," would produce a false where "2," would produce true).

But what's the point?  trapping for 340 is the simplest method.

Commented:
Checking for an error is quickest, but here's another way:

dim ctl as Control
intIndexToFind = -1
for each ctl in Me.Controls("Text1")
  if ctl.Index = intIndexToFind then
    intIndexToFind = ctl.Index
    exit for
  endif
next ctl

if intIndexToFind < 0 then
  msgbox "Index not found"
else
  msgbox "Index found"
endif
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I was looking more for some canned function to test, like IsObject or IsNull or Text1(4) Is Nothing or something like that.

I guess I might have to just trap the error.  Thanks.

Commented:
private function IsValidIndex(ControlArrayName as String) as Boolean

dim ctl as Control
intIndexToFind = -1
for each ctl in Me.Controls(ControlArrayName)
 if ctl.Index = intIndexToFind then
   intIndexToFind = ctl.Index
   exit for
 endif
next ctl

if intIndexToFind < 0 then
 IsValidIndex = false
else
 IsValidIndex = true
endif  
end function

Commented:
ooops..gotta also supply the index number:

private function IsValidIndex(ControlArrayName as String, IndexToCheck as integer) as Boolean

dim ctl as Control
dim bIndexFoundCheck as boolean

bIndexFoundCheck=false

for each ctl in Me.Controls(ControlArrayName)
if ctl.Index = IndexToCheck then
  bIndexFoundCheck = true
  exit for
endif
next ctl

IsValidIndex = bIndexFoundCheck
end function

Commented:
use :
If VarType(Text1(i)) <> vbObject Then
  MessageBox "The control exists"
Else
  MessageBox "The control does not exists"
End If
Hey Hey!  I Like it!  Good job tandrei.  Just change MessageBox to Msgbox and you're all set. Give tandrei the points!  Very nice.

Commented:
you are right twalgrave, I work in C# now and there it's MessageBox instead of MsgBox,
thanks for the support!

Author

Commented:
Tandrei,
That's close what I was looking for, but it doesn't work.  Well, it kind of does.  If the object doesn't exist, it'll return vbObject; if the object does exist, it'll return vbString -- the default property for the TextBox object.

The other options require loops or string checks which just seem a little processor intensive.

I think I'm going to have to just trap the error.

Thanks guys

Commented:
Actually, the computer has to loop either way: either through your code or its own.  When the computer checks for an index, it has to search the list of indices for the object so that it can be loaded into usable memory.  Obviously these aer happening at a different level, so they should be more efficient, but not necessarily.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial