Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrays / IsEmpty (XP/O2003/Word)

    Hi,

    Here's a quick one, but I can't locate an answer, so apologies if this one seems stupid.

    What's the easiest way to check whether an array contains data or not...?
    Initially I tried with "IsEmpty" as it applies to variants but VBE still complains...
    Thought also about checking Ubound, but seems somehow odd...

    Code snippet:
    .....
    If strCompanyID = "" Then DocProp_Get ("cdpCompanyID1")
    If IsEmpty(arrCompanyDetails) Then
    arrCompanyDetails = GetCompanyDetails(strCompanyID, iLANGID1)
    End if
    ......

    Ooops - forgot to mention that arrCompanyDetails is a custom defined type that gets filled when GetCompanyDetails are called
    (part of it's Type definition below)

    Type BasicInfo 'arrCompanyDetails get filled with these types
    Company_ID As String
    Company_Name As String
    Country_Name As String
    Sector_Name As String
    Currency_Basis As String
    Currency_Invoice As String
    ......
    End type



    Better suggestions?

    ThX
    Bests,
    RD


    PS: Wish there were a knob on the TV to turn up the intelligence. There's a knob called "brightness," but that doesn't work

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    You could loop through the elements of the array and test if they're all empty.

  3. #3
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    An array can't really be Empty. That is, if IsArray(arrCompanyDetails) is True, IsEmpty(arrCompanyDetails) will necessarily be False.

    If you initially declare arrCompanyDetails without parentheses, like this:
    <pre>Dim arrCompanyDetails As Variant</pre>

    then arrCompanyDetails will (at that point) be a simple Variant (not yet an array), and Variants initialize to Empty, so IsEmpty will be True and IsArray will be False.

    If, on the other hand, you initially declare arrCompanyDetails with parentheses, like this:
    <pre>Dim arrCompanyDetails() As BasicInfo</pre>

    then arrCompanyDetails will (at that point) be an undimensioned array, so IsArray will be True and (perhaps unfortunately for you) IsEmpty will be False (even though the array will certainly be "empty" for most purposes).

    Testing whether something is an undimensioned array is a little tricky. The best way I know is to use a function that triggers an error and detects that error. Here's a sample:
    <pre>Function IsUndimensionedArray(aTarget As Variant) As Boolean

    Dim x As Long

    If IsArray(aTarget) = True Then
    On Error Resume Next
    x = LBound(aTarget)
    If Err = 9 Then
    IsUndimensionedArray = True
    End If
    End If

    End Function</pre>


    Finally, though, if you dimension your array when you first declare it (or, using ReDim, at any time before you test it for "emptiness"), then you can use Hans's approach of looping through the elements and seeing if any of them is non-empty.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Arrays / IsEmpty (XP/O2003/Word)

    I don't know whether this applies in Word 2003 VBA, but in VB.Net, you can check the Length property of an array to determine how many items it contains.
    Charlotte

  5. #5
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    Your question reminded me of a similar thread from last year, <post#=389340>post 389340</post#>.

    Extrapolating on that posting ...
    <pre>Sub SeeIfArrayIsEmpty()
    Dim arr() As String
    '
    ' Is this array empty? Find out:
    MsgBox Not CBool(Len(Join(arr))) ' --> True
    '
    ReDim arr(1)
    arr(1) = vbNullString
    ' Now is the array empty?
    MsgBox Not CBool(Len(Join(arr))) ' --> False
    '
    ' And finally ...
    arr(1) = "foo"
    MsgBox Not CBool(Len(Join(arr))) ' --> False
    End Sub

    ' A more useful generic test function would be:
    Function IsArrayEmpty(vArr As Variant) As Boolean
    IsArrayEmpty = Not CBool(Len(Join(vArr)))
    End Function
    </pre>



    HTH,

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    (How quickly she forgets...)

    A spokesperson for the poor VB6 arrays has asked me to remind you that none of them have any property at all.

  7. #7
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    Shoot, I was a (somewhat hapless) participant in that thread you pointed to, and I somehow missed the import of that post with the Join trick. Good stuff.

    I note, however, (1) that it doesn't work (an error is triggered) if the subject of the test is an empty variant (so it may make sense, in some situations, to precede it with an IsArray test), and (2) that, as your post showed, it will return False as long as the array has any elements, even if all of those elements are themselves empty (so you'd follow it with Hans's loop through the elements if that was appropriate).

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Arrays / IsEmpty (XP/O2003/Word)

    In VBA an array with an element is NOT empty, it simply contains an empty element. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  9. #9
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Arrays / IsEmpty (XP/O2003/Word)

    As quickly as possible! <img src=/S/sneaky.gif border=0 alt=sneaky width=15 height=15>
    Charlotte

  10. #10
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    FOLLOW-UP DISCOVERY: If you specify an empty string as the delimiter of the Join function -- i.e., CBool(Len(Join(arr, vbNullString))) -- your function seems to do a pretty good job of returning True not only if the array is undimensioned, but also if it's dimensioned but all its elements are empty.

  11. #11
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    FOLLOW-UP FLY IN OINTMENT: If you try to use the Join function on an array of user-defined types, like Henrik's, VB complains that only udt's in public object modules can be coerced blah blah or passed blah blah.

  12. #12
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    That makes sense. The default delimiter of Join is a space, which is why even if it's run on an empty array, its result has a non-zero length.

  13. #13
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    That's true, but you can't use IsArray() or IsEmpty() on an array of UDTs either.

    I've never had much use for UDTs, though I'm sure they have their place. I'd opt for a class or a collection in this case, though I'm not sure if that'd suit Henrik's needs.

    <pre>Function NewCompanyDetails( _
    Company_ID As String, _
    Company_Name As String, _
    Country_Name As String, _
    Sector_Name As String, _
    Currency_Basis As String, _
    Currency_Invoice As String) _
    As Collection
    Dim col As Collection
    Set col = New Collection
    col.Add Key:="Company_ID", Item:=Company_ID
    col.Add Key:="Company_Name", Item:=Company_Name
    col.Add Key:="Country_Name", Item:=Country_Name
    col.Add Key:="Sector_Name", Item:=Sector_Name
    col.Add Key:="Currency_Basis", Item:=Currency_Basis
    col.Add Key:="Currency_Invoice", Item:=Currency_Invoice
    Set NewCompanyDetails = col
    End Function
    '
    Sub UseCollection()
    Dim colAllCompanyDetails As Collection
    Dim k As Long
    Set colAllCompanyDetails = New Collection

    ' see if collection is empty:
    MsgBox Not CBool(colAllCompanyDetails.count)


    colAllCompanyDetails.Add _
    Item:=NewCompanyDetails( _
    Company_ID:="12345", _
    Company_Name:="widgets, inc", _
    Country_Name:="US", _
    Sector_Name:="Northeast", _
    Currency_Basis:="Dollar", _
    Currency_Invoice:="no idea what this is")

    colAllCompanyDetails.Add _
    Item:=NewCompanyDetails( _
    Company_ID:="99999", _
    Company_Name:="Foobar, Inc", _
    Country_Name:="Canada", _
    Sector_Name:="West", _
    Currency_Basis:="Dollar", _
    Currency_Invoice:="no idea what this is")

    ' Now is it empty?:
    MsgBox Not CBool(colAllCompanyDetails.count)

    ' Also can iterate easily:
    For k = 1 To colAllCompanyDetails.count
    MsgBox "Company Name is: " + colAllCompanyDetails(k)("Company_Name")
    Next k
    End Sub
    </pre>


  14. #14
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    <hr>That's true, but you can't use IsArray() or IsEmpty() on an array of UDTs either.<hr>
    Ouch. Right you are. (UDTs are gotcha-heaven, aren't they?) It occurs to me (finally) that the UDTs may have been what triggered the error that prompted Henrik to post in the first place -- which would mean your last post was the answer to the question he didn't know he was asking.

    Whatever happened to Henrik, anyway? <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  15. #15
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Arrays / IsEmpty (XP/O2003/Word)

    Hiyah, still following this thread, that has become unexpectedly interesting (grin)
    I can see that Andrew also came across the error message of UDT's blah blah blah - that was the one that triggered my initial Q

    Lots of thanks for all your inputs - and yeah I agree a new collection seems to be the best way around this.
    ...I was only hoping that I could avoid it as it implies a lot of code rewriting - but I guess that's the name of the game...
    Bests,
    RD


    PS: Wish there were a knob on the TV to turn up the intelligence. There's a knob called "brightness," but that doesn't work

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •