How Can I Give a User a Yes/No Prompt?

How Can I Give a User a Yes/No Prompt?

  • Comments 12
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script which performs some file management tasks, and then should ask the user whether or not they want to delete a set of files. How do I ask a user a Yes or No question like this?

-- SE, Casper, WY

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SE. There are a couple different ways that you can do this, but because you’re dealing with users the best way is probably to just pop up a message box and let them click Yes or No. How hard is that? Hey, it’s no harder than these few lines of code:

intAnswer = _
    Msgbox("Do you want to delete these files?", _
        vbYesNo, "Delete Files")

If intAnswer = vbYes Then
    Msgbox "You answered yes."
Else
    Msgbox "You answered no."
End If

All we’re doing here is creating a message box that features the following:

The message to be displayed.

In this example, the message is Do you want to delete these files?

A Yes button and a No button.

This is specified by the VBScript constant vbYesNo. There are other values that can give you different message box configurations; for example, you could have a message box with buttons labeled Abort, Retry, and Ignore. Who could forget good old Abort, Retry, and Ignore?

The message box caption.

In this example, the caption is simply Delete Files.

The message box will sit there until the user clicks a button. And how do we know which button was clicked? Well, the results of the button click are stored in the variable intAnswer. All we have to do is check the value of intAnswer. If intAnswer is equal to another VBScript constant (vbYes) then we know the user must have clicked yes; if it’s equal to anything else, then they must have clicked No. (After all, we only have two buttons, so if they didn’t click Yes they had to have clicked No.)

For more information about the MsgBox function, see the VBScript documentation on MSDN.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This helped me alot.... not that anyone is going to care or let alone see this message.... lol

  • @cj Yes I care and I read the comments. Glad it helped. Have an awesome day.

  • I am using Visual Basic and it's saying "end of statement expected" in the error box.... Help...

    THANK YOU

  • Hi,

    I am trying to create a script that enables a user to restart a locked down Windows 7 (Ent) workstation ... a with a trigger yes/no message ("Are you sure you want to restart?" = click yes or no button to proceed/abort the shutdown).

    Can someone advise how I can do this?

    The reason for this is because we have a few machines as meeting room/presentation computers that staff keep shutting down at the end of the meetings - and we want to prevent this by way of GPO blocking this and then scripting the ability to restart and elevating the script with an account that has this privilege.

    Thanks.

    JC

  • necesito agregar una condicion AL apretar los botones

    set intro3 = createobject("scripting.filesystemobject")

    set archivo3 = intro3.opentextfile ("E:\programa adivinador\cuadro0.txt",1)

    msgbox archivo3.readall, vbyesno

    if =vbyes then

    msgbox "+"

    else =vbno

    msgbox "-"

    end if

  • Hey, Scripting Guy!

    i have a question ,how to access/read data from excell worksheet using "Range" ?

    i tired the below

    Set ObjExcel = Creatobjet(" Excel.Application )

    .....

    ...

     MsgBox " value of cell A6 is " & O bjExcel.Range("A6")

    but i get some error saying "invalid character"

    plz clarify my doubt

    Regards

    Nissi

  • Ask questions here:

    social.technet.microsoft.com/.../ITCG

  • What if you wanted to ask a User if the details entered are correct? for example i have VBS that asks for the computer name and asks the user to enter there name then if the user clicks yes an email will be sent to me with these details if the user clicks no then it takes them back to the start of the script to reenter the details.

  • its shoeing error "variable is undefined" 'intAnswer in runtime

    plz help

  • Hello, I want to incorporate the above code into a batch file, can you suggest a solution? Thank you A

  • How can we copy some properties from one shared repository(1.tsr) to another shared repository (2.tsr)

  • whether we stop the QTP tool at the end of the day in the office?or not.