Allen Browne's List Files Recursively

C

Chris

I am trying to implement the following code but cannot get it to compile. It
errors on the line lst.AddItem varItem with the message Compile Error: Method
or data member not found.

Public Function ListFiles(strPath As String, _
Optional strFileSpec As String, _
Optional bIncludeSubfolders As Boolean, _
Optional lst As ListBox)

On Error GoTo Err_Handler
'Purpose: List the files in the path.
'Arguments: strPath = the path to search.
' strFileSpec = "*.*" unless you specify differently.
' bIncludeSubfolders: If True, returns results from
subdirectories of strPath as well.
' lst: if you pass in a list box, items are added to it. If
not, files are listed to immediate window.
' The list box must have its Row Source Type property set
to Value List.
'Method: FilDir() adds items to a collection, calling itself
recursively for subfolders.

Dim colDirList As New Collection
Dim varItem As Variant

Call FillDir(colDirList, strPath, strFileSpec, bIncludeSubfolders)

'Add the files to a list box if one was passed in. Otherwise list to the
Immediate Window.
If lst Is Nothing Then

For Each varItem In colDirList

Debug.Print varItem

Next

Else

For Each varItem In colDirList

lst.AddItem varItem

Next

End If

Exit_Handler:
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_Handler
End Function
 
D

Douglas J. Steele

What version of Access are you using? I think Allen may be mistaken that
it'll work with Access 2000: my recollection is that AddItem was added until
Access 2002.
 
C

Chris

I am using a2k.

I did check and AddItem is not listed as a method for a list box in a2k so I
do believe you are correct.

I will just use his method of storing the file name and path to a table. It
will accomplish what I need.

Thanks for your help.
 
S

Stuart McCall

Chris said:
I am using a2k.

I did check and AddItem is not listed as a method for a list box in a2k so
I
do believe you are correct.

You could always roll your own (this is air code, but there's no reason for
it to fail):

Sub ListAddItem(lst As Access.ListBox, item As String)
With lst
If Len(.RowSource) > 0 Then .RowSource = .RowSource & ";"
.RowSource = .RowSource & item
End With
End Sub

So instead of:

lst.AddItem varItem

you could code:

ListAddItem lst, varItem
 
D

Douglas J. Steele

Stuart McCall said:
You could always roll your own (this is air code, but there's no reason
for it to fail):

Actually, the reason why it might fail is that they increased the maximum
length for the RowSource property when they added the .AddItem method. If he
tries to add too many files that way, it may well exceed the maximum
possible length for the property.

Worth a try, though.
 
S

Stuart McCall

Douglas J. Steele said:
Actually, the reason why it might fail is that they increased the maximum
length for the RowSource property when they added the .AddItem method. If
he tries to add too many files that way, it may well exceed the maximum
possible length for the property.

Ah. Didn't know that. Thanks.

Probably best to empty & fill a table to feed the listbox (or better yet a
continuous form).
 
A

Allen Browne

Thanks, Doug

Have corrected the link on the main tips page to show A2002 or later.

(The article stated A2002 or later for the list box, but of course the
compilation is an issue.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top