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

x
?
Solved

I can't even count

Posted on 2001-06-16
5
Medium Priority
?
179 Views
Last Modified: 2011-09-20
Hi, I'm trying to count the number of saved text files that have been saved with the date and time as name.
dlgCom.Filename = Format(Now, "YYYYMMDD HHMMSS")

This is the code I'm working on for the count.

Private Sub NumTrans()
    Dim i As Integer
    Dim ThisFolder As Integer
   
    Set ThisFolder = FSys.GetFolder("A:\Transact")
    Set NumTrans = ThisFolder.files
    For Each File In ThisFolder
            Call NumTrans(File.Count)
    Next
End Sub

I have tried a few variations but have not succeeded at all.
Many thanks for any help.
ned
0
Comment
Question by:Ned_Kelly
[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
  • 3
5 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 6198396
Are you trying to count the number of files in A:\Transact?
If it is then below is the code.
Note that you would need to include Microsoft Scripting Runtime in your Project->References. Below will return all files in yout folder.


Private Function AllFiles(ByVal FullPath As String) As Collection
'***************************************************
'PURPOSE: Returns all files in a folder using
'the FileSystemObject

'PARAMETER: FullPath = FullPath to folder for
'which you want all files

'RETURN VALUE: An array containing a list of
'all file names in FullPath, or a 1-element
'array with an empty string if FullPath
'does not exist or it has no files

'REQUIRES: Reference to Micrsoft Scripting
'          Runtime

'EXAMPLE:

'Dim sFiles() as string
'dim lCtr as long
'sFiles = AllFiles("C:\Windows\System")
'For lCtr = 0 to Ubound(sFiles)
'  Debug.Print sfiles(lctr)
'Next

'REMARKS:  The FileSystemObject does not
'Allow for the use of wild cards (e.g.,
'*.txt.)  If this is what you need, see
'http://wwww.freevbcode.com/ShowCode.asp?ID=1331
'************************************************

Dim oFs As New FileSystemObject
Dim oFolder As Folder
Dim oFile As File
Dim colFiles As New Collection

If oFs.FolderExists(FullPath) Then
    Set oFolder = oFs.GetFolder(FullPath)
 
    For Each oFile In oFolder.Files
      colFiles.Add oFile.Name
    Next
End If

Set AllFiles = colFiles
ErrHandler:
    Set oFs = Nothing
    Set oFolder = Nothing
    Set oFile = Nothing
End Function

Private Sub Form_Load()
    Dim colFiles As Collection
    Dim strFile As Variant
   
    Set colFiles = AllFiles("A:\Transact")
    For Each strFile In colFiles
        MsgBox strFile
    Next
   
    Set colFiles = Nothing
End Sub

hongjun
0
 
LVL 33

Accepted Solution

by:
hongjun earned 400 total points
ID: 6198405
Below will return the number of files in your folder. Note the modification done by me.

Private Function AllFiles(ByVal FullPath As String) As Integer
    Dim oFs As New FileSystemObject
    Dim oFolder As Folder
    Dim oFile As File
    Dim intCount As Integer
   
    intCount = 0
    If oFs.FolderExists(FullPath) Then
        Set oFolder = oFs.GetFolder(FullPath)
     
        For Each oFile In oFolder.Files
          intCount = intCount + 1
        Next
    End If
   
    AllFiles = intCount
ErrHandler:
    Set oFs = Nothing
    Set oFolder = Nothing
    Set oFile = Nothing
End Function

Private Sub Form_Load()
    MsgBox AllFiles("A:\Transact") & " files returned"
End Sub

hongjun
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 6198606
fyi ned kelly,

If you don't want to add the reference the FileSystemObject (Microsoft Scripting Runtime) you can do it all programmatically....



Private Function AllFiles(ByVal FullPath As String) As Integer
    Dim oFs As Object
    Set oFs = CreateObject("Scripting.FileSystemObject")
    Dim oFolder As Object
    Dim oFile As Object
    Dim intCount As Integer
   
   intCount = 0
   If oFs.FolderExists(FullPath) Then
       Set oFolder = oFs.GetFolder(FullPath)
   
       For Each oFile In oFolder.Files
         intCount = intCount + 1
       Next
   End If
   
   AllFiles = intCount
ErrHandler:
   Set oFs = Nothing
   Set oFolder = Nothing
   Set oFile = Nothing
End Function

Private Sub Form_Load()
   MsgBox AllFiles("A:\Transact") & " files returned"
End Sub

0
 

Author Comment

by:Ned_Kelly
ID: 6199122
Hi Honqjun, thanks for your help, it works well.
Ned
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6199292
wileecoy suggests using runtime setting references and that is another alternative. But for a beginner, I would suggest you adding references so that when you do a ., you will get the available methods, etc. This would be more helpful that way.

Glad that I can help here.

hongjun
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

705 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