Results 1 to 9 of 9

Thread: How to find out if control array element exists

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Location
    Meenahsohtah
    Posts
    22

    How to find out if control array element exists

    I've got a program that creates and deletes control array elements at runtime. I'm relatively new to VB, so I don't know how to check to see if an element exists. I want something along this line though


    If controlArray(x) does not exist
    Load controlArray(x)
    else
    Unload controlArray(x)

    I tried isEmpty and isNull, but those seemed to return false regardless of whether the control array element was set or not

  2. #2
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378
    Check the UBound(YourArrayName), this will return the size of the array. But if you want to check the content of the array itself you should loop on it.
    Bear in mind though that UBound of a dynamic array that has not yet been resized would raise a runtime error. So maybe before you check for UBound you could check for IsArray(YourArrayName).
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Location
    Meenahsohtah
    Posts
    22
    Good stuff to know, but I'm not sure it applies to my situation. The size of the array seems to be irrelevant to me, and I know that it's always going to be an array, since the program starts with ControlArray(0) already in the form..

    I want to be able to pick an arbitrary number and see if that index is an element in the control array. If it's not, load it into the form. if it's already loaded, do nothing.

  4. #4

    Unhappy List of indexes not continuous

    The Ubound technique only works where you have a control array that has no gaps in. I need to have gaps in my control array which means that certain elements do not exist. for instance, my control array has 118 textboxes but element '2' does not exist. is there any way to check if the actual element exists?

    Please Help ASAP.
    |3 |_| >< *
    _____________________
    ScholesMAFIA.co.uk

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    why not just error trap error '340'?

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I second kleinma use error trapping, you can even right a function to test it if you want:

    VB Code:
    1. Public Function IsInArray(CtrlArray as Variant,Index as integer) as boolean
    2.    On Error Resume Next
    3.    Dim x As String
    4.    x=CtrlArray(Index).Name
    5.    If Err.Number=0 Then IsInArray=TRUE
    6. End Function

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by Edneeis
    I second kleinma use error trapping, you can even right a function to test it if you want:

    VB Code:
    1. Public Function IsInArray(CtrlArray as Variant,Index as integer) as boolean
    2.    On Error Resume Next
    3.    Dim x As String
    4.    x=CtrlArray(Index).Name
    5.    If Err.Number=0 Then IsInArray=TRUE
    6. End Function
    using error trapping is greatly overlooked... people don't like to do it because it has the word "ERROR" in it.. but it is actually sometimes the best way to code things...

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I agree, they need to focus on the 'trapping' part instead of the 'error' part or something.

  9. #9
    Cheers guys, that works fine now.

    I did try trapping but for some reason it failed before.
    |3 |_| >< *
    _____________________
    ScholesMAFIA.co.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width