MS Access 2010 VBA tutorial- Operators


VBA Operators

When writing VBA expressions, you will need to use VBA operators. In an expression, there are operands and operators to produce a final value. The followings are commonly used operators in VBA programming:

-Assignment Operator

The assignment (=) is used to assign value to variable.

Example:

Dim x As Integer x=10  

-Arithmetic operators

The arithmetic operators (+,-,*, /, mod, ^) are used to do math operations such as addition, subtraction, multiplication, division, modulus of division, and exponential computation. Example: Dim x As Integer Dim y As Integer Dim sum As Integer   x=10 y=20 sum=x+y
MsgBox “Total=” & sum  

-Logical operators

The logical operators are used to do logical operations such and, or, exclusive or, and not. The most commonly used VBA logical operators are And, Or, Xor, and Not.
 
The Not operatorneeds only one operand. It used to contrast the value of the operand.

Example:

x=True

y=Not x 'then y get False value

 

-String concatenation operator (&)

It is used to concatenate sub-strings.
Example:

MsgBox “Lastname: “ & “Yuk”  

-Line feed operator (Vbcrlf)

This operaor is used to make a new line in the text output.
Example:

Msgbox “Lastname:”& “Yuk” & Vbcrlf & “Firstname:” &”Dara”
 

-Underscore operator(_)

The underscore operator is used to continue code when it is too long to write it on a single line.
Example:
Sub_
 printval End Sub  

-Comparison operators (=,>,<,>=,<=,<>)

The operators are used to compare values. The results from using comparison operators are true or false.

Example:

Dim f As Boolean
f=10>5 Msgbox f  

-Comma operator (,)

The comma operator is used to separate variables declare.
Example:
Dim x, y, z As Single


Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.