Counting Outside of Access

W

williamr

I have some images in a directory that I view from within my Access program.
Is it possible for me to create a text box and count the number of images in
that directory?

Thanks
 
B

bob

The code below will get you the number of jpg files in a directory (for example).

Dim FileName As String
Dim Count As Long

Count = 0
FileName = Dir("C:\temp\Photos\*.jpg")

Do While Len(FileName) > 0
Count = Count + 1
FileName = Dir
Loop

MsgBox Count

It's a bit long-winded - anyone suggest a more elegant way?
 
W

Wayne Morgan

Yes, you can use the Dir() function in VB to loop through all of the images
and keep track of the number of files you find.

Example:
Dim intCount As Long, strFileName As String
strFileName = Dir("<path>\*.jpg")
Do Until strFileName = ""
intCount = intCount + 1
strFileName = Dir()
Loop
Debug.Print intCount & " files were found."
 
J

John Nurick

A function like this should do the job:

Public Function CountFilesInFolder(Filespec As String) As Long
Dim Count As Long
Dim Found As Long

Found = Len(Dir(Filespec))
Do While Found > 0
Count = Count + 1
Found = Len(Dir())
Loop
CountFilesInFolder = Count
End Function

You can use it in VBA code or in the controlsource of a textbox
 
O

open a adobe file from a command button

Hi. I used Bob's code first because I understood it better but I get the
number "7" as a count when there are 63 .tif files in the directory. There
are seven files that have seven characters in the name "123.tif", but don't
know enough to know why!!

Help Please!!!
 
J

John Nurick

Bob's code works for me. Is it possible that some of your files are
hidden? - or are you also trying to count files in subfolders?
 
B

bob

This should give you the same results as typing dir with the same parameter on the command line. Try doing
that and see what results you get.

You could also put a breakpoint in the loop and examine which files it finds - maybe then you can see why
it does not return the files you are expecting.

I'm sure it has nothing to do with the number of characters in the filename!
 
O

open a adobe file from a command button

I got it to work. I used Bob's code but I tried all 3. Thank You ALL!!!!
 
Top