Checking if myArray(x) is actually set (1 Viewer)

HairyArse

Registered User.
Local time
Today, 23:34
Joined
Mar 31, 2005
Messages
92
Hello,

I'm working on a form which has multiple variables passed to it via OpenArgs with a pipe (|) separating each variable.

My code captures the string and creates an array containing each element and this works just fine.

However.

This particular form can be opened from multiple different locations each of which passes a different number of variables to the form. For example one form passes 2, another form passes 3.

Each array element is assigned to a string variable, but when I try and assign the third string variable, if there is no third element in the array I receive an error message about the subscript being out of range.

How do I actually check that an array element exists before assigning it to a variable?

I've tried:

If myArray(2) = ""
etc...

If IsNull myArray(2)
etc...

If myArray(2) IS NOTHING
etc...

But none of them seem to work
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:34
Joined
Sep 12, 2006
Messages
15,653
something like this?

myarray = split(openargs,"|")
TopElement = ubound(myarray) 'zero based

if ubound(myarray)>=2 then
etc
 

vbaInet

AWF VIP
Local time
Today, 23:34
Joined
Jan 22, 2010
Messages
26,374
something like this?

myarray = split(openargs,"|")
TopElement = ubound(myarray) 'zero based

if ubound(myarray)>=2 then
etc
Else, Redim the array and add an extra index.
 

HairyArse

Registered User.
Local time
Today, 23:34
Joined
Mar 31, 2005
Messages
92
Thanks for your help Gemma. Your suggestion sorted it. :)

I went with this in the end:

Dim myArray As Variant
Dim myVar0 as string, myVar1 as string, myVar2 as string

If Not IsNull(Me.OpenArgs) Then
myArray = Split(Me.OpenArgs, "|")
End If

If UBound(myArray) = 2 Then
myVar2= myArray(2)
myVar1= myArray(1)
myVar0 = myArray(0)
End If

If UBound(myArray) = 1 Then
myVar1 = myArray(1)
myVar0 = myArray(0)
End If

If UBound(myArray) = 0 Then
myVar0 = myArray(0)
End If
 

Users who are viewing this thread

Top Bottom