Results 1 to 13 of 13

Thread: Subscript out of range error [resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Subscript out of range error [resolved]

    In this code, if DomainQuery is empty I get this error:

    Run-time error '9':
    Subscript out of range


    Code:
    DomainQuery = Split(TempDomainQuery, " ")
    
    If Len(DomainQuery(0)) < 3 Or Len(DomainQuery(0)) > 63 Then
        MsgBox "Domain names must be between 3 and 63 characters long.", vbExclamation + vbOKOnly, "Domain Name Error"
        Exit Sub
    End If

    Any ideas what might be wrong?

    Simon
    Last edited by simonp; Apr 1st, 2004 at 05:07 AM.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    If you use Erase on a dynamic array you wont have any array index including zero. Do ReDim DomainQuery(0) so reinit the values.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265
    Hi!

    I don't 100% understand what you mean (but that's my lack of knowledge!)

    I've added ReDim DomainQuery(0) before the split and in between the split and the len code - the first still has the subscript error - the second seems to reset DomainQuery!

    Simon

  4. #4
    Addicted Member
    Join Date
    Jan 2003
    Posts
    163
    1) don't do any redim's on DomainQuery, especially after the split
    2) if TempDomainQuery contains no spaces then DomainQuery will be empty - you should check for this prior to checking the length, do this using ubound(DomainQuery)

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    You didn't indicate initially that it was from a Split(). Strangely, IsEmpty won't work. Use an error handler

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265
    Sorry leinad31 - my omission!

    Is it OK to just stick an "on error resume next" in - it seems to do the job? is that lazy!?

    Simon

  7. #7
    New Member
    Join Date
    Sep 2003
    Posts
    7
    Originally posted by john24
    2) if TempDomainQuery contains no spaces then DomainQuery will be empty - you should check for this prior to checking the length, do this using ubound(DomainQuery)
    Not true.

    If TempDomainQuery as no space, the you will have an single element in your array (the same value than TempDomainQuery)
    If TempDomainQuery is an empty string (value = ""), then the Split function will return an empty array

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265
    Originally posted by Idephix
    Not true.

    If TempDomainQuery as no space, the you will have an single element in your array (the same value than TempDomainQuery)
    If TempDomainQuery is an empty string (value = ""), then the Split function will return an empty array
    That's correct.

    but it's that empty string that's causing the subscript error - on error resume next seems to get round that niccely!

    Simon

  9. #9
    New Member
    Join Date
    Sep 2003
    Posts
    7
    I think it's a bad habit to use the On error Resume Next. If you forget to put back your old error trapping, you won't get any error.

    I would do something more like this:
    VB Code:
    1. DomainQuery = Split(TempDomainQuery, " ")
    2.  
    3.     If IsEmpty(DomainQuery) = True Then
    4.         MsgBox "Error message"
    5.         Exit Sub
    6.     ElseIf Len(DomainQuery(0)) < 3 Or Len(DomainQuery(0)) > 63 Then
    7.         MsgBox "Domain names must be between 3 and 63 characters long.", vbExclamation + vbOKOnly, "Domain Name Error"
    8.         Exit Sub
    9.     End If

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    *sigh*

    IsEmpty won't work cause it works only with variants. Split returns an array hence what your testing won't ever be a variant.

    Error handler will depend on the rest of the code in the procedure. Either on error goto and labels or on error resume next.
    Last edited by leinad31; Apr 1st, 2004 at 03:52 AM.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265
    Originally posted by leinad31
    IsEmpty won't work cause it works only with variants. Split returns an array hence what your testing won't ever be a variant.
    Correct! Have had to go with on error resume next

    Thanks all

    Simon

  12. #12
    Addicted Member Bregalad's Avatar
    Join Date
    Jul 2000
    Location
    Oslo,Norway
    Posts
    183
    hmm...

    Wouldn't a simple
    VB Code:
    1. If TempDomainQuery<>"" Then
    before the split do?

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    It's an ARRAY

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