martin2k

ForumsVB6GamesSoftwareDirectoryHTMLArticlesBlogContact

Using the If Statement


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

Beginner Tip

You will now learn how to use the If statement.  The If statement tests to see if a condition is the same as the one specified and then executes the commands following it. You must use 'Then' after the condition. This simple example will perform different actions depending what day it is.

1. Put a CommandButton onto the Form.

1. Type the following code into the CommandButton's Click event - remember to choose Command1 in the General ComboBox in the code window.  You do not have to type in the comments (the green text), but they may help you remember what the code means when you next return to the project.

Dim d As String 'Declare a variable for the day

d = Format(Date, "dddd") 'Set the variable to the current day

If d = "Saturday" Then
    MsgBox "Today is Saturday"
Else 'The code below happens when it is not Saturday
    MsgBox "It is not Saturday today"
End If

2. Press F5 to start the program and click on the CommandButton and see what happens.  If it is Saturday, you will see a message box telling you that it is, otherwise the message will be that it isn't Saturday.

ElseIf

If you want to test a condition more than once before executing the code under Else, use the ElseIf clause:

If d = "Saturday" Then
    MsgBox "Today is Saturday"
ElseIf d = "Sunday" Then
    MsgBox "Today is Sunday"
Else 'The code below happens when it is not Saturday or Sunday
    MsgBox "It is neither Saturday nor Sunday today"
End If

You can have use ElseIf as many times as you want.  If you were to use it for everyday of the week, there would be no need for Else the code underneath it would never be executed.

Once a condition has been met and its code executed, execution continues with the code following End If.

You can have an If statement on one line if you only need to test for one condition e.g.:

If g = 1 Then h = 2

End If is not necessary for single line If statements.

Previous < My First Program

blog comments powered by Disqus

Archived Comments

From: DEEPAPRIYA
Date: Monday, December 20, 2010 at 10:55:43
Comments: .... Its quite easy to understand the programs... It is very useful to the beginners.... I need more than this..... thank you martin2k
 
From: sudhir
Date: Wednesday, February 10, 2010 at 21:37:22
Comments: this website is easy to use
 
From: shivani
Date: Saturday, October 3, 2009 at 06:36:49
Comments: thanks a lot this site help me in vb
 
From: RAHMAN
Date: Friday, September 19, 2008 at 15:44:21
Comments: I like this types of study and i want to learn much more
 
From: RAHMAN
Date: Wednesday, September 17, 2008 at 16:21:47
Comments: I realy thanks to Martin 2k bcoz its help me very much in create a project of VB it's my Pleader that i m sending a msg my self! thanks alot once again
 
From: vijay
Date: Sunday, September 30, 2007 at 08:30:28
Comments: hi goodmorning
          in this web site is easy learn to vb.
 
From: Sangeetha
Date: Thursday, September 27, 2007 at 11:12:14
Comments: thank u. The examples are very useful for the beginnners but i want to know more than that...
 
From: rupa
Date: Monday, May 14, 2007 at 12:31:55
Comments: this site is truly best.
 
From: Kyle
Date: Wednesday, May 5, 2004 at 15:56:17
Comments: Well.. For my project im doing hangman.. and  i need my hangmang to appear... i was thinking of a command somethig along these lines.

IF blanka.visible = True Then
blanka.visible = False

And like if there is no blanka then i want it to go into a set of comands taht will take view the body.
I was having so much trouble with this.

 
From: X_Dave_X
Date: Friday, November 7, 2003 at 16:16:51
Comments:

hey. im doin a project for school, and i need help. my project consists of 4 coloured circles moving in the direction towards the center, and in the middle of my form, i have a stop button. i want the circles to bounce off th stop button. so far, ive got nothin, and the if statments are pissing me off somewhat. heres my code:

Private Sub cmdStop_Click()
'STOPS SHAPES FROM MOVING
    Me.tmrBlue.Enabled = False
    Me.tmrYellow.Enabled = False
    Me.tmrGreen.Enabled = False
    Me.tmrRed.Enabled = False
End Sub


Private Sub mnuExit_Click()
'exit program
    End
End Sub


Private Sub mnuReset_Click()
'RESETS SHAPES TO ORIGIONAL PLACE
    Me.shpRed.Top = 840
    Me.shpRed.Left = 480
    Me.shpYellow.Top = 840
    Me.shpYellow.Left = 7560
    Me.shpBlue.Top = 5880
    Me.shpBlue.Left = 7560
    Me.shpGreen.Top = 5880
    Me.shpGreen.Left = 480
End Sub


Private Sub mnuStart_Click()
'START MOVING SHAPES
    Me.tmrBlue.Enabled = True
    Me.tmrYellow.Enabled = True
    Me.tmrGreen.Enabled = True
    Me.tmrRed.Enabled = True
End Sub


Private Sub tmrBlue_Timer()
'MOVE BLUE CIRCLE
    Me.shpBlue.Top = Me.shpBlue.Top - 15
    Me.shpBlue.Left = Me.shpBlue.Left - 15
End Sub


Private Sub tmrGreen_Timer()
'MOVE GREEN CIRCLE
    Me.shpGreen.Top = Me.shpGreen.Top - 15
    Me.shpGreen.Left = Me.shpGreen.Left + 15
End Sub


Private Sub tmrRed_Timer()
'MOVE RED CIRCLE
    Me.shpRed.Top = Me.shpRed.Top + 15
    Me.shpRed.Left = Me.shpRed.Left + 18
End Sub


Private Sub tmrYellow_Timer()
'MOVE YELLOW CIRCLE
    Me.shpYellow.Top = Me.shpYellow.Top + 15
    Me.shpYellow.Left = Me.shpYellow.Left - 15
End Sub

can you please help me? thanks.

 
Reply:

Firstly, add the following code to the General Declarations section of your code:

Option Explicit

Dim RedDir As Boolean, GreenDir As Boolean, YellowDir As Boolean, BlueDir As Boolean

The above variables relate to the direction of the balls i.e. False is towards the button and True is away from the button.  You will then need to change your code so that when a ball hits the button, its variable will change to True so that the ball goes in the opposite direction:

Private Sub tmrBlue_Timer()
'MOVE BLUE CIRCLE
If BlueDir = False Then
    Me.shpBlue.Top = Me.shpBlue.Top - 15
    Me.shpBlue.Left = Me.shpBlue.Left - 15
Else
    Me.shpBlue.Top = Me.shpBlue.Top + 15
    Me.shpBlue.Left = Me.shpBlue.Left + 15
End If

If shpBlue.Left < cmdStop.Left + cmdStop.Width And shpBlue.Top < cmdStop.Top + cmdStop.Height Then BlueDir = True
End Sub


Private Sub tmrGreen_Timer()
'MOVE GREEN CIRCLE
If GreenDir = False Then
    Me.shpGreen.Top = Me.shpGreen.Top - 15
    Me.shpGreen.Left = Me.shpGreen.Left + 15
Else
    Me.shpGreen.Top = Me.shpGreen.Top + 15
    Me.shpGreen.Left = Me.shpGreen.Left - 15
End If

If shpGreen.Left + shpGreen.Width > cmdStop.Left And shpGreen.Top < cmdStop.Top + cmdStop.Height Then GreenDir = True
End Sub


Private Sub tmrRed_Timer()
'MOVE RED CIRCLE
If RedDir = False Then
    Me.shpRed.Top = Me.shpRed.Top + 15
    Me.shpRed.Left = Me.shpRed.Left + 15
Else
    Me.shpRed.Top = Me.shpRed.Top - 15
    Me.shpRed.Left = Me.shpRed.Left - 15
End If

If shpRed.Left + shpRed.Width > cmdStop.Left And shpRed.Top + shpRed.Height > cmdStop.Top Then RedDir = True
End Sub


Private Sub tmrYellow_Timer()
'MOVE YELLOW CIRCLE
If YellowDir = False Then
    Me.shpYellow.Top = Me.shpYellow.Top + 15
    Me.shpYellow.Left = Me.shpYellow.Left - 15
Else
    Me.shpYellow.Top = Me.shpYellow.Top - 15
    Me.shpYellow.Left = Me.shpYellow.Left + 15
End If

If shpYellow.Left < cmdStop.Left + cmdStop.Width And shpYellow.Top + shpYellow.Height > cmdStop.Top Then YellowDir = True
End Sub

 
From: henry
Date: Wednesday, February 26, 2003 at 22:34:43
Comments:

I'm using a checkbox to print out a selection. It works fine. Now I want to add in an If statement so if someone checks the checkbox it'll search to see if there is anything in a cell and if there is, print a selection. Here's my coding:

Private Sub CheckBox1_Click22()

' CheckBox1_Click22 Macro
' Macro recorded 2/26/2003 by sinichkoh
'
'

If A7 > 0 Then
Range("A3:C13").Select
Selection.PrintOut Copies:=1, Collate:=True
Else
MsgBox ("nothing is worth printing")
End If
End Sub

Any help you can offer would be appreciated

 
Reply:

Replace:

If A7 > 0 Then

with:

If Range("A7").FormulaR1C1 <> "" Then
 
From: meriam
Date: Wednesday, February 26, 2003 at 10:57:27
Comments: But how do you say:
if (checkbox 1 = true and ckeckbox 2 = true)
then
show the message?
 
Reply:

Try this code:

If Check1.Value = vbChecked And Check2.Value = vbChecked Then MsgBox "Here is a message"
 
From: sp0on
Date: Thursday, December 5, 2002 at 22:27:00
Comments:

Hi. i was wondering if it was possible to use the IF statement for multiple IFs. what i am trying to do is use make a random integer equal to text, which is then displayed elsewhere.  this is for a Fortune Teller program im trying to make. a bit of the code i have, which doesnt work, follows:

Private Sub findfortune_Click()
Randomize Timer
moneyNum = Int(Rnd * 100) + 1
moneytext = Str$(moneyNum)

Rem BEGIN MONEY FORTUNES
If moneytext2 = 1 Or 10 Or 20 Or 30 Or 40 Or 50 Or 60 Or 70 Or 80 Or 90 Then
moneytext = "Money Bags!"
End If
If moneytext2 = 2 Or 12 Or 21 Or 31 Or 41 Or 51 Or 61 Or 71 Or 81 Or 91 Then
moneytext = "Poor!"
End If
If moneytext2 = 3 Or 13 Or 23 Or 33 Or 43 Or 53 Or 63 Or 73 Or 83 Or 93 Then
moneytext = "Not bad"
End If
If
moneytext2 = 4 Or 14 Or 24 Or 34 Or 44 Or 54 Or 64 Or 74 Or 84 Or 94 Then
moneytext = "Hard to tell"
End If
If
moneytext2 = 5 Or 15 Or 25 Or 35 Or 45 Or 55 Or 65 Or 75 Or 85 Or 95 Then
moneytext = "Be generous today!"
End If
If
moneytext2 = 6 Or 16 Or 26 Or 36 Or 46 Or 56 Or 66 Or 76 Or 86 Or 96 Then
moneytext = "You've had better"
End If
If
moneytext2 = 7 Or 17 Or 27 Or 37 Or 47 Or 57 Or 67 Or 77 Or 87 Or 97 Then
moneytext = "You've had worse"
End If
If
moneytext2 = 8 Or 18 Or 28 Or 38 Or 48 Or 58 Or 68 Or 78 Or 88 Or 98 Then
moneytext = "Much like yesterday"
End If
If
moneytext2 = 9 Or 19 Or 29 Or 39 Or 49 Or 59 Or 69 Or 79 Or 89 Or 99 Then
moneytext = "Prosperous!!!"
End If
If
moneytext2 = 100 Then
moneytext = "all $ to sp0on!"
End If
Rem END MONEY FORTUNES

I thought this code would work, but when i run the application it always comes up as moneytext = "Prosperous".  I know this isnt right because when i have the text display the random integer, the interger itself changes, it is just the possible money outcomes dont change. am i off to a right start? or am i completely on the wrong track?  (please keep in mind im teaching myself vb, so please forgive my ignorance)

thanks
sp0on!

 
Reply:

The above code doesn't work because each number needs to be tested i.e.:

If moneytext2 = 1 Or moneytext2 = 10 Or moneytext2 = 20 ...

This will result in the lines being very long.  Therefore the best way of doing this is to use Select Case instead.  It is a lot easier:

Select Case moneytext2
Case 1, 10, 20, 30, 40, 50, 60, 70, 80, 90
moneytext = "Money Bags!"
Case 2, 12, 21 ...
moneytext = "Poor!"
...

By the way, what happens if moneytext2 equals 11?

 
Reply from Ah Cat - Wednesday, June 20, 2007 at 08:24:18: you're using "moneytext2" for almost all except "prosperous", which is using "moneytext". Therefore the only one that works would be "Prosperous" since the variable you're using is "moneytext" not "moneytext2".

Martin Allen 1999 - 2011.  Last updated Monday 08 August 2011 07:20:03 PM +0100.