Using Visual Basic 6
About the Authors
About the Authors
Bob Reselman is a principal consultant with the Des Moines office of Cap
Gemini, a worldwide consulting organization that helps businesses achieve transformation
through technology. Bob enjoys hearing from other developers. He can be reached at
[email protected].
Wayne Pruchniak has been a Human Factors Engineer with Gateway since 1995.
He spends much of his time designing software and trying to make computers a bit
less frustrating. After that, he can be found either listening to music, tuning his
home theater, or modifying his car. He currently resides in Sioux City, Iowa, and
can be reached at [email protected].
Richard A. Peasley is also a principal consultant at Cap Gemini, practicing
in the Information Technology Solutions Architecture group. His interests include
learning new technology, trying to read technical reference materials, and teaching.
When Richard isn't working, he's involved with his family and church in Elk Point,
South Dakota. He can be reached at [email protected].
Eric A. Smith is a senior consultant with Information Strategies, a Washington,
D.C.-based Microsoft Solution Provider. He specializes in Visual Basic development,
but is also experienced in Web and database programming. He is a frequent contributor
to Visual Basic Programmer's Journal and speaks at industry conferences. He is also
the creator of Ask the VB Pro, a Web site that provides how-to information on Visual
Basic (http://www.inquiry.com/thevbpro).
Eric can be reached at [email protected].
Dedication
I dedicate this book to my sister, Nancy, and the miracle of e-mail.
-Bob Reselman
I dedicate this book to my parents. Without you, I wouldn't be what I am today,
and I never forget.
-Wayne Pruchniak
To my wife and best friend, Sonja Swanson, and our two daughters, Sara and
Annie, for their support on the long weekends and late nights writing this book.
They are my life.
-Richard A. Peasley
For Jodi.
-Eric A. Smith
Acknowledgements
I very much want acknowledge the patience, support and understanding of Angela
Kozlowski, acquisitions editor extraordinaire. Without her continuing guidance and
help, this would have been an impossible project to do. Also I want to acknowledge
the continued perseverance of my wife, Dorothy Lifka, and my daughters, Alexandra
and Genevieve, for allowing yet another book into their lives.
--Bob Reselman
I'd like to thank Bob Reselman for giving me the opportunity to help write this
book. Also, thanks to Angela Kozlowski and Susan Dunn for their patience and advice.
And thanks to everyone at Gateway for giving me the opportunity to work on many different
things.
-Wayne Pruchniak
On the shoulders of giants and in the company of friends, the depth of our vision
and strength extends. Bob, it is a privilege to work with you professionally and
to know you as a friend. I would like to thank you for inviting me to the party,
Wayne for joining the party, Angela Kozlowski for hosting the party and helping me
work through a travesty of personal technical problems, and finally the editors and
production staff for making the pieces whole. Without the great team at Que, books
like these would not be possible. A commercial book is truly a collaborative work
of art.
-Richard A. Peasley
Tell Us What You Think!
As the reader of this book, you are our most important critic and commentator.
We value your opinion and want to know what we're doing right, what we could do better,
what areas you'd like to see us publish in, and any other words of wisdom you're
willing to pass our way.
As the Executive Editor for the Client/Server and Database team at Macmillan Computer
Publishing, I welcome your comments. You can fax, e-mail, or write me directly to
let me know what you did or didn't like about this book--as well as what we can do
to make our books stronger.
Please note that I cannot help you with technical problems related to the topic
of this book, and that due to the high volume of mail I receive, I might not be able
to reply to every message.
When you write, please be sure to include this book's title and author as well
as your name and phone or fax number. I will carefully review your comments and share
them with the author and editors who worked on the book.
Fax: |
317-817-7070 |
E-mail: |
[email protected] |
Mail: |
Executive Editor |
|
Client/Server and Database Team |
|
Macmillan Computer Publishing |
|
201 West 103rd Street |
|
Indianapolis, IN 46290 USA |
Introduction
THIS BOOK IS NOT ONLY FOR beginning programmers who want to learn Visual Basic
but also experienced programmers who are familiar with other programming languages
and seek a working knowledge of Visual Basic. Every page of Using Visual Basic
6 provides you with useful facts and techniques. The aim is to teach you how
to program by showing you programs--sometimes step by step and sometimes with a conceptual,
from-the-ground-up point of view.
The author of this book assumes that you have a working knowledge of Windows,
that you've done word processing or used a spreadsheet, and that you know your way
around the Windows interface. It's also assumed that you have a copy of Visual Basic
6, access to a computer, and time to review the topics and work with the examples
in each chapter. Very, very rarely can someone read a book on computer programming
and instantly "get it" without taking the time for some hands-on experience.
Learning a programming language is a challenging experience. The author and technical
reviewers of this book are professional programmers who have made every effort to
convey the fun and challenge of VB programming while addressing your need for relevant,
easy-to-understand material. If Visual Basic programming is something that you want
to do, either professionally or as a side activity, and you need a book that instructs
in a clear, concise, real-world manner, this is for you.
Why This Book?
Have you ever purchased a Using book from Que? For many years, the Using
books have proven invaluable to readers as both learning guides and references. The
Using series is an industry leader and has become practically an industry
standard. We encourage and receive feedback from readers all the time, using their
suggestions whenever possible.
This book isn't a compiled authority on all the features of Visual Basic. It's
a streamlined, conversational approach to productive, efficient programming in Visual
Basic. Features of this book include
- Relevant information written just for you. We have carefully scrutinized
which features and tasks to include in this book and have chosen those that apply
to your everyday use of Visual Basic. Why invest in material that teaches you how
to perform tasks you might never need to perform?
- Reference or tutorial. You can learn to quickly perform a task by using
the step-by-step instructions, or you can investigate the why and wherefore of a
task in the discussions preceding each task.
- Wise investment. We don't waste your valuable bookshelf space with redundant
or irrelevant material, nor do we assume that you know it all or need to know it
all. Here is what you need, when you need it, and how you need it--with an appropriate
price tag.
- Easy-to-find procedures. Every numbered step-by-step procedure in the
book has a short title explaining exactly what it does. This saves you time by making
it easier to find the exact steps necessary for accomplishing a task.
How to Use This Book
You can use this book in various ways:
- If you are a beginning programmer, the best way to go about using this
book is to read each chapter in sequence. Some chapters build on knowledge shared
in previous chapters. If you skip around, you might be confused because you missed
a concept or technique introduced in other areas of the book.
- If you are an experienced programmer, skipping around might be the better
way to go. If you have mastered some of the conceptual items--such as variables,
arrays, loops, and conditional statements--reading chapters at random would make
the best use of your time. The book is designed so that each chapter can more or
less stand on its own.
Each page of the book contains the main text and margin notes. These margin notes
are helpful when you're looking for brief snippets of information. The book is also
full of figures, code listings, and tables that you will find useful now and in months
to come as you refer to this book in your day-to-day programming activity.
More than 20 working programs are associated with this book. You can download
these from the book's Web site at http://www.mcp.com/info. When you locate the URL,
you'll be asked to enter the book's ISBN: Enter 078971633x and then click the Search
button to go to the Book Info page. You'll find references and listings that
relate to the code throughout the book. Feel free to use the code in your own programming
activities.
How This Book Is Organized
The book is organized into five parts. The first three parts give you a fundamental
knowledge of the Visual Basic programming language and the Visual Basic programming
environment; the last two parts cover advanced Visual Basic topics such as object-oriented
programming, ActiveX control creation, Internet programming, data access, and databases.
Part I: Visual Basic Fundamentals
The chapters in this section introduce you to the basics of Visual Basic programming
and working within the Visual Basic programming environment.
- Chapter 1, "Building Your First Application," helps you build a desktop
calendar application from the ground up. Through hands-on learning, you develop an
understanding of what Visual Basic is and how it works.
- Chapter 2, "What's New with Visual Basic 6," introduces you to the
new features of Visual Basic 6 and the enhancements to previous versions.
- Chapter 3, "Working in the Visual Basic 6 Programming Environment,"
shows you how to work in the Visual Basic integrated development environment. The
IDE enables you to make powerful programs quickly by taking the tedium out of Windows
programming.
- Chapter 4, "Using the Intrinsic Controls," explains how to use the
small, reusable programs that make VB so special.
- Chapter 5, "Working with Projects in Visual Basic 6," teaches you how
to create different projects with different file types, describing how a Visual Basic
project is organized and how to use the different file formats.
- Chapter 6, "Working with Properties, Methods, and Events," is about
manipulating ActiveX controls by adjusting their various properties, using their
methods, and responding to their events.
Part II: Programming with Visual Basic
The chapters in this section cover subjects that are fundamental to Visual Basic
and also conceptually applicable to other programming languages.
- Chapter 7, "Using Data Types, Constants, and Variables," shows you
how to control data and manipulate different types of information, known and unknown.
- Chapter 8, "Making Statements in a Program," teaches you how to make
programming instructions in Visual Basic. Also, you learn how to do arithmetic calculations.
- In Chapter 9, "Working with Conditional Statements," you learn how
to program to make decisions by using If...Then and Select Case statements.
- Chapter 10, "Working with Loops," shows you how to make statements
in a repetitive, controlled manner.
- Chapter 11, "Working with Arrays," presents the concepts and techniques
that you need to know in order to work with groups of variables.
- Chapter 12, "Working with Strings and Typecasting," describes how to
examine, combine, and shorten strings. You also learn how to turn strings into numbers,
and vice-versa.
Part III: The Elements of Visual Basic 6
The chapters in this section develop concepts and techniques that you learned
in Part 1, while giving you a better understanding of Visual Basic as it relates
to Windows programming.
- Chapter 13, "Creating Menus in Your Programs," shows you how to design
menus, use the Menu Editor, and program menu items.
- Chapter 14, "Enhancing Your Programs with Forms and Dialog Boxes,"
tells you about the different types of forms, how to create multiple-document interface
programs, and how to use the CommonDialog ActiveX controls.
- Chapter 15, "Handling Keyboard and Mouse Input in Your Programs," shows
you how to make your programs work with a mouse. Also, you learn how to work with
data coming from a keyboard.
- Chapter 16, "Working with Time and Timers," explains how to work the
Timer ActiveX control to write programs that function in real time. Also, you learn
how to use static variables.
- Chapter 17, "Adding Graphics to Your Programs," teaches you how to
use the PictureBox and Image controls, as well as the various graphics functions
internal to VB.
- In Chapter 18, "Writing Reusable Code with Subs and Functions," you
learn to make user-defined subroutines and functions. Writing your own subs and functions
paves the way for advanced object-oriented programming.
- Chapter 19, "Saving and Retrieving Your Data with Visual Basic," shows
you how to read and write data to a file. Also, you learn how to use the FileSystemObject,
which is new to Visual Basic 6.
- Chapter 20, "Deploying Your Visual Basic Applications," covers the
concepts and techniques that you need to know in order to distribute your program
to others.
Part IV: Advanced Programming with Visual Basic 6
This section introduces you to the advanced topics in Visual Basic programming
and relates the language to ActiveX and Internet programming.
- In Chapter 21, "Debugging Your Applications," you learn how to write
error-free code and make your applications more robust.
- Chapter 22, "Creating Controls On-the-Fly Using Control Arrays," shows
you how to work with control arrays to write code once and yet have it apply to many
controls.
- Chapter 23, "Programming Beyond Visual Basic Using the Windows API,"
teaches you how to work directly with the Windows application programming interface.
You can use the Windows API to do things that standard Visual Basic won't let you
do.
- Chapter 24, "Adding Help to Your Programs," shows you how to use the
Windows Help Compiler to write interactive, context-sensitive help files.
- Chapter 25, "Using VBA to Connect Your VB Programs with Microsoft Office,"
teaches you how to use Visual Basic for Applications to interact with Microsoft Word
and Microsoft Excel.
- Chapter 26, "Making Object-Oriented Programs with Visual Basic," introduces
you to the fundamentals of object-oriented programming and describes how to create
true OOP programs in VB.
- Chapter 27, "Creating Your Own ActiveX Controls with Visual Basic,"
shows you how to make ActiveX controls in VB that you can distribute to other programmers.
- Chapter 28, "Creating VB Programs for the Internet," explains how to
create interactive Web pages with VB. This chapter covers the fundamentals of HTML,
VBScript, and Dynamic HTML.
- Chapter 29, "Making Programming Easier with Visual Basic Add-Ins,"
teaches you how to enhance the Visual Basic programming environment. Add-ins
are programs that you write in VB to use as programming helper applications. In this
chapter, you learn to create an add-in that adds code headers to your user-defined
subroutines and functions.
Part V: Database Programming with Visual Basic 6
This section shows you how to program Visual Basic to handle data access and databases.
- Chapter 30, "Database Basics and the Data Control," describes how to
construct databases and use the data control to access and manipulate data.
- Chapter 31, "Making Reports in Visual Basic 6," demonstrates how to
use Visual Basic's new data-reporting features.
- Chapter 32, "Enhancing Your Programs Using the Advanced Data Controls,"
shows you how to use the new ADO data control. This chapter also explains data designers,
data providers, data consumers, and SQL.
Conventions Used in This Book
The following items are some of the features that make this book easier for you
to explore:
- Cross-references. We've looked for all the tasks and topics related to
a topic at hand and referenced them for you. If you need to look for coverage that
leads up to what you're working on, or you want to build on the new skill you just
mastered, these references direct you to the appropriate sections in the book:
- Glossary terms. For all terms that appear in the glossary, you'll find
the first appearance of that term italicized in the text.
- Boldfacing. You can easily find the onscreen menu and dialog commands
by looking for boldfaced text, as in the following example: From the File
menu, choose Save.
- Sidenotes. Information related to the task at hand or inside information
from the authors is offset in SideNotes--to avoid interrupting the flow of the text
and to make it easy for you to spot valuable information. Each SideNote has a short
title to help you quickly identify the information you'll find there.
Visual Basic's syntax for commands, scripts, and SQL statements also incorporates
special elements. Look at the following syntax example:
For CounterVar = StartNum To EndNum [Step StepNum]
statements
Next [CounterVar]
- Terms that are italicized are placeholders. When you use one of these
terms in real life, you replace the italicized word with an appropriate value. For
example, StartNum in the preceding code would be replaced with an actual number
from which the loop starts.
- Brackets ([]) in command syntax indicate optional clauses. The brackets around
[Step StepNum] in the preceding code indicate that you aren't required to
provide the Step keyword or what size step the increment/decrement should be. When
you use the command, don't include the brackets.
- A | character indicates that you choose between one item or the other, not both.
Again, don't use this character in the actual command.
- An ellipsis (...) in listings indicates a clause that can repeat or skipped code
that's not pertinent to the discussion. Don't use the ellipsis in the actual code.
Most code listings include line numbers to make discussion about the code easier
to reference. Don't include the numbers with any real-life Visual Basic code.
© Copyright, Macmillan Computer Publishing. All
rights reserved.