• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Window handle for controls - Crash when using EnumChildWindows.

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

  • 2
1 Solution
Hi xerle,
Check out http://www.mvps.org/access/api/api0027.htm
Good Luck!

xerleAuthor Commented:
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.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now