Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing collection element.

Posted on 2006-12-01
12
Medium Priority
?
221 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
[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
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 660 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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 680 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
LVL 11

Assisted Solution

by:Dany Balian
Dany Balian earned 660 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 143

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

688 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