
hWnd Property Help Example gives Run-time error 2475 in Access but not in VB6
Quote:
> Why am I getting Run-time error 2475 when I try this
> Access Help Example in Access? I don't get this run-time
> error in Visual Basic 6 Professional.
> When I tried the following Access Help Example in either
> an Access form or a Visual Basic form, I got the "Declare
> statements not allowed as Public members of object
> modules" message. So I added the Private keyword.
> After changing "Declare" to "Private Declare" I got "Run-
> time error '2475': You entered an expression that requires
> a form to be the active window" when running the form in
> Access. To try this in a Visual Basic 6 Professional form,
> I replaced DoCmd.Maximize with WindowState = 2 and that
> works.
> The following is copied verbatum from Access Help:
> -------------------------------------------------
> The following example uses the hWnd property with the
> Windows API IsZoomed function to determine if a window is
> maximized.
> ' Enter on single line in Declarations section of Module
> window.
> Declare Function IsZoomed Lib "user32" (ByVal hWnd As
> Long) As Long
> Sub Form_Activate()
> Dim intWindowHandle As Long
> intWindowHandle = Screen.ActiveForm.hWnd
> If Not IsZoomed(intWindowHandle) Then
> DoCmd.Maximize
> End If
> End Sub
> I also got Run-time error 2475 in Access with this code:
> -------------------------------------------------------
> Private Declare Function IsZoomed Lib "user32" (ByVal hWnd
> As Long) As Long
> Private blnWasMaximized As Boolean
> Sub Form_Activate()
> Dim lngWindowHandle As Long
> lngWindowHandle = Screen.ActiveForm.hWnd
> ' The IsZoomed function returns True if a window is
> maximized
> If IsZoomed(lngWindowHandle) Then
> blnWasMaximized = True
> DoCmd.Restore
> End If
> End Sub
> Private Sub Form_Deactivate()
> If blnWasMaximized = True Then DoCmd.Maximize
> End Sub
First, you should put the declaration of IsZoomed into a standard
module, not in the form's module. Then you can declare it as Public and
use it in multiple forms.
Second, it appears that when you first open the form, its Activate event
fires before Screen.ActiveForm has been set. My experiments show that
if you switch the focus away from the form and then back to it, the code
works. You can get around this problem by dropping the use of
Screen.ActiveForm completely, and using the form's hWnd property
directly instead:
Sub Form_Activate()
Dim lngWindowHandle As Long
lngWindowHandle = Me.hWnd
If Not IsZoomed(lngWindowHandle) Then
DoCmd.Maximize
End If
End Sub
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)