Window handle for controls - Crash when using EnumChildWindows.

Posted on 2006-05-07
Last Modified: 2012-06-21
I'm trying to use the Windows API function EnumChildWindows to determine the window handle of text box controls, but my code crashes at the point where I try to use the lngHwd or lParam parameters in the code below.  Can anyone see why this code should crash.   Or alternatively, is there an easier way of determining the window handle of an Access 2003 form control?

Declare Function EnumChildWindows Lib "user32" _
        (ByVal hWndParent As Long, _
         ByVal lpEnumFunc As Long, _
         ByVal lParam As Long) As Long

'This function is called during from a form control's event eg. CtlName_OnClick() using
'the syntax: Call test( Me.Form).
Function test(aForm As Form)
    Dim WindowHandle As Long
    WindowHandle = aForm.Hwnd
    Call EnumChildWindows(WindowHandle, AddressOf EnumChildProc, 0)
End Function

Function EnumChildProc(lngHwd As Long, lParam As Long) As Long
    MsgBox CStr(lngHwd)  '*** Crashes here ***
    EnumChildProc = 0
End Function

Question by:xerle
    LVL 46

    Accepted Solution

    Hi xerle,
    Check out
    Good Luck!

    LVL 1

    Author Comment

    Thanks Gary,

    The info at the link helps with the main task (to get a control's windows handle).   I'm still curious why the code crashes though, as I'd like to be able to use EnumChildWindows elsewhere.

    LVL 46

    Expert Comment

    Read up about the Addressof operator in the VBA help.
    Re the function...
    "It must represent a procedure in a standard module module in the project in which the call is made.:

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article is a continuation or rather an extension from Cascading Combos ( and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

    728 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

    24 Experts available now in Live!

    Get 1:1 Help Now