3

是否有任何选项可以使用 VBA 访问表单应用程序中的任何代码关闭当前打开的 MsgBox?

4

9 回答 9

6

在 microsoft.public.vb.general.discussion 中查看 Randy Birch对此线程的回复

他建议在名为 MsgBox 的 .bas 文件中创建一个函数。这样做会导致 VB 调用您的函数而不是内置函数。

然后,您将创建自己的 MsgBox 表单并构建一个计时器以在一段时间后关闭您的表单。他提供了显示如何执行此操作的链接。

他还讨论了一种在需要时显式调用内置 MsgBox 函数的方法。

注意:我从未这样做过,但我发现 Randy Birch 是一个知识渊博的资源。

于 2009-07-16T18:06:27.910 回答
2

MsgBoxes 不打算以编程方式关闭,这就是为什么很难这样做。如果您发现自己处于必须强制关闭 MsgBox 的设计中,您可能应该重新评估您的设计。

于 2009-07-16T18:41:34.047 回答
1

我可能是错的,但 MsgBox 是一个创建模式表单的阻塞调用,所以我认为没有一种简单的方法可以做到这一点。您对此有特定的用例吗?

于 2009-07-16T18:04:26.820 回答
1

正如 MarkJ 指出的那样,这可能是 Access 生成的对话框(而不是在您自己的代码中调用的 VBA.MsgBox)吗?

例如,当在 Access UI 中使用表的“数据视图”添加一行时,您会收到一条消息,“您即将追加 1 条记录...”(或类似内容)。这是你的意思吗?如果是这样,确实有办法压制他们……

于 2009-07-17T09:40:08.573 回答
1

I used to have an easy answer to this: use the Windows Scripting Shell object, which has a 'Popup' function - a Message Box, just like the VBA MsgBox() function, with a 'SecondsToWait' parameter that provides exactly the timeout you wanted.

With CreateObject("Scripting.WsShell")
    .Popup "Watch me disappear in 5 seconds", 5, Application.Name & ": test", vbInformation + vbOkCancel
End With

If you include a 'Cancel' button, it might still work: the available parameters are vbOkCancel, vbYesNoCancel, and vbRetryCancel.

If you're trying to close a dialog box you didn't initiate with your own msgBox() function call, that's unhelpful: and, as I've hinted above, the 'SecondsToWait' parameter doesn't really work these days - someone in Redmond really does't like the idea of one thread closing another thread's helpful warnings and important interruptions to the user's workflow.

However, you can launch a delayed Message Box 'Close' command using the API Timer() function - not quite 'close the currently opened MsgBox', as this requires advance warning that you intended to open it - but it's the closest thing I have, it fits into a self-contained VBA module, and I posted the code in an answer to a very similar StackOverflow question to yours.

I should warn you that the answer in question is using a sledgehammer to crack a nut, with a side order of lengthy explanation.

于 2016-02-01T22:44:15.997 回答
0

与其从头开始编写 Access MsgBox 的替代方案,不如考虑使用(或至少学习)Arvin Meyer 的 Custom MessageBox Creator(可在此页面上下载)。

于 2009-07-16T19:16:56.487 回答
0

我也遇到过类似的问题;理论上你可以使用“SendKeys”功能(见http://msdn.microsoft.com/en-us/library/8c6yea83%28VS.85%29.aspx)。但是,MsgBox 会阻止运行,因此您无法使用该命令。如果您知道它何时会弹出,您可以(从脚本)运行外部 whshell,等待一段时间,然后使用 SendKeys。但是,如果您知道该怎么做,请告诉我(此处)。其他类似的可能性是为其打开不会被阻塞的不同线程/进程,但我不知道在 VBA 中是否可行。

于 2009-07-22T09:21:10.000 回答
0

消息框旨在向用户描述一些信息。因此,以编程方式关闭不是一个好的设计,除非您没有自动化某些过程。

您可以使用 sendkeys 或 win API。

于 2009-07-22T11:14:25.810 回答
0

一个易于使用的基于 Win32 的答案实际上对调用它很有用。使用下面的代码,您可以轻松地创建一个基于计时器的 PopUpBox。我喜欢 1/2 秒定时器控制,所以 2 = 1 秒。找到这个答案寻找这个线程的答案。以上答案似乎不起作用。当我想显示快速消息或快速回答时,我想使用它默认值通常是正确的,可以用于更多。

功能代码通常在一个模块中:

Declare Function MessageBoxTimeout Lib "user32.dll" Alias "MessageBoxTimeoutA" ( _
    ByVal hwnd As Long, _
    ByVal lpText As String, _
    ByVal lpCaption As String, _
    ByVal uType As Long, _
    ByVal wLanguageID As Long, _
    ByVal lngMilliseconds As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

Public Function PopUpBox(Optional stMessage As String _
        = "Yes or No? leaving this window for 1 min is the same as clicking Yes.", _
        Optional stTitle As String = "PopUp Window", _
        Optional HalfSecTimer As Long = 120, Optional lgVBmsgType As Long = vbYesNo) As Long

    Dim RetVal As Long

    HalfSecTimer = HalfSecTimer * 500
    RetVal = MessageBoxTimeout(FindWindow(vbNullString, Title), stMessage, stTitle, lgVBmsgType, _ 
        0, HalfSecTimer)

    PopUpBox = RetVal
End Function

调用函数代码
示例:来自我的数据库的实际代码

PopUpBox "Re-Linking and Closing dB", "Closing dB", 3, vbOKOnly
intAnswer = PopUpBox("Software Lock Down Active?", "Security", 10, vbYesNo)
于 2019-09-20T08:34:54.457 回答