Solved

Accessing collection element.

Posted on 2006-12-01
12
211 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

914 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

20 Experts available now in Live!

Get 1:1 Help Now