Solved

I can't even count

Posted on 2001-06-16
5
164 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
  • 3
5 Comments
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Honqjun, thanks for your help, it works well.
Ned
0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA Check for Character 9 69
VBA to format 3 77
Window placement 17 64
Runtime error 76: path not found error VB6 program on Windows 7 3 174
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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

9 Experts available now in Live!

Get 1:1 Help Now