Hey guys welcome back. So in this video we're going to learn how to capture the user's input on a message box. So we're going to learn how to capture the answer of a user if the user clicks on a certain button on the message box then I need to take some actions based on that so we'll learn how to do that. So basically here the message box buttons each one of them has a value so the VB okay the ok button has a certain value the cancel has another value to abort is three or tries four and so on and so forth. So basically, the idea is that we will assign these values to a variable and then we will put an if statement testing what the value is and if the value for example is one or VB Okay, then we will do this and this and that if the value is two, then we will do this and this and that, and so on and so forth.
So let's have an example. Okay, guys, so I've created a simple message box that asks the user Very simple question. Do you love chocolate? And it will have yes and no buttons, and it's going to be a VB question and title of chocolate. So if you run that the user will have this question. And he can answer with either yes or no.
So if the answer is either yes or no, at the moment, nothing will happen. So to make something happen, we need to actually assign the answer of the message box to a variable. And for the data type of the variable, we will have several options. So let's create a so I'll create a variable named answer and the data type for it. This is the one I prefer is VB message box result. And VB message box result is basically an enumeration.
And this is a fancy word However, all it means is that it's just a list of values having constants assigned to them. So now we need to assign the message box to the variable answer. And you can see here when I read equals, I get a list of the different buttons that the user can click, but we won't use this list. Right now, and if I just click away I will get an error as you can see here. The reason for that is that if you write the message box on the right hand side of an assignment operator like the equal sign, you need to put the arguments between brackets. Okay?
So this is something in the VBA syntax. So now we've assigned the message box to the answer variable and so now whatever answer the user gives us will be stored in the variable called answer. Okay, so now we need to put an if statement so if answer equals For example, here we've got yes and no buttons only so if answer equals VBS, then message box, in this case, just going to be a simple message box. Great. I love chocolate too. And elsif answer is equal to VB No.
Then message box. Why not? Chocolate is delicious. And if Okay, so So now based on the user's answer, he will get one of these two message boxes. So if we run this code here, do you love chocolate? If I answer Yes, great.
I love chocolate too. And if I run it again, do you love chocolate? No. Why not? Chocolate is delicious. So based on the user's answer, you can take any kind of actions.
If you put an if statement, you can put any lines of code here on the if statement and do whatever you want. You could also instead of VB yes and VB no use their corresponding values. So instead of VBS, you could put a six and VB No, you could put a seven here and you can use their corresponding values and this will work too. You could also change the data type to an integer and this will work to with the numbers and also you could write equals VBS and VB no And this works as well. However, the advantage of using a VB message box result is that you do get the drop down menu while you're typing the variable. So when you write answer equals, you start getting the list and you don't need to worry about remembering the answers.
Okay, so this is why I'd recommend using the VB message box result data type in this case, so thank you guys for watching this video and I'll see you on the next one.