Message Boxes
You tell the program you want a message box using the following: msgbox There are many options used in a message box, the table below lists the different button set ups and different icons to display on the message box.
Now you may be wondering how you apply all these fantastic features to the message box. The syntax layout below should explain that pretty well. msgbox "prompt", button, "title", help file, context We will only be using the first three (prompt, button, title) because I dont know how to use the other two :D. Anyways, its pretty simple, in the area it says prompt, you put in what you want it to say. For button, you put in any of the options from the above table (button or icon) exactly as it appears (IE; vbOkCancel) and then you put the title. Make sure things that are displayed in quotes stay in quotes.
Alrighty, now that we have covered that, Im going to show you how to use a message box. Not very hard at all. For now, just create a new form. Form name and project name do not matter for this particular tutorial. Double click the Command Button in your tool box to add a command button to the form. Name it cmd1 and set the caption to Message Box Now we get to coding. Double click cmd1 to open up the click event procedure sub in the code window. Add the following code. msgbox "Hey, you clicked my button" , vbvbExclamation, "You clicked it!" Thats all there is to it.
Now Im going to make it a little more complicated. Before you continue on, make sure you are pretty comfortable with variables and familiar with If statments as they are used a bit here in the next few steps.
Alright, erase what you coded for the command button. Add a textbox to the form and name it txt1, and set the text property to nothing. Once that is done, open up the code window and make sure you are under click event procedure for cmd1. Declare some variables. dim strMsg as string dim strStyle as string dim strTitle as string See anything familiar.
What we are going to do is change the values of those variables to alter the display of our message box based on what a user inserts. Alright, now for the If statement. Put the following code under the variable declerations. if txt1.text >= 5 then strmsg = "The number was more than 5" strStyle = vbInformation strTitle = "More than 5" else strmsg = "The number was less than 5" strStyle = vbCritical strTitle = "Less than 5!!!" End if Now, that will do absolutley nothing, so we need to have a message box pop up. Add this under the if statement. i = MsgBox(strMsg, strStyle, strStyle) There is a bit of a difference here between the regular decleration and this one. When using variables in your message box, you must set it equal to another vairable and nothing can be in quotes. This works best if you just use a one letter variable, undimmed, and have it at the end of the coding for that event procedure. You see how that works? Simple enough.
Quick Tip:
You can use multiple button/icon combos by doing this: MsgBox "Hey", vbInformation + vbOKCancel, "Titled" Notice the added plus sign?
Input Boxes
An input box is different from a message box in the sense that it asks the user a question and leaves a text field for the user to type in. Input boxes are a bit more complicated because must have a variable associated with them or they serve no purpose. You can have one just pop up, but the information the user puts in will not be saved anywhere.
The syntax layout below explains how to customize your input boxes: inputbox("Prompt", "Title", "Default", Xpos, YPos ,Help File, Context) Again, we will only worry with the first three. Prompt is basically what you want to ask the user. The title is the title of the input box and default is the default text already in the text box. The only required attribute is Prompt, the rest you don't need. Now onto making something useful.
Start a new form, again, form name and caption do not matter, same with project name. Make a label in the middle of the form. Get rid of the caption and name it lblOutput. Time for some code. Double click the form to bring up the code window for the sub form load event procedure. We need to declare one variable, and that is to store the data the user types in. dim strPets as string Now, on a new line, insert the following: strpets=inputbox("How many pets do you have?","My Survey","Enter Number Here") Simple enough, not complicated.
Next we need to display the data. On a new line, type this lblout.Caption = "You have " & strpets & " pets!" So our entire event procedure looks like this: Dim strpets As String strpets = InputBox("How many pets do you have?", "My Survey", "Enter Number Here") lbloutput.Caption = "You have " & strpets & " pets!" Play it and see what happens. Nifty, huh? Well thats it for this tutorial. Good work if you completed it. If you have any questions, post below.
Tutorial Challenge:
Create a form that asks a user how old they are and displays the age on a form. Then make a command button that when clicked, an alert pops up and asks if they are sure they want to quit. Hints: vbOkCancel, put the message box before End in the CmdExit click procedure.
Comments
Post new comment