Form_Open VBA代码不起作用

时间:2017-02-21 12:22:31

标签: vba ms-access access-vba

我有一个访问数据库,其中包含表单后面的一些表单和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任务。

有什么方法可以解决这个问题吗?

编辑:每个表单都是一个PopUp

1 个答案:

答案 0 :(得分:2)

据我了解,您需要隐藏主窗体并保持弹出窗口打开。我通常使用窗口不透明度设置而不是隐藏:

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