Library tutorials & articles

MsgBox Function

Usage

To show a OK dialog with a question mark you would type vbOKOnly+vbQuestion in the Buttons+Icon Parameter. The Dialog Title is a string which contains the title text. If this parameter is left blank, then the project name is used as the the Message Box's title

Msgbox "Are you sure?", vbYesNo+VBQuestion, "Front Page"

This shows a message box asking 'Are you sure' with yes and no buttons and a question icon. The Message Box function returns a number which tells you what button was pressed. The following constants are used:

Constant Button
vbOK OK Button
vbYes Yes Button
vbNo No Button
vbAbort Abort Button
vbRetry Retry Button
vbCancel Cancel Button
vbIgnore Ignore Button

Dim Ans As Integer
Ans = Msgbox("Are you sure?", vbYesNo+vbExclamation , "Front Page")
If Ans = vbYes Then
   Msgbox "You clicked yes"
Else
   Msgbox "You clicked no"
End If

This displays a message box asking 'Are you sure?', with yes and no buttons and a exclamation mark. If you click yes, then another message box is displayed saying 'You clicked yes', otherwise it displays a message saying 'You clicked no'.

If you want to include a variables contents in any of the parameters you can use an ampersand (&).

TheText = Text1.Text
Msgbox "The contents of Text1 is" & TheText

---OR---

Msgbox "The contents of Text1 is" & Text1.Text

Comments

  1. 17 May 2005 at 07:23

    hi,
    is it possible to have something which will display the information (information is one or two lines of text) for few seconds and disappear. if i use msgbox for it, there are many message boxes in the application which becomes annoying to the user.
    do i have to create a new form for it or there is another function.
    thanx in advance,
    bye

  2. 18 Mar 2005 at 16:20
    I get the following error:

    Code:
    It is invalid to show a modal dialog or form when the application is not running in UserInteractive mode. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
  3. 12 Apr 2004 at 02:25

    Hello
    dear i dont know how i can add icons in menu bar in visual basic .
    plaease help me to solve this problem.
    khuram

  4. 21 Apr 2003 at 18:11

    No, you need to make a new form that looks like a message box for that.

  5. 08 Oct 2002 at 22:22

    Dim  ans As String
    ans = MsgBox("Would you like to save changes?", vbYesNoCancel)
    If ans = vbYes Then
    'Put your Yes commands in here
    Else
       If  ans = vbNo Then
       'Put your No commands in here
       Else
       'Put your Cancel commands in here
       End If
    End If

  6. 05 Oct 2002 at 11:12

      if you have a message box like do you want to save changes you get yes , no and cancel but when you write a messagebox like that.


    if msgbox("Do you want to save changes",vbyesnocancel)=vbyes then
    ' saving document
    Else
    ' could be EITHER CANCEL OR NO
    End if


    can you see the problem?

  7. 24 Sep 2002 at 19:49

    I am not an expert infact im only 13 however i have been doing vb for about 2 years and i have sure done a lot with the msgbox() function and i do remember reading somewere that you cannot add or remove buttons from the default messages that vb has already installed for you, however if you build yourown msgbox the possibilities are endless you can even add images and movies and anything u can think of , the easiest and lamest way to do this is to make a new form  and call it say........"Message1" make 2 Buttons (Command1 and Command2) and make the "Message1" default BorderStyle
    03 - Fixed Dialog  then it will look like a normal window then u can manually set the height and width to whatever u like and then just have tha application load the "Message1" form whenever u need the message box and actually those types of message boxes are the easiest to code for desisions like if u have "ya" and the user clicks it u just code the Command1 for "ya" and command2 for "Tidak"


    THAT SHOULD WORK!!!!
    if it doesnt send me an email at Fruit_Cake70@hotmail.com
    good luck with your application

  8. 20 May 2002 at 00:00
    Can we add button component in the Msgbox? if it's possible, how?

    Example I want to add:
    button vbYa (which is in Malay) and the button caption is "Ya" (equal to Yes in English)
    button vbTidak (in Malay) and the caption is "Tidak" (equal to No in English)

    it's about changing the msgbox caption into my own Language.

    thanks
  9. 25 Mar 2002 at 12:08

    I'm fairly new to VB but picking it up quite well.


    Can I use custom buttons in a MsgBox? Like, if I want 'Fred', 'Bob' and 'Cancel'.

  10. 30 Jan 2002 at 21:14

    Button2 would be for no and Button3 is for cancel...

  11. 27 Jan 2002 at 22:25

    Msgbox "Do you want to save changes?", vbYesNoCancel+vbExclamation+vbDefaultButton3 , "Front Page"


    would set the default button as No.


    vbDefaultButton3 is the button for No?  Is button2 for cancel?  Would we want the user to cancel if they accidently pressed enter so they would make the choice of yes or no?


    I am new so bear with me!  Thanks.

  12. 01 Jan 1999 at 00:00

    This thread is for discussions of MsgBox Function.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!