Avatar of gdunn59
gdunn59

asked on 

Why is Access Closing Out Each Time it Gets to a Certain Line of VBA Code

I have the following code, and whenever it gets to Line 22 of the Code it closes Access.

Function FindFolders(strStartDir As String, strResults() As String) As Long 'NOTE: this is non recursive, so the results will only show for the given folder
  On Error GoTo ErrHandler
  Dim wfd As WIN32_FIND_DATA
  Dim nFind As Long
  Dim strDirectoryName As String
  ReDim strResults(0) '0 will not be used
  
  If InStr(1, strStartDir, "*") < 1 Then
    If Right(strStartDir, 1) <> "\" Then strStartDir = strStartDir & "\"
    strStartDir = strStartDir & "*"
  End If

  nFind = FindFirstFile(strStartDir, wfd)   'api call
  If (wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) > 0 Then     'if this is a directory
     strDirectoryName = Left(wfd.cFileName, InStr(1, wfd.cFileName, Chr(0)) - 1)
     If strDirectoryName <> "." And strDirectoryName <> ".." Then
       ReDim Preserve strResults(UBound(strResults) + 1)
       strResults(UBound(strResults)) = strDirectoryName
     End If
  End If
  Do While FindNextFile(nFind, wfd)
    If (wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) > 0 Then  'if this is a directory
      strDirectoryName = Left(wfd.cFileName, InStr(1, wfd.cFileName, Chr(0)) - 1)
      If strDirectoryName <> "." And strDirectoryName <> ".." Then
        ReDim Preserve strResults(UBound(strResults) + 1)
        strResults(UBound(strResults)) = strDirectoryName
      End If
    End If
  Loop
  
ErrHandler:
  FindClose nFind
  FindFolders = ErrorHandler(err, "FindFolders")
End Function

Open in new window


Here are the declarations:
Private Declare PtrSafe Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare PtrSafe Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare PtrSafe Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Open in new window


Thanks,
gdunn59
VBAMicrosoft Access

Avatar of undefined
Last Comment
gdunn59

8/22/2022 - Mon