vba 打开表单时隐藏访问窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36528090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide access window when opening a form
提问by Robert Kendall
My Access database has an autoexec macro which opens a main menu (Form). All necessary functions are menu (form)-controlled, and I want to hide the Access Window so that only the forms are displayed. I was referred to http://www.tek-tips.com/faqs.cfm?fid=2562but this doesn't work with later versions. Is there a snippet which will work for Access 2007-2013?
我的 Access 数据库有一个 autoexec 宏,它打开一个主菜单(表单)。所有必要的功能都是菜单(表单)控制的,我想隐藏访问窗口,以便只显示表单。我被提到http://www.tek-tips.com/faqs.cfm?fid=2562但这不适用于更高版本。是否有适用于 Access 2007-2013 的代码段?
采纳答案by marlan
This is the code I run in my Acc 2003 and 2007 Apps, running in 2010 environment:
这是我在 2010 年环境中运行的 Acc 2003 和 2007 应用程序中运行的代码:
Private Sub Form_Load()
'Hide Data-Base Window:
DoCmd.SelectObject acTable, , True
DoCmd.RunCommand acCmdWindowHide
'...Other Actions...
end sub
For hiding the ribbon in 2007 and higher use this line I found here:
为了在 2007 年及更高版本中隐藏功能区,请使用我在此处找到的这一行:
DoCmd.ShowToolbar "Ribbon", acToolbarNo
回答by HansUp
It seems your goal is to restrict the Access UI features which are available to the user of your database. You want to limit them to the options which you provide via your startup form.
您的目标似乎是限制您的数据库用户可用的 Access UI 功能。您希望将它们限制为您通过启动表单提供的选项。
In that case, take a copy of your ACCDB file and change its file extension to ACCDR. Then when you open the ACCDR from Windows Explorer, Access will open it in "runtime mode". Runtime mode suppresses most of the standard UI options. For example, the Navigation pane is not displayed, and can't even be opened. Also it gives you a very minimal version of the Ribbon; the majority of the standard Ribbon options are gone.
在这种情况下,复制您的 ACCDB 文件并将其文件扩展名更改为 ACCDR。然后当您从 Windows 资源管理器打开 ACCDR 时,Access 将在“运行时模式”下打开它。运行时模式禁止大多数标准 UI 选项。例如,导航窗格不显示,甚至无法打开。它还为您提供了一个非常小的 Ribbon 版本;大多数标准功能区选项都消失了。
Runtime mode has other consequences which you should investigate to see whether it's a good fit for your needs. One important issue is runtime mode will quit the application when an unhandled error is encountered.
运行时模式还有其他后果,您应该调查一下,看看它是否适合您的需要。一个重要的问题是运行时模式会在遇到未处理的错误时退出应用程序。
If ACCDR/runtime mode suits your particular situation, it is an inexpensive way to limit the database UI features. However, beware a user could make a copy of the ACCDR and change the file extension back to ACCDB, so this approach alone may not satisfy your security requirements.
如果 ACCDR/运行时模式适合您的特定情况,它是一种限制数据库 UI 功能的廉价方法。但是,请注意用户可以复制 ACCDR 并将文件扩展名更改回 ACCDB,因此仅此方法可能无法满足您的安全要求。
回答by Anthony Griggs
This is what I use that works up to 2016:
这是我使用的直到 2016 年都有效的方法:
Option Compare Database
Option Explicit
'''HIDE WINDOW MODULE
'''USE THIS TO ACTIVATE HIDE WINDOW MODULE
''' SixHatHideWindow(SW_SHOWMINIMIZED)
Global Const SW_HIDE = 0
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Global Const SW_SHOWMAXIMIZED = 3
Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Function SixHatHideWindow(nCmdShow As Long)
Dim loX As Long
Dim loForm As Form
On Error Resume Next
Set loForm = Screen.ActiveForm
If Err <> 0 Then
loX = apiShowWindow(hWndAccessApp, nCmdShow)
Err.Clear
End If
If nCmdShow = SW_SHOWMINIMIZED And loForm.Modal = True Then
MsgBox "Cannot minimize Access with " _
& (loForm.Caption + " ") _
& "form on screen"
ElseIf nCmdShow = SW_HIDE And loForm.PopUp <> True Then
MsgBox "Cannot hide Access with " _
& (loForm.Caption + " ") _
& "form on screen"
Else
loX = apiShowWindow(hWndAccessApp, nCmdShow)
End If
SixHatHideWindow = (loX <> 0)
End Function
To Hide the Window Simply: Call SixHatHideWindow(SW_SHOWMINIMIZED)
简单地隐藏窗口: Call SixHatHideWindow(SW_SHOWMINIMIZED)
ALSO NOTE: Depending on Which Version i.e. 32 bit or 64 bit you may need to add the PtrSafe attribute so If you are having issues with this Declare the API Function like this: Private Declare PtrSafe Function apiShowWindow...
另请注意:根据哪个版本,即 32 位或 64 位,您可能需要添加 PtrSafe 属性,因此如果您遇到此问题,请像这样声明 API 函数: Private Declare PtrSafe Function apiShowWindow...
回答by phillfri
I was seeking to accomplish hiding the Access application window and use popup forms that I could run from Outlook. That's how I ran across this post. But I found that with Outlook 2013 64bit on Windows 7 64bit one doesn't even need the SixHatHideWindow function. Using the following code in Outlook 2013 accomplishes the same thing. (Don't forget to add a reference to the Access object library in VBA.) This procedure saves the caption from the currently active Outlook window, starts a new hidden instance of Access (application window not visible), opens the designated Access database, runs the designated form (as visible), exits the Access instance when the form is closed, and re-activates the original Outlook active window. I have not tested this in any other environment nor with runtime Access.
我试图完成隐藏 Access 应用程序窗口并使用可以从 Outlook 运行的弹出表单。这就是我看到这篇文章的方式。但是我发现在 Windows 7 64bit 上使用 Outlook 2013 64bit 甚至不需要 SixHatHideWindow 功能。在 Outlook 2013 中使用以下代码可以完成同样的事情。(不要忘记在 VBA 中添加对 Access 对象库的引用。)此过程从当前活动的 Outlook 窗口中保存标题,启动一个新的隐藏的 Access 实例(应用程序窗口不可见),打开指定的 Access 数据库,运行指定的窗体(如可见),在窗体关闭时退出 Access 实例,并重新激活原始 Outlook 活动窗口。我没有在任何其他环境中测试过这个,也没有使用运行时访问。
The great thing about this approach is that it doesn't require that any special code be inserted in the form open event in the Access database. All the necessary code is contained in Outlook VBA. Nor do the database form's popup and modal properties need to be set to "Yes" in the database.
这种方法的优点在于它不需要在 Access 数据库的表单打开事件中插入任何特殊代码。所有必要的代码都包含在 Outlook VBA 中。数据库表单的弹出窗口和模式属性也不需要在数据库中设置为“是”。
The form in this case is a complicated form with a tab control and several subforms. Everything appears to work whether the form is opened from Access itself or via automation from Outlook.
本例中的表单是一个复杂的表单,带有一个选项卡控件和几个子表单。无论表单是从 Access 本身打开还是通过 Outlook 的自动化打开,一切似乎都能正常工作。
Note: The SetWindowPos api sets the location and size of the Access main window even though Access is not visible. When Access is closed, the next time the user opens Access it will re-open to the size and position set by the SetWindowPos api. This could be annoying to users, so the SetWindowPos api sets the Access application window to full screen size. The next time the user opens Access it will be maximized on their screen. There may be more sophisticated ways to handle this, but this approach is fast and easy, and most users work with Access maximized in most cases anyway.
注意:即使 Access 不可见,SetWindowPos api 也会设置 Access 主窗口的位置和大小。当 Access 关闭时,下次用户打开 Access 时,它将重新打开到 SetWindowPos api 设置的大小和位置。这对用户来说可能很烦人,因此 SetWindowPos api 将 Access 应用程序窗口设置为全屏大小。下次用户打开 Access 时,它会在他们的屏幕上最大化。可能有更复杂的方法来处理这个问题,但这种方法既快速又简单,而且大多数用户在大多数情况下都使用最大化的 Access。
Hope this helps someone out.
希望这可以帮助某人。
Private Declare PtrSafe Function SetWindowPos Lib "user32.dll" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal X As Long, ByVal Y As Long, ByVal cX As Long, ByVal cY As Long, ByVal wFlags As Long) As Long
Private Declare PtrSafe Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Sub OpenAccessFrmProjDetails()
Dim accapp As Access.Application
Dim HoldCaption As String
Dim ScreenRect As RECT
Dim ret As Long
Dim width As Variant
Dim height As Variant
'Get the width and height of the screen in pixels
10 ret = GetClientRect(CLng(GetDesktopWindow()), ScreenRect)
20 width = ScreenRect.Right - ScreenRect.Left
30 height = ScreenRect.Bottom - ScreenRect.Top
'Get Outlook activewindow caption
40 HoldCaption = ActiveWindow.Caption
'Display the Access database form
50 Set accapp = New Access.Application
60 With accapp
70 .opencurrentdatabase ("C:\Users\pmf\Documents\Programming Development\Access Visibility Testing\Hidden.accdb")
80 SetWindowPos .hWndAccessApp, 0, 0, 0, width, height, 0
90 .DoCmd.OpenForm FormName:="frmProjDetails", WindowMode:=acDialog
100 End With
110 Set accapp = Nothing
'Reactivate Outlook
120 AppActivate HoldCaption
110 End Sub

