|
-
Mar 31st, 2004, 12:56 PM
#1
Thread Starter
Hyperactive Member
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.
-
Mar 31st, 2004, 12:57 PM
#2
If you use Erase on a dynamic array you wont have any array index including zero. Do ReDim DomainQuery(0) so reinit the values.
-
Mar 31st, 2004, 01:05 PM
#3
Thread Starter
Hyperactive Member
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
-
Mar 31st, 2004, 01:16 PM
#4
Addicted Member
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)
-
Mar 31st, 2004, 01:16 PM
#5
You didn't indicate initially that it was from a Split(). Strangely, IsEmpty won't work. Use an error handler
-
Mar 31st, 2004, 01:42 PM
#6
Thread Starter
Hyperactive Member
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
-
Mar 31st, 2004, 03:11 PM
#7
New Member
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
-
Mar 31st, 2004, 03:13 PM
#8
Thread Starter
Hyperactive Member
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
-
Mar 31st, 2004, 03:22 PM
#9
New Member
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:
DomainQuery = Split(TempDomainQuery, " ")
If IsEmpty(DomainQuery) = True Then
MsgBox "Error message"
Exit Sub
ElseIf 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
-
Apr 1st, 2004, 03:45 AM
#10
*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.
-
Apr 1st, 2004, 03:48 AM
#11
Thread Starter
Hyperactive Member
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
-
Apr 1st, 2004, 05:03 AM
#12
Addicted Member
hmm...
Wouldn't a simple
VB Code:
If TempDomainQuery<>"" Then
before the split do?
-
Apr 1st, 2004, 07:53 AM
#13
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|