Form_Open VBA 代码不起作用
Posted
技术标签:
【中文标题】Form_Open VBA 代码不起作用【英文标题】:Form_Open VBA Code not working 【发布时间】:2017-02-21 12:22:31 【问题描述】:我有一个访问数据库,其中包含一些表单和表单后面的 vba 代码。
在Form_Open
(在每个表单上)我都有这段代码。
Dim hWindow As Long
Dim nResult As Long
Dim nCmdShow As Long
hWindow = Application.hWndAccessApp
nCmdShow = SW_HIDE
nResult = ShowWindow(ByVal hWindow, ByVal nCmdShow)
Call ShowWindow(Me.hWnd, SW_NORMAL)
这会隐藏 Access 并仅显示打开的表单。
我的问题是 Access 正在启动,但表单无法打开。
我必须在任务管理器中终止 Access 任务。
有什么办法可以解决这个问题吗?
编辑:每个表单都是弹出窗口
【问题讨论】:
【参考方案1】:据我了解,您需要隐藏主表单并保持弹出表单打开。我通常使用窗口不透明度设置而不是隐藏:
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Const LWA_ALPHA As Long = &H2&
Private Const LWA_COLORKEY As Long = &H1&
Private Const GWL_EXSTYLE As Long = -20
Private Const WS_EX_LAYERED As Long = &H80000
Public Function SetWndOpacity(hwnd As Long, opacity As Byte, clr As Long)
DoCmd.Maximize
SetWindowLong hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
SetLayeredWindowAttributes hwnd, 0&, opacity, LWA_ALPHA
End Function
然后调用:
SetWndOpacity Access.hWndAccessApp(), 0, 0
【讨论】:
以上是关于Form_Open VBA 代码不起作用的主要内容,如果未能解决你的问题,请参考以下文章