The easiest way to teach you If statments is to have you make a program. Open a new project, and place 4 controls on the form as follows. Place a text box named txtNumber, place a label before the text box that says "Guess a &number from 1 to 10". Now place another label below the other controls named lblAnswer so we can return some information to the user. The last control needed is a command button, name it cmdGuess and the caption should say "&Guess". Your form should look something like:
Now that the interface is ready lets write some code! Open the cmdGuess event, and Dim an integer named intGuess. We'll use this to get the value out of txtnumber. So the next step is to put the data from txtnumber into intGuess, I recommend using the val function here so that we only get numbers from our input. So far our code should look like:
Private Sub cmdGuess_Click()
Now we have the user input in our variable lets make an If statment to see if they inputed 7. It's very simple to do. just add If intGuess = 7 then Now on the next line we'll tell the program what to do if the number inputed was 7. Lets tell the user they got the number right, on the next line type lblAnswer.caption = "Wow! You guessed the right number." now if you tried to run the problem you would get an error! oh-no what did you do wrong? We forgot to end our if statment. On the next line type End If. Never forget to end your if statments or you'll run in to problems every time. Now your Code should look like:
Private Sub cmdGuess_Click()
Notice how we tabed our statment in from the rest of the code. This is to make it alot easier to read. It may not seem important to you now but when we talk about nested statments you'll see why we do this. Try out your new program and enter any number but 7 in the text box then enter 7 once. The label box didn't change until you entered 7, and if you entered any numbers after you tried 7 the label stayed the same. Lets add some bells and whistles to this program. lets say if the user enters anything other then 7 we'll tell them they suck! :-) To do this enter Else before the end if. Then enter lblanswer.caption = "You suck! Try again." on the next line. The If statment will now look like:
Now try the program. One little line of code can make your program more user friendly. The user will now know if they are right or wrong. In the next section we'll show you how to do this in a Select Case Statment and we'll add some more features while we're at it.
Now we're going to modify the program you made in the last section. But first let me give you some background on how to use a case statment. Instead of starting with If we'll use Select case Variable, then we'll state different "case's" for that variable. Don't worry if your totally lost, you'll understant it better in an example. Delete the If statment in your program and add Select Case intGuess. Now on the next line lets make a case of intGuess type case 7 Now what do we want the program to do if intGuess = 7? Let's do the same thing as we did in the if statment. Under case 7 type lblAnswer.Caption = "Wow! You guessed the right number.". Like the If we need to end our case too, add End Select. Now our Case Select works just like our If statment. We can add an Else statment just like we did in our If but we need to add case to it. After you add that your code should like:
Private Sub cmdGuess_Click()
You may be asking yourself, self "why do people like case better then if statments? They look like the same amount of coding." Most programs only use if when there is only a few options but when you have many options for the same variable a case select is much easier then writing 10 million if..else statments. Well Now lets add a new feature to this program. Before the case 7, but after the select case, add case is < 0. Note: We have to put "is" before we can use relational operators in our case statments. If the user guesses a - number lets show them an error message. msgbox "1 to 10 only please", vbOkay, "Wrong!" Notice we did not use a variable & () around our message box. This is becuase we don't need to know what number is returned by the message box. We can re-use this same message box if the user inputs anything higher then 10 too. Lets change the case is < 0 to Case is < 0, is > 10. Want ot add some more features? Lets add two more. We'll tell the user if they guessed too high or too low. Try adding your own cases with out looking at the code below yet. Once you made your code here is ours:
Private Sub cmdGuess_Click()
This program may seem fun to some one the first time they guess 7, but after that it is just plan boring! Read the Random Number Function below to learn how to make this program pick a number from 1 to 10 for you! Your user will love it :-)
In the last few sections we made a little guessing game. If you have skipped ahead please start reading from Here to follow along. Right now our program only takes guesses from 1 to 10 and the user wins if they guess 7. After this section the program will make a random number for the user to guess at. The Random Number function works like this, Variable = rnd(lower_limit + upper_limit) * N). The rnd will return a random number from 0 to 1 (a very long decimal), but it will never be 0 and it will never be 1. So we take the total of numbers we want (lower + upper) and times that by (N) how many places we want to move the deciaml. Yes, this does work! Now if we say lower is 1 and upper is 10 we will get a random number some where in between there. The only problem is it has a long tail of decimals behind it. We can fix this by adding the int function to random statment. Still with me? it will look like: Variable = int(rnd(lower_limit + upper_limit) * N)). Now lets make it work in our program. First thing to do is add the form load event to your code. We'll make a new number everytime the form loads. Now we need a variable for the number to go into, lets call it intRandom. Note: we are going to pass this variable in to the cmdGuess event so we need to make it form level. If i just totally lost you then you should read Scope of Variables found in tutorial 2. For the rest of you, you should now be ready to add the random statment. So in the form load type: intRandom = Int(Rnd(1 + 10) * 10). This will put a number from 1 to 10 into intRandom. There is one little bug in is function! It will usally give the same random number the first time. What i mean is not totally random (yet). Test your program either by stepping though the code (hitting F8) or adding txtNumber.Text = intRandom under the random statment. Now you may notice