Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Statements Overview

A statement in Visual Basic is a complete instruction. It can contain keywords, operators, variables, constants, and expressions. Each statement belongs to one of the following two categories:

  • Declaration statements, which name a variable, constant, or procedure and can also specify a data type. For more information, see Declaration Statements.
  • Executable statements, which initiate actions. These statements can execute a method or function, and they can loop or branch through blocks of code. Executable statements include assignment statements, which assign a value or expression to a variable or constant. For more information, see Executable Statements and Assignment Statements.
  • Declaration statements, which name a variable, constant, or procedure and can also specify a data type. For more information, see Declaration Statements.
  • Executable statements, which initiate actions. These statements can execute a method or function, and they can loop or branch through blocks of code. Executable statements include assignment statements, which assign a value or expression to a variable or constant. For more information, see Executable Statements and Assignment Statements

Having Multiple Statements on One Line

You can have multiple statements on a single line separated by the colon (:) character. For example:

Dim MyString As String = "Hello World" : MsgBox(MyString)

Though occasionally convenient, this form of syntax makes your code hard to read and maintain. Thus, it is recommended that you keep one statement to a line.

Continuing a Statement over Multiple Lines

A statement usually fits on one line, but when it doesn't, you can continue a long statement onto the next line using a line-continuation character, which consists of an underscore character (_) followed by a carriage return. In the following example, the MsgBox executable statement is continued over two lines:

Public Sub DemoBox()   'This procedure displays a message.
   Dim myVar As String
   myVar = "John"
   MsgBox("Hello " & myVar & _
      ". How are you?")
End Sub

Adding Comments

Source code is not always self-explanatory, even to the programmer who wrote it. To help document their code, therefore, most programmers make liberal use of embedded comments. Comments in code can explain a procedure or a particular instruction to anyone reading or working with it later. Visual Basic ignores comments, however, when it runs your procedures.

Comment lines begin with an apostrophe (') or REM followed by a space, and can be added anywhere in code. To append a comment to a statement, insert an apostrophe or REM after the statement, followed by the comment. Such a comment appears in the preceding example. Comments can also go on their own separate line.

Checking Compilation Errors

If, after you type a line of code, the line is displayed with a wavy blue underline (an error message may display as well), there is a syntax error in the statement. You must find out what's wrong with your statement (by looking in the task list, or hovering over the error and reading the help text, for example) and correct it. Until you have fixed all syntax errors in your code, your program will fail to compile correctly.

See Also

Assignment Statements | Declaration Statements | Executable Statements | Labeling Statements

Show:
© 2015 Microsoft