Solved

I can't even count

Posted on 2001-06-16
5
176 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 100 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month4 days, 15 hours left to enroll

636 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