Microsoft & .NETVisual BasicBasics of VB Programming

Basics of VB Programming

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This article is designed to help beginners get to grips with some
basic functions in VB. The following things are covered:

  • Message Boxes
  • Input Boxes
  • For..Next
  • Do..Loop
  • If..Then..Else

Message Boxes are used to display information to the user. You see
them all the time in Windows and other programs.

Message boxes can be called like this:

MsgBox Prompt, Buttons/Icon, Title

The prompt is basically what you want it to say, the buttons/icon
is what buttons you want and which icon you want and title is really self explanatory.
e.g.

Msgbox "This is a test.",vbCritical,"Title"  

You can use the following constants for buttons:

  • vbOkOnly
  • vbOkCancel
  • vbYesNo
  • vbYesNoCancel
  • vbAbortRetryIgnore
  • vbRetryCancel

and these constants for icons:

  • vbQuestion
  • vbInformation
  • vbExclamation
  • vbCritical

An important thing that you need to be able to do is to translate
what the user has done. e.g.

Dim iRes As Integer
iRes=MsgBox("Test Question?",vbYesNo+vbQuestion,"Title"
If iRes=vbYes Then
  Msgbox "The Yes button was clicked."
Else
  Msgbox "The No button was clicked."
End If

To include the contents of a control, such as a text box, you can
use the ampersand character (&) to concentate the prompt string (see No Strings Attached). e.g.

Msgbox "The contents is:" & Text1.Text

The input box is a useful way of getting the user to input a
string or other data type.

Input boxes can be called like this:

InputBox Prompt, Title, DefaultValue

The prompt is the same for the message box and so is the title,
the default value can be left as blank if you don’t want anything to appear. e.g.

InputBox "This is a test.","Title","Hi there!"

The InputBox always returns a string data type, so in some
circumstances you may want to convert it to a number. To do this you can use the Val()
function. e.g.

Val(InputBox("This is a test.","Title","9"

This would return the string value of 9 as an integer value.

You may want to check if the input from the user is nothing, or
hasn’t changed. e.g.

Dim sText
Dim sValue
sValue="Hi there!"
sText=InputBox("This is a test.","Title",sValue)
'Check for nothing
If sText="" Then Msgbox "No text"
'Check for no change
If sText=sValue Then Msgbox "No change."

The For..Next statement is an easy way to control program flow.
e.g.

Dim i As Integer
Dim iValue As Integer
For i=0 To 10
 iValue=0+1
Next i

In this situation iValue will equal 0 at the start and will
increase by 1 every time until it reaches 10.

This can be used to process items from a list box. e.g.

Dim i As Integer
For i=0 To List1.ListCount
List2.AddItem List1.List(i)
Next i

This code copies all the list items from List1 to List2.

The Do..Loop is a good way to loop a certain condition depending
on different criteria. e.g.

Do
Msgbox "Hi"
Loop

The problem with this code is that it will keep on going forever.
You can of course set certain conditions. e.g.

Dim i As Integer
i=10
Do While i>0
i=i-1
Loop

This code will count down from 10 and when i is 0, the loop ends.
Another use of this loop is:

Dim i As Integer
i=10
Do Until i=0
i=i-1
Loop

This code will continue until i is 0, and then stop.

The If..Then..Else statement is one used a lot by programmers. It
checks if a certain condition is true and then executes a statement. The Else statement is
used if the statement does not comply with the criteria. e.g.

Dim i As Integer
i=10
If i=10 Then
Msgbox "i=10"
Else
Msgbox "i is not 10"
End If

You can also use nested If..Then..Else statements. e.g.

Dim i As Integer
i=10
If i=10 Then
If i>9 Then
Msgbox "i>9"
Else
Msgbox "i<9"
End If
Msgbox "i=10"
Else
Msgbox "i does not = 10"
End If

Easy.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories