The Firstapp Sample Application

Visual Basic provides you with a wealth of tools beyond the ones used in this first application, so you'll soon use many other features to manage and customize your applications. Reviewing sample applications can be an excellent way to learn more about Visual Basic. The following example illustrates how easy it can be to create a useful application in Visual Basic.

The Firstapp application demonstrates how a data control and a grid control can be used to display a table of information from a database. Visual Basic makes it easy to access database information from within your application. The data control provides the ability to navigate through the database recordset, synchronizing the display of records in the grid control with the position in the recordset.

The application consists of a data control, a MSFlexGrid control, a list box control, and two command buttons. The grid displays a table of information about products retrieved from the Northwind database. As the user selects an item by using the navigation buttons on the data control, the name of the selected product is displayed in the data control. The user can also add items to a "shopping list" in the list box control by double-clicking the current selection in the grid.

To add items to the list box, you use the AddItem method. (A method is a Visual Basic function that acts on a particular object, in this case a ListBox object.) The syntax for specifying a method (object.method) is similar to the syntax for setting a property (object.property). The AddItem method allows you to dynamically add items to the list box while the application is running. Conversely, the Clear method is used to remove all items from the list box.

For More Information   To learn more about methods, see "Understanding Properties, Methods, and Events" in "Forms, Controls, and Menus."

Creating a Project

You begin creating this application by choosing New Project from the File menu, then selecting Standard EXE in the New Project dialog box (when you first start Visual Basic, the New Project dialog box is presented). Visual Basic creates a new project and displays a new form. To draw the interface, you use a data control, a MSFlexGrid control, a list box control, and two command buttons. The MSFlexGrid control isn't in the default toolbox, so you'll need to add it:

To add a control to the toolbox

  1. Select Components from the context menu for the toolbox. (You can right-click within the toolbox window to display the context menu.)

    The Components dialog box will be displayed.

  2. Find the MSFlexGrid (Microsoft Flex Grid 6.0) in the Controls list box and select the check box to its left.

  3. Click the OK button.

The icon for the MSFlexGrid control will appear in the toolbox.

Use the Toolbox to draw a data control, an MSFlexGrid control, a list box control, and two command buttons on the form. If you don't remember how, check out "Creating the Interface" earlier in this chapter.

Setting Properties

In the Properties window, set properties for the objects according to the following table. Use the default settings for all other properties.

Object Property Setting
Form Caption Products
Data1 DatabaseName
RecordSource
path \Nwind.mdb
Products
MSFlexGrid1 DataSource Data1
Command1 Caption Clear
Command2 Caption Exit

The DatabaseName property of the data control must include the actual path to the database. By default, the Nwind.mdb database is installed in the same directory as Visual Basic. When you select the DatabaseName property in the Properties window, you can click the button to the right of the property to display a standard File Open dialog box to browse for the file. Once the DatabaseName property has been set, the RecordSource property in the Properties window will contain a list of tables or recordsets for the selected database. Setting the DataSource property of the MSFlexGrid control to Data1 automatically links the grid to the data control.

Writing Event Code

All the code for the application is contained in the Command1_Click, Command2_Click, Data1_Reposition, and MSFlexGrid1_DblClick event procedures. Double-click the form or control to display the Code window, and then type the code for each event procedure.

Add this code to the Command1_Click event procedure to clear the list box when the user clicks the button:

Private Sub Command1_Click ()
   List1.Clear               ' Clears the list box.
End Sub

In the above statement, you are invoking the Clear method of the list box, List1. The Clear method deletes the contents of the list box.

Add this code to the Command2_Click event procedure to unload the form from memory and end the application:

Private Sub Command2_Click ()
   Unload Form1
   End                     ' Ends application.
End Sub

In the above procedure, the first statement invokes the Unload event for the form. If you needed to perform a function at shutdown, such as saving a file, you could place that code in the form's Unload event procedure. The second statement calls the End function, which ends the application.

Add this code to the Data1_Reposition event procedure to update the caption each time a record is selected:

Private Sub Data1_Reposition ()
   Data1.Caption = Data1.Recordset("ProductName")
End Sub

In the above statement, you are assigning the value on the right (the contents of the Title field in the Recordset of the data control) to the property on the left (the Caption property of the data control object).

Add this code to the MSFlexGrid_DblClick event procedure to add an item to the list box when the user double-clicks a selected row:

Private Sub MSFlexGrid1_DblClick ()
   List1.AddItem MSFlexGrid.Text
End Sub

In the above statement, you are invoking the AddItem method of the list box (List1). The text to be added to the list box is contained in the argument of the method, in this case, the value of the title field in the recordset of the data control. Passing a value to an argument is similar to assigning a value to a property; unlike the assignment statement, the equal sign isn't required.

Saving a Project

You finish your work on the application by choosing Save Project from the File menu. Visual Basic will prompt you separately to save the form and then the project. One possible name for the project is "Northwind Shopping List." Both Windows 95 and Windows NT allow you to use file names up to 255 characters in length, and file names can include spaces. Older versions of Microsoft Windows limited you to file names of eight characters, with a three-character extension.

Enhancing Your Application

You have just completed your first Visual Basic application: one that performs a simple but useful function. You can use this application as a basis for adding similar functionality in your own applications, substituting your own data instead of Nwind.mdb. Of course, to make this application truly useful, you might want to add functionality to save or print the contents of the list box, to add additional information such as price and availability, and even to gather credit card information and transmit an order across the Internet. As you continue on through the rest of the Programmer's Guide, you will find examples of doing all that and a lot more.