Solved

Accessing collection element.

Posted on 2006-12-01
12
210 Views
Last Modified: 2010-04-30
Hi there,

***********************
        Dim fso As Object
        Dim fld As Object
        Dim subfs As Object
        Dim subf As Object
       
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fld = fso.GetFolder(path)
        Set subfs = fld.SubFolders
        Set subf = subfs.Item(2)
***********************

I get an error on the last line, "Invalid procedure call or argument"...
0
Comment
Question by:Unimatrix_001
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 165 total points
ID: 18059049
You have to access it via the key name, not the index, so
set  subf = subf.Item("foldername")
It seems a little pointless, as most of the time you are creating the collection to get the folder names.

You can still iterate through the collection with a for ... each statement

  For Each subf In subfs
     MsgBox subf.Name
  Next
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18059057
What I'm after is getting hold of the last folder in that collection, is there a tidy way to do it without a for loop?
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 170 total points
ID: 18059069
>What I'm after is getting hold of the last folder in that collection, is there a tidy way to do it without a for loop?
the "last" means the list needs to be ordered. however, the collections are not ordered in a guaranteed way, so you need
1) to specify the "order"
2) order by that property
3) get the last from there.
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18059071
Without knowing the key name, not that I know of.

I agree with you, it seems kind of dumb that you can't get it by index and having to know the key name to reference the object (why else would you be iterationg through in the first place, if not to get the name!).
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18059073
angelIII: Could you expand on that please, are those methods I can use etc... ?
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18059074
"Without knowing the key name, not that I know of."
Hmm, okay. Although yes, seems incredibly silly not getting an element by index!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Assisted Solution

by:Dany Balian
Dany Balian earned 165 total points
ID: 18059242
try this

        Dim fso As Object
        Dim fld As Object
        Dim subfs As Object
        Dim subf As Object, subfol As Object
       
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fld = fso.GetFolder("C:\")
        Set subfs = fld.SubFolders
        For Each subf In subfs
            Set subfol = subf
        Next
       
        MsgBox subfol.Name  '<< this will give u the last item in the list...


hope this helps..

dan

0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18059253
mrdany: Thanks, although what I'm after is:

"is there a tidy way to do it without a for loop?"

Although, I'll wait for AngelIII to reply, and then I'll probably split the points. :)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18059281
>"is there a tidy way to do it without a for loop?"
no.
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 18059790
sorry unimatrix, i didnt read the comments.. i just read the main question and answered...
my bad!!
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18060346
Hrm, okay... Thanks any everybody for your contributions. Points split. :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18060348
mrdany: Np mate. ;)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now