EnumWindowProc - MSAccess Crashes

craigachan

Registered User.
Local time
Yesterday, 16:03
Joined
Nov 9, 2007
Messages
285
I have the following code that I used to find an open window so that I won't open it again. I have tracked down somewhere close to what makes MSAccess shut down when the code runs. When it shuts down, no error comes up even though I have error handling. It just shuts down.

I placed, in the 'EnumWindowProcExit:' section a msgbox, and when i = 0 and then recycles thru the code, it crashes. I don't know if this has anything to do with it or not, but it is consistent. Can anyone identify my problem?

Code:
Public Function EnumWindowProc(ByVal hwnd As Long, lParam As FindWindowParameters, Optional i As Long) As Long
   'GROUP CODE: FnFindWindowLike, EnumWindowProc, TrimNull
   On Error GoTo EnumWindowProcErr
   Dim strWindowTitle As String

   strWindowTitle = Space(260)
   Call GetWindowText(hwnd, strWindowTitle, 260)
   strWindowTitle = TrimNull(strWindowTitle) ' Remove extra null terminator
                                          
   If strWindowTitle Like lParam.strTitle Then
   
        lParam.hwnd = hwnd 'Store the result for later.
        EnumWindowProc = 0 'This will stop enumerating more windows
   Else
        EnumWindowProc = 1
   End If
                                           
EnumWindowProcExit:
    MsgBox "'" & strWindowTitle & "' - " & i
    i = i + 1
    Exit Function
EnumWindowProcErr:
    MsgBox "WindowAppPublic-EnumWindowProc: " & Err.Number & " - " & Err.Description
    Resume EnumWindowProcExit
    
End Function

Supporting Code
Code:
Public Function TrimNull(strNullTerminatedString As String)
    'GROUP CODE: FnFindWindowLike, EnumWindowProc, TrimNull
    On Error GoTo TrimNullErr
    Dim lngPos As Long

    'Remove unnecessary null terminator
    lngPos = InStr(strNullTerminatedString, Chr$(0))
   
    If lngPos Then
        TrimNull = Left$(strNullTerminatedString, lngPos - 1)
    Else
        TrimNull = strNullTerminatedString
    End If
TrimNullExit:
    Exit Function
TrimNullErr:
    MsgBox "WindowAppPublic-TrimNull: " & Err.Number & " - " & Err.Description
    Resume TrimNullExit
End Function
 
What happen if you remove the i?
For what purpose is i, (you haven't show the code where you use it)!
Can't you put the i in the Type declaration for FindWindowParameters?
 
Hi JHB, It was crashing before I put the 'i" in and I put it in to keep track of the number of times it cycled thru successfully until it crashed (my effort to try to find when it crashed) I also have to mention that it crashes only during runtime. I'll see if puting 'i' in the Type declarations helps.
 
I placed the 'i' in the Type but it did not make a difference. Any other ideas?
 

Users who are viewing this thread

Back
Top Bottom