Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing collection element.

Posted on 2006-12-01
12
Medium Priority
?
225 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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

885 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