Accessories for Code Writing


Code Writing


The Indentation

Microsoft Visual Basic makes it very easy to write code by providing you skeleton code as we saw above, a very intuitive Code Editor, and other accessories. Indentation is a technique that allows you to write easily readable code. It consists of visually showing the beginning and end of a section of code. Indentation is done at various levels. For the code of an event, indentation consists of moving your code to the right side so that the only line to the extreme left are those of the Private and End Sub lines.

The easiest and most common way to apply indentation consists of pressing Tab before typing your code. By default, one indentation, done when pressing Tab, corresponds to 4 characters. This is controlled by the Module property page of the Options dialog box. To change it, on the main menu of Microsoft Visual Basic, you would click Tools -> Options and use the Tab Width text box of the Editor property page:

If you don't want the pressing of Tab to be equivalent to 4 characters, change the value of the Tab Width text box to a reasonable integer value and click OK. Otherwise, it is (strongly) suggested that you keep to its default of 4 characters.

Practical LearningPractical Learning:  Using Indentation

  1. Click anywhere on the line that has NavigationButtons and press Home

  2. Press Tab to indent the line to the right

  3. In the same way, indent the other line of code:
    Private Sub Form_Load()
        NavigationButtons = False
        [Company Name].SetFocus
    End Sub


A comment is a piece of text in a code section that the database engine would not consider when reading your code. As such, a comment can be written any way you want.

In Visual Basic, the line that contains a comment can start with a single quote. Here is an example:

Private Sub Form_Load()
    ' This line will not be considered as part of the code
End Sub

Alternatively, you can start a comment with the Rem keyword. Anything on the right side of rem, Rem, or REM would not be read. Here is an example:

Private Sub Form_Load()
    ' This line will not be considered as part of the code
    Rem I can write anything I want on this line
End Sub

Comments are very useful and it is strongly suggested that you use them regularly. They can never hurt your code and they don't increase the size of your database. Comments can help you and other people who read your code to figure out what a particular section of code is used for, which can be helpful when you re-visit your code after months or years of not seeing it.


Line Continuation

It is usually suitable to write lines of code that are not too long. This makes it easy to read code and avoid scrolling left and right. In some cases, you will not have much choice but to create long expressions. Unlike many other languages such as C/C++/C#, Java, etc, Visual Basic doesn't allow to simply continue a line of code from one line to the next without alerting the compiler. Still, if a line of code becomes too long, there is a technique you can use to span on various lines.

To continue a piece of code from one line to the next, type an empty space followed by an underscore symbol, then continue your code on the next line.


Fundamental Built-In Objects


Introduction to Built-In Objects

To lay a solid foundation and assist you in creating your applications, Microsoft Access ships with various pre-configured objects. These start with any primary thing that appears on the MS Access interface: the menus, the toolbars, and the Database window. Other objects are almost hidden but you can access them any time when needed.


The Object Object

As mentioned earlier and as you will find out for the rest of our lessons, a Microsoft Access database is made of various objects and various types of objects. All of these have in common that they are primarily considered as objects. To represent them, each object is of type Object. Sometimes, before using an object, you may not know exactly what particular type it would be. In Lesson 4, we will learn how to declare variables and how you can declare a variable for any type of object using the Object type.


The Application Object

When you create a database using Microsoft Access, you are said to have created an application. To identify the database you are currently using or that is opened, Microsoft Visual Basic provides the Application object. The Application object contains all of the object that you create or use and that belong to the database you are working on.

If you are planning to refer to the current database in your code, you can directly use the Application object. If you want to refer to a database other than the one that is currently opened, you should first declare an Application variable. In Lesson 3, we will learn how to declare a variable and how to initialize it.


The Current Database

When working in a Microsoft Access application, the database that is currently opened is identified as the CurrentDb object. This object is a child of the Application object. The CurrentDb object allows you to refer to the current database in your code.


The Objects in the Current Database

In the next few lessons, we will learn different techniques of creating the objects that would belong to a database. While using an application, the objects that belong to the database are stored in a collection called CurrentData. The CurrentData object itself is a property of the Application object. To access the CurrentData object, you can first call the Application object, type the period operator, and type CurrentData. This would be done as follows:


The CurrentData object holds the collections of objects that belong to the database.


An Access Object

There are various types of objects you will use in your databases. As we will learn in different lessons, each object belong to a particular collection. Still, to generally identify these various objects, each is identified as an AccessObject. This means that the AccessObject object is used to identify an object that belongs to a collection, whatever that object is and whatever its parent collection.


The DoCmd Object



When using a database, the user routinely performs actions such as opening or closing an object, etc. An action performed on a database is also called a command. To carry the various actions that can be performed on a database, Microsoft Access provides an object called DoCmd.


DoCmd Methods

The Methods of the DoCmd are:

AddMenu ApplyFilter Beep CancelEvent Close
CopyDatabaseFile CopyObject DeleteObject DoMenuItem Echo
FindNext FindRecord GoToControl GoToPage GoToRecord
Hourglass Maximize Minimize MoveSize OpenDataAccessPage
OpenDiagram OpenForm OpenFunction OpenModule OpenQuery
OpenReport OpenStoredProcedure OpenTable OpenView OutputTo
PrintOut Quit Rename RepaintObject Requery
Restore RunCommand RunMacro RunSQL Save
SelectObject SendObject SetMenuItem SetWarnings ShowAllRecords
ShowToolbar TransferDatabase TransferSpreadsheet TransferSQLDatabase TransferText

Because the DoCmd is equipped with so many methods, we will review the necessary ones as each is introduced at the appropriate time.




Previous Copyright © 2005-2010 FunctionX, Inc. Next