Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Test to determine if variant is an array

Posted on 2012-04-07
6
Medium Priority
?
369 Views
Last Modified: 2012-04-09
I am working in Access 2010.
I need to determine if a variant is an array. I put together the following function:

Public Function IsArray(varIn As Variant) As Boolean
   Dim i As Integer
   On Error GoTo ExitFunction
   i = UBound(varIn)
   IsArray = True
ExitFunction:
   On Error GoTo 0
End Function

It appears to work. Is there some unforeseen circumstance I'm not thinking of where this procedure wouldn't work?
Is there a better way to determine if a variant is an array?
0
Comment
Question by:shambalad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 37820684
IsArray exists already as a native function of VBA. But you may need this test:
Public Function IsReDimmed(ByRef varArrayToCheck) As Boolean

' Checks a dynamic array to see if it has been ReDim'med.
' This is to prevent errors before applying LBound() or UBound().
'
' Returns False if varArrayToCheck is dynamic and
' never has been ReDim'med or just has been erased.
'
' 2001-08-11. Cactus Data ApS, CPH. Gustav Brock.

  Dim booIsReDimmed As Boolean
  
  On Error GoTo Err_IsReDimmed
   
  If IsArray(varArrayToCheck) = True Then
    ' Check if varArrayToCheck is ReDim'med.
    ' LBound() will fail if varArrayToCheck not has been ReDim'med.
    booIsReDimmed = LBound(varArrayToCheck) Imp True
    ' No error raised; varArrayToCheck is ReDim'med.
  Else
    ' varArrayToCheck is not an array.
  End If
  
  IsReDimmed = booIsReDimmed
  
Exit_IsReDimmed:
  Exit Function
  
Err_IsReDimmed:
  Select Case Err
    Case 9
      ' Subscript (array index) out of range.
      ' varArrayToCheck is dynamic and not ReDim'med.
    Case Else
      ' Other error.
  End Select
    ' Exit function returning False.
  Resume Exit_IsReDimmed

End Function

Open in new window

/gustav
0
 
LVL 7

Author Comment

by:shambalad
ID: 37820707
>  IsArray exists already as a native function of VBA
Yeah, it's 2:46AM on a Sunday morning and I probably should shut this computer down and go to sleep. It's a good thing I don't take myself too seriously...
Thanks for the IsReDimmed function. It will be useful.
Good night.
Todd
0
 
LVL 7

Author Closing Comment

by:shambalad
ID: 37820708
Thanks again.
th
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Expert Comment

by:Gustav Brock
ID: 37820984
You are welcome!

/gustav
0
 
LVL 7

Author Comment

by:shambalad
ID: 37822348
Gustav -
I must say your perspective never ceases to intrigue me. I have been looking over the IsReDimmed function you posted last night and I have a question about one of the lines of code:

booIsReDimmed = LBound(varArrayToCheck) Imp True

I'm trying to discern your rationale behind adding the 'Imp True' piece. If I understand correctly how the Imp function works, when the second expression is True, the result is always True. In other words, this line of code should always return True. Would it be correct to say that the intent here is to ensure a valid 'True' value is always returned (as opposed to simply some non-zero result) so long as the LBound function does not kick out an exception? Or is there more to this than meets the eye?
Thanks,
Todd
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 37822605
Good question. I forgot myself, and Imp is not carried forward to the never languages.

The catch is Null Imp True:

True Imp True = True
False Imp True = True
Null Imp True = True

But that is not important here. The goal is that if LBound does not fail, the expression should in any case - no matter if LBound returns zero (False) or some other number (True) return True.

The rationale behind the function is, that if a dynamic array has not been ReDim'med or has been erased, functions like LBound() and UBound() will fail. This is so basic that one could believe code could be found all over the net, but no. On the contrary, the question is raised again and again in the user groups how to check this.

There is - as far as I can see - no way to find out except to apply xBound() on the array and see if it fails. Very primitive.

Note that the array to test is not passed as an array but as a variant. This is to allow for the array to be declared as whatever type you like. Doing so, you will have to check if the variable is an array but that is very easy to do.


Another method I just looked up is suggested by A. D. Tejpal using the IsError function (which I never have used):
Public Function Fn_IsArrayInitialized(ByVal varArray As Variant) As Boolean
    On Error Resume Next
    Fn_IsArrayInitialized = Not IsError(UBound(varArray) >= 0)
End Function

Open in new window

It seems to do exactly the same as mine but a bit simpler.

/gustav
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question