During this hour you will learn
The final part of this book begins the major Fast Track section that walks you through the building of a complete application from start to finish. Although you've seen applications built throughout this entire course, this part of the tutorial takes things a step further. You'll learn how to design, develop, debug, and package your complete application. This and the next four hourly lessons don't follow the usual three-topic sections that you've become accustomed to. Instead, these lessons raise your application-building skills.
If you've worked with Visual Basic before or are already a strong developer in other languages, you might have jumped directly to this part of the book to see what Visual Basic is all about. You won't be disappointed! If you're brand new to Visual Basic and you jumped to this part of the book, you'll be excited to see the results of what the first 32 lessons will teach you when you go through them sequentially.
If you've followed the book sequentially from the first page to this point, this part of the book doesn't just offer review. Each hour's lesson walks you through an application's development, but each lesson also trains you in some advanced development ideas and features that the previous hours couldn't get to. In other words, this part of the book takes you one step beyond your current skill level to initiate you into the ranks of an advanced Visual Basic developer.
The application you learn here is a complete stand-alone application that rivals many applications sold today. The application is fairly simple, but its simplicity is its beauty. The application you develop in this and the next four hours offers an easy-to-use contact management system with an appointment manager. This VBScheduler application will advance your skills.
Figure 33.1 shows VBScheduler's form window. By the time you complete this and the next four lessons, you'll have completed the VBScheduler application yourself. These lessons, however, don't just walk you through the step-by-step creation-you've had enough of that for now. These lessons teach you about designing, debugging, and distributing your application and use the VBScheduler application for a teaching example.
The VBScheduler application's window forms the appointment scheduler.
Now that you have a glimpse of the application these lessons describe, you're ready to learn more about application planning and design.
FAST TRACK |
Turn to Hour 2, "Creating Your First Application", to learn how to use Visual Basic's development environment to load and execute programs. |
The most difficult part of creating any application is deciding what it will do and how that should be done. The most important part of creating an application is in the design process. If done correctly, the design takes as long or longer to work out than the application itself. Unfortunately, many programmers don't spend as much time designing the application as they should. They would prefer jumping right in and starting to create the application. For every hour that you spend designing your application, however, you can save as much as a day of debugging time. If you start with a good design, you consider more of the situations that might present problems later. You then can head off small problems before they become bigger ones.
As a general rule, it's always easier to fix a design problem before the program is created.
![]()
To arrive at a good design, you need to understand what makes up a good application. Both a house and an application must be built according to a carefully planned set of steps. Following this plan prevents the house's roof from being built before the walls. In a Visual Basic application, you usually shouldn't add any code before you create the forms. Although you might see slight modifications that you want to make to the forms as you add code in a later step, the forms should be fairly solid before you begin to add code. Without the user's interface firmly in mind, your early code is prone to design flaws that consume lots of time to modify.
If you build the application in the wrong order, or leave a part out, it's very hard to code and even harder to test. The application can take longer to complete or might never work at all. To design an application, you must first understand what you want it to do.
Applications comprise the following major components:
FAST TRACK |
Turn to Hour 1, "Introducing Visual Basic", to learn more about the program code that comprises many programs. |
You should set up your applications in their own Windows folder because applications often require several auxiliary files, such as a Readme.txt file for last-minute documentation fixes and release information. The folder should contain your application with an appropriate matching program icon. (The form's Icon property determines the icon displayed on the taskbar when your program runs or is minimized.)
Compile all your applications before distributing them. Keep your applications running interactively inside the development environment only for as long as you create and test the application. Compile the application before distributing the application. Hour 37, "Packaging Your Application," explains application distribution in detail.
![]()
The first step in the application design is to put your application into words. This description phase is usually done by writing a short goal-oriented paragraph that describes exactly what the application is and what it will be able to do. The following is an example of such a description for the VBScheduler:
This description gives you a good idea of what functions the application will do, providing a final goal to aim for as you design the application. The next section discusses application tasks in more detail.
As you know from earlier lessons, a Visual Basic application's code consists of program building blocks called procedures. Although you aren't ready to write procedures when you finish the goal statement, you're ready to begin breaking down your application into smaller tasks that eventually help you determine which procedures are needed.
Breaking the overall goal into smaller tasks is called top-down design.
![]()
One way to determine the tasks the application requires is to ask questions about the application. For example, how complicated will this application be? Will the application print day-planning sheets for the daily appointments? Should users be able to search for an appointment by entering text that might appear in an appointment? Will the application support multiple data files for a project team's usage? Who will the users be-families, small businesses, or both? Also, how many tasks will you include in it, given your time commitments and project costs? The answers to these questions help you build a features list. Some tasks are absolutely required for the application to work, whereas other tasks might be nice to have but aren't required.
Following are some of the more important tasks that must be in a scheduling application:
This is far from a complete list of tasks, but you're headed in the right direction. Already, you're thinking about the form (or forms) required and of coding approaches to such tasks. As the design progresses, you find that you should add more tasks.
When designing in Visual Basic, you should start by designing the forms you might need, and then change them as you go along. This technique, called prototyping, lets you create a demo as you design actual programs. Prototyping also lets you decide what should happen when a given button on the form is clicked or when a menu option is selected.
Visual Basic is one of the best programming systems on which to prototype. The Form window makes designing, creating, and testing a user interface simple. After you design your form (or forms), put one of the application's users in front of the running application prototype to make sure that all the controls are placed well and that you've included all the interaction needed for the program to produce its intended result.
You must always realize that the design you come up with isn't set in cement. You should go back over the design several times with several people (preferably end users who will use the eventual application) looking for possible problems in logic before you start building the actual application and tying the controls together with code. This critical plan review is an important part of the design process. Also, you learn more about Visual Basic and programming this way as you build the application. Some of these things make the application better, but only if you can go back and rework the application's task list. Allow yourself to go back and change the design, especially this early in the creation, and the application will come out better due to proper prototyping and proper review.
A Windows application is a collection of related forms that allows users to interact easily with the data. As you might have seen in other Windows applications, some applications can be very useful, but boring to look at or use. Throughout this tutorial, you've brought many Visual Basic objects together to create simple applications that were interesting to look at. By using what you've learned about VB's features, in addition to some design standards that you'll learn here, you can create useful applications that look good and are easy to use. However, no matter how closely standards are followed, an application's forms, to some degree, reflect the personality of the programmer who created them.
For the rest of this hour and the next, I'm going to cover how to design a form, what you should place on the form, and where the objects should be placed. Also, you'll see how the menus are created and how they enhance the application.
Remember that this part of the book is a Fast Track part. Therefore, some of you are reading this but haven't mastered all the previous hours' topics. Therefore, along the way, this and the next four lessons review some of the application-creation process as well as describe application design theory.
![]()
The form is the most important part of any application. If the interface isn't easy or just doesn't look good, nobody will want to use it. The GUI (Graphical User Interface) offers a wide variety of ways of making users feel comfortable with the application. The users' experience must be taken into account when considering the layout of the forms.
FAST TRACK |
Turn to Hour 2, "Creating Your First Application", to learn more about forms. |
When you design a form, you should maintain as much consistency as possible. Keep the following design concepts and tips in mind:
Look at each form in the application as having a distinct job to do. Most applications have many different types of forms. All applications have a main form (also called a parent form), which comprises the application's frame. All other objects and forms are selected and displayed from the main form.
Again, VBScheduler is fairly simple, so it has only one form (the main form). If you create a multiform project, however, keep track of the forms you add to the project and document each form's purpose and contents as you go along.
![]()
Updating Documentation
As you create your application, update your documentation. Your application might contain different kinds of documentation. You might want to document the application internally with remarks so that you can more easily maintain the project later. Also, you might want to keep a notebook handy with form and control names and their descriptions so that you have a system documentation paper for the project.
End users need documentation as well. You should supply online help and ToolTips where appropriate. Also, write a user manual so that users can learn the program without your help. The best time to write the system and user documentation is during the design and development of the project.
Therefore, documentation is an ongoing task that isn't over until you finish the project. Even then, your users will suggest improvements that you'll want to track so that you can offer updated software and documentation periodically.
Whenever you start Visual Basic or open a new project (by choosingNew
Project from the File menu), Visual Basic displays Figure
33.2's New Project dialog box. You'll almost always double-click
the icon labeled Standard EXE because most of the projects you
create become a compiled executable program with the .EXE file
name extension.
FAST TRACK |
Turn to Hour 3, "Adding Controls to a Program", to learn how to create new projects and add controls to forms. |
Double-click the Standard EXE icon to build a new project.
After you select the Standard EXE icon, Visual Basic opens a new project and puts a blank Form window on-screen. This initial form is the main form and provides, for the VBScheduler application, the only form used.
Prepare the form for the VBScheduler application as follows:
Property | Value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Your Form window should now look like the one in Figure 33.3.
Your Form window is now set up properly.
Users of VB's Standard, Professional, and Enterprise Editions can develop the VBScheduler application because it uses no additional controls than what appears by default in the Toolbox. Ordinarily, you choose C
omponents from theProjects menu to add new ActiveX controls if you need to work with controls that you don't find on the regular startup Toolbox.![]()
Any application must have a way of storing the information that it works with. Selecting the method for storing the data is very important. Some applications use a sequential or random-access file, often called a flat file, while other applications use a database created by another application such as Microsoft Access or Borland Paradox. (Visual Basic supports the use of several vendors' databases, including the industry-standard ODBC database format.)
FAST TRACK |
Turn to Hour 21, "Accessing Files", to learn more about data files. Turn to Hour 27, "Accessing Databases", to learn how to use Visual Basic's development environment to load and execute programs. |
With the inclusion of Visual Basic's Data control that manipulates a database-as well as the data-bound controls, such as text boxes, that automatically update the underlying database if users change the data-you'll almost certainly want to use a database file for your program's data storage.
Which database should you use? Dozens of commercial databases exist on the market today, such as dBASE, Paradox, and Microsoft Access. Most work with Visual Basic's Data control, as mentioned in the previous section, so compatibility is rarely the determining factor in your database selection.
Although the database selection should primarily rest with the database that best suits your skills (or your company standards), more and more Visual Basic developers are turning to Microsoft Access because of its close relationship to Visual Basic. Using Microsoft Access requires nothing special other than locating the database file that you want to work with.
Some databases, such as Watcom's, requires special Visual Basic Open Database Connectivity (ODBC) drivers that don't normally install to your PC at VB setup time. Therefore, if you use an ODBC database, you might need to run Visual Basic's Setup program again and add the appropriate drivers. For more information, search the online help for ODBC.
![]()
As the amount of stored data increases, the need to have efficient ways of storing and accessing the data becomes a priority. In the sequential-file approach to data, every record needs to have all the data required for the process (for example, name, payee, and so on) included in the record. This duplication of data is a very large waste of disk space and processing time. You need a way to relate, or link, several separate files to each other without so much redundancy. This relation ensures that data that needs to appear several times throughout the database can be kept as a single entry in a related table. The relational database concept, supported by most of today's database systems such as Microsoft Access, is an organized collection of data tables that your application can access and update in a variety of ways.
Although a sequential file is sometimes still used, most applications today use some form of relational database.
![]()
The VBScheduler database is relatively simple. Two related files are needed: one to hold the contacts and one to hold the appointments. Look at the database layout in Figure 33.4 to see how having certain information in separate tables makes it easier to control the data. After the tables are created, the database system links them together to get the data from the separate tables when needed. This linking is called a table join. A table is one file from a database collection of files. A table is joined by a field or fields in one table matching a field or fields in another table. While the data is processed, an entire record of information can be constructed from the different tables in the database.
Relational tables relate on a common field.
Figure 33.4 describes the database tables needed for VBScheduler. Only two tables are required: the contact table and the appointment table. The appointment table doesn't need to store the contact's name because the appointment table can simply hold the contact's record number (in the field named Rec_Num). The database format is Microsoft Access.
A big advantage that relational databases offers is the built-in capability to access data by using various queries. A query is a way of requesting certain records from database tables. Most relational databases have a standard language used to communicate with the database. This language is called SQL, or Structured Query Language. An example of a SQL request is
Select * from tblContacts order by tblContacts.LastName
This query retrieves all contact information from the contact table in last-name order. Indexing can speed up the retrieval process by putting the records in a particular order before your application searches the table. Your database can take care of the indexing as long as you state the index to use.
Although this lesson doesn't and can't delve into database design, you should understand that you can create just about any database you'll ever need with nothing more than Visual Basic. As Hour 27 explains in greater detail, Visual Basic's add-on tool called the Visual Data Manager lets you create, analyze, and modify databases in several popular database formats. VBScheduler's database is called VBSched.mdb. You can load and view this database by starting the Visual Data Manager and opening VBSched.mdb to show the database's format, such as the one in Figure 33.5.
Use the Visual Data Manager to analyze the database file.
The Visual Data Manager doesn't replace a database system, such as Microsoft Access, even though you can work with Microsoft Access database files from the Visual Data Manager. A full-blown database manager, such as Microsoft Access, gives you much better control over your data than the Visual Data Manager can.
![]()
The VBScheduler application that comes on this book's CD-ROM includes an initial database file with sample data so that you can analyze the database if you'd like from within the Visual Data Manager.
Obviously, much of this book devotes itself to teaching you the Visual Basic programming language-yet the language has not been mentioned to this point in this lesson. The code is the last part of the application that you finalize. If you've followed along through this lesson, you've built an initial form and set some form properties, but you've not added controls yet. You'll add controls in a couple of lessons.
This might seem like a strange topic to include in a chapter on how to create Visual Basic applications; however, knowing what not to do when creating an application is just is important as knowing what you should do. Why applications begin with design and why standards are important when building the application are discussed in the next two sections.
It should be clear by now why it's important to have a good design before starting to create an application. In case it's not, here are the reasons stated another way: Having a good design keeps you from guessing what to do next. Paying attention to the design helps minimize changes to an application after the building of the application begins. Most important, a good design makes the coding process much less tedious.
What if, during the building of a house, the builder forgot to put in the bedroom window? The builder would have to knock the wall down and start again, or cut out a window after the fact, which probably won't look as good as planned windows. The same idea applies to programming. If you find that you've forgotten something in a section of program logic, it can be very difficult to add the forgotten logic without completely rewriting the section of code or the entire application. However, if you find a logic problem early, you can change the program logic and simply move on. Finally, if you find a design problem, you have to alter the design to match the change. To accommodate the design change, you might have to remove some program logic that's already written.
Having programming standards when creating any computer program is very important. The better you name the routines and program variables, the easier it is to understand the code and find errors while you're testing code. You can see, for example, that the routine in Listing 33.1 is easier to follow than the one in Listing 33.2. (These listings aren't included on the CD-ROM because they're only examples.)
Listing 33.1 A Routine That Follows No Conventions
Private Function function1 (ByVal var1) As Integer Dim x as Integer, y As Integer Dim z As String If Right$( varl, 1) <> "\" Then varl = varl & "\" End If On Error Resume Next ChDrive varl If Err <> 0 Then GoTo label1 ChDir "\"var1 x = 3 y = InStr(4, varl, "\") Do While y <> 0 z = Mid$( varl, x + 1, y - x - 1) Err = 0 Mkdir z If Err <> 0 And Err <> 75 Then GoTo label1 Err = 0 ChDir z If Err <> 0 Then GoTo label1 X = y y = InStr(x + 1, varl, "\") Loop function1 = True Exit Function label1: If Err = 71 Then v = MsgBox("Please insert Disk in Drive") Resume End If function1 = False End Function
Although you aren't completely familiar with all aspects of Listing 33.2, the commented and better-structured code should make the routine fairly simple to you.
Listing 33.2 A Routine That Follows Standard Programming Conventions
' Create the path contained in DestPath ' First char must be drive letter, followed by ' a ":\" followed by the path, if any. ' ----------------------------------------------------- Private Function CreatePath (ByVal DestPath) As Integer Dim intBackpos As Integer, intForepos As Integer Dim strTemp As String ' ' Add slash to end Of path if hot there already ' ----------------------------------------------------- If Right(DestPath, 1) <> "\" Then DestPath = DestPath & "\" End If ' ' Change to the drive's root directory ' ----------------------------------------------------- On Error Resume Next ChDrive DestPath If Err <> 0 Then GoTo errorOut ChDir "\" ' ' Attempt to make each directory, than change to it ' ----------------------------------------------------- intBackpos = 3 intForepos = InStr(4, DestPath, "\") Do While intForepos <> 0 strTemp = Mid(DestPath, intBackpos + 1, intForpos - intBackpos - 1) Err = 0 MkDir strTemp If Err <> 0 And Err <> 75 Then GoTo errorOut Err = 0 ChDir strTemp If Err <> 0 Then GoTo errorOut intBackpos = intForepos intForepos = InStr(intBackpos + 1, DestPath, "\") Loop CreatePath = True Exit Function ' errorOut: If Err = 71 Then varPress = MsgBox("Please insert Disk in Drive") Resume End If CreatePath = False End Function
FAST TRACK |
Hour 6, "Understanding the VB Program Structure", begins the part of the book that teaches how to understand and write Visual Basic code. |
You should be able to tell exactly what a given routine is doing just by its name. Make variable names as long as necessary so that the purpose for each variable is plain. Also, each procedure name should reflect the task that it performs so that each is easy to read and understand.
The VBScheduler application follows a standard coding convention that generally matches that described in the rest of this tutorial. Some of the differences are a matter of style preferences. Therefore, you might want to adopt the earlier chapter standards or adopt the VBScheduler standards (many overlap).
![]()
This hour's lesson focused on application and form design. Designing and building an application isn't easy. As you read in this lesson, you need to think about a great many things before you code and watch for many pitfalls as you design. You might have noticed that this lesson avoided the term coding as much as possible. Applications are designed, created, and built rather than coded and written. You can never spend too much time up front designing an application. Of course, you must balance design time with the overall schedule and funds to ensure that you finish the project on time, but you'll finish on time more often and more accurately the more up-front time you put into an application.
Hour 34, "Adding the Controls," continues building the VBScheduler application by adding menus and controls to it. You won't add much code, however, until Hour 35, "Integrating Code," because the code is the final step before you test and debug the application.
No questions or exercises exist for this hour's lesson due to the general nature of the material.
![]()