Do you have a reference set to "Microsoft Scripting Runtime"? I has to set that reference to get the code to run.
I think there is a logic error in the FOR loop. "ctrlGetFileCount" (control on form?) will be (re)set to the dir count every loop if the extension is not ".db". If the file with ".db" happens is the 20th file of 40, when the 20 th file is reached, the count is reduced to 39. But on the next loop, the file count is back at 40 (objFolder.Files.Count).
Consider this:
Code:
Dim strFile As String
Dim ctrlGetFileCount As Integer
strFile = "C:\Maindir\Subdir1\Subdir2\"
If Len(Dir(strFile)) = 0 Then
ctrlGetFileCount = 0
Else
Dim fsoFile As New FileSystemObject
Dim objFolder As folder
Dim objFile As File
Set objFolder = fsoFile.GetFolder(strFile)
ctrlGetFileCount = objFolder.Files.Count
For Each objFile In objFolder.Files
If InStr(objFile.Name, ".db") Then
ctrlGetFileCount = objFolder.Files.Count - 1
End If
Next
Set objFolder = Nothing
End If
One other thing. Do you have the following two lines as the top two lines in the (every) module?
Code:
Option Compare Database
Option Explicit