A Guide to Commands in Visual Basic

Contents

What is a Command?

A command is a specific instruction given to a computer application to perform a task or function. When referring to a programming language, a command is a unique word/keyword used to perform a specific operation.

Unlike variables, which are named and defined within your code, command names are defined by the programming language itself and cannot be changed. Differences in the names and use of different commands are essentially what make one programming language different from another.

As a programmer, you may be able to accomplish the same task coding in either Python or Visual Basic, but the code involved would be completely different between the two projects because of the way each language defines and interprets commands. Visual Basic, like any other programming language, employs commands. In this article, you are going to learn more about Visual basic commands and how to use them in your code.

Visual Basic Commands

In Visual Basic programming there is a multitude of commands and it will not be possible to list all of them in this article. Few of them will be studied here just for the purpose of understanding how they function.

Commands for String Manipulation

Asc

Function

Returns the number corresponding to the ASCII code for the first character of string

Syntax

Asc(“String”)

string refers to the string containing character as first letter whose ASCII code is to be found.

Example

In this example we are going to look for the ASCII (American Standard Code Information Interchange) code of the first letter of the word “Happy” hoping that you are happy reading this article. The code will be written in an old version of a Visual Basic IDE (VB6) and a new version (VB.NET). The screenshot will be done just for this example. In the other examples we shall give only the expected outcome. Do the following:

  1. Create a new project in your VB IDE (VB6 or VB.NET)
  2. Double Click on the default form that has been created automatically in the project.
  3. Write the following code in the “Form_Load” procedure that was created when you double-clicked on the form in the previous step.
Dim code
code = Asc("Happy")
MsgBox "The ASCII code is " & code

Screenshot from a VB.NET IDE

vb.net IDE

Screenshot from VB6 IDE

vb6 ide asci code

Note: This command can be useful when you have a problem with a key on your keyboard. For example if you have a problem with letter “H” on your keyboard, knowing that the ASCII code for “H” is 72 as seen in the example above, if you type ALT + 72 on a word processor or an IDE editor, that will give you letter “H” (or Alt + 104 for “h”).

Chr

Function

Returns a string character that corresponds to the ASCII code in the range 0-255. It is the reverse of the Asc function.

Syntax

Chr(code)

code = ASCII code.

Example

char = Chr(72)

Here char gets value “H”

Read the full tutorial about Chr here.

Len

Function

Returns the length of a given string or 0 for Empty.

Syntax

Len(expression)

expression = a string

Example

Mystring = InputBox("Enter a string to get the length")
length = Len(Mystring)
MsgBox "The length of the string is " & length

e.g. if Mystring is “Welcome”, length will be 7.

Read the full tutorial about Len() here.

Left

Function

Returns a given number of characters from the left-hand side of a string

Syntax

Left(string,x)

string = string to use
x = number of characters

Example

mystring = InputBox("Enter a string")
mystring = Left(mystring, 5)
MsgBox "First five characters of your input are " + mystring

e.g. where the input mystring is “Welcome”, the output mystring will be “Welco”

Right

Function

Returns a given number of characters from the right-hand side of a string

Syntax

Right(string, x)

string = string to use
x = number of characters

Example

mystring = InputBox("Enter a string")
mystring = Right(mystring, 4)
MsgBox "Last four characters of your input are " + mystring

e.g. where the input mystring is “Welcome”, the output mystring will be “come”

For more info about substring functions, see our article here.

Trim

Function

Removes leading and trailing spaces from a string

Syntax

Trim(string)

string = string to use

Example

mystring = Trim(mystring)

e.g. where the original value of mystring was ” Hello “, the new value of mystring will be “Hello”.

When using Trim, make sure you don’t run into common errors that may come up.

LCase

Function

Converts a string to lowercase (while Ucase converts a string to uppercase)

Syntax

Lcase(string)

string = string to use

Example

mystring = LCase(mystring)

e.g. where the original value of mystring was “HELLO”, the new value of mystring will be “hello”.

String

Function

Creates a string with the specified length of the specified character

Syntax

String(length, character)

length = length of string
character = character to fill string with

Example

mystring = String(5,"a")

e.g. the new value of mystring will be “aaaaa”.

Mathematical Functions

Abs

Function

Returns the absolute value of a number

Syntax

Abs(number)

Example

msgbox "The absolute value of -4 is " & abs(-4)

Note: The Abs function only accepts numbers. Non-numeric values generate an error.

Int

Function

Returns the integer portion of a number.

Syntax

Int(n)

Where n is the number to return

Example

Int(10)

Returns 10

Int(5.1)

Returns 5

Input and Output Commands

MsgBox

Function

The MsgBox function displays a message in a dialog box, waits for the user to click a button, and returns an Integer value indicating which button the user clicked.


Syntax

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

Examples


MsgBox "An error has occurred!",vbExclamation,"Error"

message box stating "an error has occurred"


Response=MsgBox("Yes or no?",vbYesNo + vbQuestion,"Choose one")

message box asking "yes or no?"

InputBox

Function

Displays a simple input box for user to enter data

Syntax

Variable=InputBox(Prompt,Title)

Variable = variable to which the input value will be stored Prompt = text displayed to the user in the input box Title = title of the input box

Examples

myint=InputBox("Enter a number","Enter a number")

input box saying "enter a number"

Date and Time

Date

Function

Returns or sets the current system date

Example

MsgBox "Today 's date is " & Date & "."

Time

Function

 Returns or sets the current system time

Example

MsgBox "The time now is " & Time & "."

Timer

Function

Returns the number of seconds since midnight

Example

NumHrs=Int(Timer / 3600)

MsgBox "Currently in hour " & NumHrs

One thought on “A Guide to Commands in Visual Basic”

Leave a Reply

Your email address will not be published. Required fields are marked *