19

I need to display a message to the user. When I do this using MsgBox, the program stops until the user clicks the box away. I'd like to know if there's a way to open the MsgBox without pausing the program.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Patrícia Villela
  • 808
  • 4
  • 14
  • 38
  • 2
    Nope, MsgBox always halts execution until clicked away. – Jean-François Corbett Mar 28 '11 at 14:45
  • 1
    This title should be updated to reflect that MsgBox is not a requirement of your question. Coming here from a problem where I can't use the `Application.Statusbar` to communicate to the user as Excel is already using it for something else. – Jay Killeen Mar 21 '17 at 03:59

8 Answers8

31

Sounds like you're not expecting any user input from the MsgBox. In this case, depending on your application, the StatusBar may be an adequate substitute.

In Excel this is easy:

Application.StatusBar = "Please be patient..."
Application.StatusBar = iDone & " of " & iTotal & " items done."

To clear the StatusBar when done:

Application.StatusBar = False

In Access, the syntax is a tiny bit more convoluted:

Temp = SysCmd(acSysCmdSetStatus, "Hey, look at me!") ' Puts out your message
Temp = SysCmd(acSysCmdClearStatus) ' Clears StatusBar
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
7

As far as I've ever been able to discover, the answer is you can't. The work-around is a custom form that serves as a dialog box.

See http://www.mvps.org/access/forms/frm0046.htm (not precisely your question, but applicable).

Smandoli
  • 6,919
  • 3
  • 49
  • 83
6

MsgBox is modal (meaning the window comes up and halts execution of code until it is cleared). As other posters/commenters have mentioned - your alternative is to write your own version of a popup that is not modal. Not really worth the effort unless you really need it that way.

ktharsis
  • 3,160
  • 1
  • 19
  • 30
  • 1
    This is incorrect. Msgbox are modal dialogs; the dialog being what halts code. Only forms opened using acDialog and actual dialog boxes (MsgBox, FileDialog, etc) halt code. One can make a modal or popup form, neither of which halt code. – usncahill Nov 21 '16 at 19:39
5

In the VB editor: Select Insert menu UserForm. In the Toolbox select TextBox: Drag a rectangle in the UserForm and type your text into it. Right click on the UserForm and select Properties. In the ShowModal property: Select False. In your VBA module enter UserForm1.Show where you want to turn it on and UserForm1.Hide where you want to turn it off. UserForm1 is mine, of course use the appropriate name for the form you created.

Einar Sundgren
  • 4,325
  • 9
  • 40
  • 59
user4539187
  • 51
  • 1
  • 1
4

Create a Form instead. I created a small form that only has a text box that says "Working, Please Wait". When needed I open the form, as a pop-up (docmd openform "form name"), usually just before starting some operation that is going to take some time to complete. When the work completes I close the form (docmd close acform "form name"). This does not stop the program but does provide a "Message" to the user.

Sam
  • 41
  • 1
2

You can use WScript's Popup method. Here's the full details including sample code: http://msdn.microsoft.com/en-us/library/x83z1d9f%28v=vs.85%29.aspx

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
HK1
  • 11,941
  • 14
  • 64
  • 99
  • I believe this option does not allow the code to keep running, as per user's requirement. It's a modal window just like msgbox, as far as I tested (for VBA/Excel, at least). – Tiago Cardoso Mar 29 '11 at 14:31
  • @TiagoCardoso It does indeed pause the code from running, but unlike msgbox, it allows the user to switch to different tabs, sheets, forms, etc. within Excel (or other software, for that matter) - which could have been what the OP actually wanted, after all. Another advantage is that it allows much larger messages to be displayed. – Yin Cognyto Dec 26 '19 at 20:00
1

I believe you first need to evaluate if you really need a msgbox to pops-up and keep your code running.

The msgbox functionality (as already stated) is modal and you cannot 'bypass' it. However, you can create a form (similar to the msgbox), set this form as 'not Modal' and call the code to show this form. The code workflow goes on. Tested and works in Excel.

feetwet
  • 3,248
  • 7
  • 46
  • 84
Tiago Cardoso
  • 2,057
  • 1
  • 14
  • 33
1

You can use this. It worked for me to create a dialog for outlook:

Dim myMessage as String  
Dim myDelay as Integer  
myMessage = "This message will self destruct in 8 seconds!"  
myDelay = 8  
Shell "msg /TIME:" & myDelay & " " & Environ("Username") & " " & myMessage