[AccessD] Determine the number of columns in an array

Stuart McLachlan stuart at lexacorp.com.pg
Thu Jul 3 17:08:33 CDT 2003


On 3 Jul 2003 at 16:32, Mark H wrote:

> Hello all...
> 
> Sorry if this is a bit simple, but I'm in a bit of a rush to get out and
> wanted to ask before I go (climbing - yippeeee)...
> 
> Anyway, does anyone know how to determine the number of columns in an
> array? I am trying to write a bit of generic code and want to be able to
> pass the array and then figure out its width. I looked in the helps "See
> also" under Lbound and Ubound but found nothing...
> 

Not that simple :-( .  
This is the first  working solution I can think of, hopefully someone 
has something more elegant. It just keeps on trying to get the 
UBOUND() of successive dimensions until it errors out with a 
"Subscript out of range".

Function Dimensions(ArrayName As Variant) As Long
    Dim dimcount As Long
    Dim result As Long
    If Not IsArray(ArrayName) Then
        MsgBox "Not an Array"
        Exit Function
    End If
    On Error GoTo errtrap
    Do
     dimcount = dimcount + 1
     result = UBound(ArrayName, dimcount)
    Loop
    On Error GoTo 0
    Exit Function
errtrap:
    If Err = 9 Then
     Dimensions = dimcount - 1
     Exit Function
    Else
       MsgBox "Error " & Err.Number & ": " & Err.Description & " in 
Function Dimensions()"
    End If
End Function

Function test() As Long
    Dim testarray(3, 4, 5, 6, 7, 8, 9) As String
    test = Dimensions(testarray)
End Function



-- 
Lexacorp Ltd
http://www.lexacorp.com.pg
Information Technology Consultancy, Software Development,System 
Support.





More information about the AccessD mailing list