Tải bản đầy đủ (.pdf) (877 trang)

visual basic 6 - special edition 1998

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (10.09 MB, 877 trang )

i
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Using
Visual Basic
®
6
Using
Special Edition
Untitled-1 9/1/98, 10:18 AM1
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
ii
Special Edition Using Visual Basic 6
Untitled-1 9/1/98, 10:19 AM2
iii
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Brian Siler and Jeff Spotts
Visual Basic
®
6
Using
Special Edition
Using
Untitled-1 9/1/98, 10:19 AM3
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
iv
Special Edition Using Visual Basic 6
Special Edition Using Visual Basic
®
6


Copyright© 1998 by Que
All rights reserved. No part of this book shall be reproduced, stored
in a retrieval system, or transmitted by any means, electronic, me-
chanical, photocopying, recording, or otherwise, without written
permission from the publisher. No patent liability is assumed with
respect to the use of the information contained herein. Although
every precaution has been taken in the preparation of this book, the
publisher and author assume no responsibility for errors or omis-
sions. Neither is any liability assumed for damages resulting from
the use of the information contained herein.
International Standard Book Number: 0-7897-1542-2
Library of Congress Catalog Card Number: 98-84617
Printed in the United States of America
First Printing: September 1998
00 99 98 4 3 2 1
Trademarks
All terms mentioned in this book that are known to be trademarks
or service marks have been appropriately capitalized. Que cannot
attest to the accuracy of this information. Use of a term in this book
should not be regarded as affecting the validity of any trademark or
service mark.
Windows is a registered trademark of Microsoft Corporation.
Visual Basic is a registered trademark of Microsoft Corporation.
Warning and Disclaimer
Every effort has been made to make this book as complete and as
accurate as possible, but no warranty or fitness is implied. The
information provided is on an “as is” basis. The authors and the
publisher shall have neither liability or responsibility to any person
or entity with respect to any loss or damages arising from the infor-
mation contained in this book.

Untitled-1 9/1/98, 10:19 AM4
v
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Contents at a Glance
I Getting Started with Visual Basic
1 Starting Out with Visual Basic 11
2 Creating Your First Program 17
3 Visual Basic Building Blocks 39
4 Using Visual Basic’s Default Controls 59
II Programming with Visual Basic
5 Responding to the User with Event Procedures 95
6 Giving More Control to the User: Menus and Toolbars 107
7 Using Dialog Boxes to Get Information 139
8 Using Variables to Store Information 159
9 Visual Basic Programming Fundamentals 173
10 Controlling the Flow of Your Program Code 203
11 Managing Your Project: Sub Procedures, Functions, and Multiple Forms 223
III Visual Basic Program Components
12 Microsoft Common Controls 245
13 Working with Control Arrays 289
14 Creating ActiveX Controls 305
15 Extending ActiveX Controls 341
16 Classes: Resusable Components 359
IV Visual Basic Interfaces
17 Multiple Document Interface Applications 381
18 Proper Interface Design 409
19 Using Visual Design Elements 425
V Advanced Programming Topics
20 Accessing the Windows API 449

21 Working with Files 469
22 Using OLE to Control Other Applications 489
23 Master’s Toolbox 501
Untitled-1 9/1/98, 10:19 AM5
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
vi
Special Edition Using Visual Basic 6
VI Visual Basic and Databases
24 Database Basics 527
25 The Data Control and Data-Bound Controls 545
26 Using Data Access Objects (DAO) 565
27 Using Remote Data Objects (RDO) 595
28 Using ActiveX Data Objects (ADO) 611
29 Creating Reports 641
VII Visual Basic and the Internet
30 Using VBScript 665
31 Active Server Pages 687
32 ActiveX Documents 719
33 Visual Basic and Other Uses of the Internet 741
VIII Appendixes
A Introduction to the Development Environment 759
B Packaging Your Applications 775
C SQL Summary 793
Index 829
Untitled-1 9/1/98, 10:19 AM6
vii
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Table of Contents
Introduction 1

Fundamental Visual Basic Program-
ming 2
Working with Visual Basic Components 3
Creating Application Interfaces 4
Advanced Visual Basic Programming 4
Database Programming Techniques 5
Additional References 5
Source Code and Programs Used in This
Book 6
Conventions and Special Elements Used in
This Book 6
I Getting Started with Visual
Basic 9
1 Starting Out with Visual Basic 11
What Is a Computer Program? 12
Computer Programs and Programming
Languages 13
Visual Basic Is a Smart Language 14
The Importance of Designing Your
Program 14
How Design Fits into the Programming
Process 15
Program Design in a Nutshell 15
From Here… 16
2 Creating Your First Program 17
Creating Your Program’s User Inter-
face 18
Getting Started 18
Saving Your Work 20
Getting Information From the User 23

Adding a TextBox Control 23
Labeling Your Program’s Controls 28
Adding a Command Button 29
Changing a Form’s Properties 30
Saving Your Work—Again… 31
Coding Your Program’s Actions 31
Responding to Events 31
Specifying Event Procedures 33
Writing Program Code 34
Running Your Program 36
From Here… 38
3 Visual Basic Building Blocks 39
Forms 40
Parts of a Form 40
What Do Forms Do? 41
Using Controls 41
What Are Controls? 41
Control Functions 42
Exploring Properties 42
Property Basics 43
Common Properties 44
Using Properties to Control an Object’s
Size 44
Using Properties to Adjust an Object’s
Position 44
Changing Properties at Runtime 45
Using Properties to Control User Inter-
action 48
Referencing Forms and Controls from
Your Code 49

A First Look at Methods and Events 50
Taking Action with Methods 50
Responding to Actions with Events 51
How Properties and Methods Are Re-
lated 51
Form Properties Revisited 52
Displaying a Form 55
From Here… 57
Untitled-1 9/1/98, 10:19 AM7
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
viii
Special Edition Using Visual Basic 6
4 Using Visual Basic’s Default
Controls 59
Introduction to the Intrinsic Controls 60
Working with Text 62
Displaying Text with a Label Con-
trol 63
Entering Text with a Text Box 65
Controls for Making Choices 66
The Command Button 67
Check Boxes 68
Option Buttons 68
The List Box 70
The Combo Box 75
Special-Purpose Controls 78
Scrollbars 78
The Timer Control 81
Frames 83
Working with Multiple Controls at

Designtime 85
Selecting Multiple Controls 86
Using the Properties Window 87
Using the Form Editor Toolbar 87
Using the Format Menu 89
Multiple Controls in a Frame 90
Working with the Controls Collection 90
Changing All Controls 90
Changing Selected Controls 91
From Here… 91
II Programming with Visual
Basic 93
5 Responding to the User with Event
Procedures 95
Introducing Events 96
Handling Events in Your Programs 97
Determining When an Event Has Oc-
curred 97
Types of Events 98
Writing Event Procedures 100
Calling an Event Procedure from
Code 101
Understanding Event Sequences 102
Multiple Events for Each Action 102
Determining the Order of Events 103
From Here… 106
6 Giving More Control to the User:
Menus and Toolbars 107
Creating a Menu Bar 108
Common Menus 109

Setting Up the Main Items 110
Multiple-Level Menus 112
Grouping Menu Items 114
Modifying the Menu 115
Adding Access Keys and Shortcut Keys
for Quick Access 116
Writing Code for the Menu Items 119
Optional Settings 119
Creating Pop-Up Menus 122
Creating the Menu to Be Dis-
played 123
Activating a Pop-Up Menu 124
Using Toolbars in Visual Basic 124
Toolbar Basics 125
Getting the Images for Your
Toolbar 126
Creating a Standard Toolbar 127
Creating the Toolbar’s Buttons 129
Starting a Toolbar Example 131
Enabling the Buttons with Code 132
Creating a Toolbar with Code 133
Allowing the User to Customize the
Toolbar 135
Using the CoolBar Control 136
From Here… 137
7 Using Dialog Boxes to Get
Information 139
Keeping the User Informed 140
Understanding the Message Box 140
Displaying a Message 141

Untitled-1 9/1/98, 10:19 AM8
ix
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Returning a Value from the MsgBox
Function 143
Demonstrating the MsgBox Func-
tion 146
Getting Information from the User 146
Setting Up the InputBox Function 146
Values Returned by InputBox 147
Using Built-In Dialog Boxes 148
General Usage of the CommonDialog
Control 148
Testing the CommonDialog Con-
trol 149
The File Dialog Boxes 150
The Font Dialog Box 152
The Color Dialog Box 154
The Print Dialog Box 155
The Help Dialog Box 157
Creating Your Own Dialog Boxes 157
Creating a Custom Dialog Box 157
Using Form Templates for Other Dialog
Boxes 157
From Here… 158
8 Using Variables and Constants to
Store Information 159
Introduction to Variables 160
Naming Variables 160

Types of Variables 161
Variable Declarations 163
Explicit Declaration 163
Implicit Declaration 164
Fixed-Length Strings 165
Variable Arrays 166
Determining Where a Variable Can Be
Used 166
Creating Variables That Are Available
Everywhere 167
Keeping a Variable Local 168
Using Static Variables 168
Using the Option Explicit Statement 169
What’s Different About Constants 171
How to Use Constants 171
Constants That Visual Basic Sup-
plies 171
Creating Your Own Constants 172
From Here… 172
9 Visual Basic Programming
Fundamentals 173
Writing Statements 174
Using Assignment Statements 175
Using Math Operations 176
Addition and Subtraction 177
Multiplication and Division 178
Exponentiation 181
Operator Precedence 181
Working with Strings 182
String Concatenation 183

Determining the Length of the
String 185
Changing the Case of a String 185
Searching a String 187
Extracting Pieces of a String 189
Getting Rid of Spaces 191
Replacing Characters in a String 191
Working with Specific Characters 193
Strings and Numbers 194
Formatting Results 195
Specific Formatting Functions 195
Using the Format Function 197
Manipulating Date Values 200
From Here… 202
10 Controlling the Flow of Your Program
Code 203
Making Decisions in Your Program 204
Using the If Statement 204
Working with the False Condition 205
Working with Multiple If State-
ments 206
Using Select Case 207
Untitled-1 9/1/98, 10:19 AM9
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
x
Special Edition Using Visual Basic 6
Working with Loops 209
For Loops 210
Do Loops 211
Enumeration Loops 214

Debugging Your Programs 214
Stepping Through Your Code 216
Working in the Immediate Win-
dow 217
Tracking Variable Values 218
Error Trapping 219
Using the On Error Statement 219
Labeling Code Lines 219
Controlling Program Flow After
an Error 220
Determining the Type of Error 221
From Here… 222
11 Managing Your Project: Sub
Procedures, Functions, and Multiple
Forms 223
Using Procedures and Functions 224
Working with Procedures 224
Working with Functions 231
Determining the Scope of Procedures
and Functions 232
Reusing Functions and Proce-
dures 233
Working with Multiple Forms 235
Adding New Forms to Your Pro-
gram 235
Adding Code Modules to a Project 236
Accessing the Forms and Modules of a
Project 236
Managing Components in Your
Project 237

Managing Program References 238
Controlling Your Controls 238
Adding Forms, Modules, and Classes
to the Project 238
Removing Pieces 241
Controlling How Your Program Starts 241
Setting the Startup Form 241
Using Sub Main 241
From Here… 242
III Visual Basic Program
Components 243
12 Microsoft Common Controls 245
Introduction to the Common Controls 246
The ImageList: A Fundamental Common
Control 248
Setting Up an ImageList at
Designtime 248
Setting Up an ImageList with
Code 249
Organizing Your Data 250
Using the ListView Control 251
Using the TreeView Control 258
Using the TabStrip Control 261
Accepting User Input 266
Using an ImageCombo Control 266
The UpDown Control 269
Working with Dates 271
Sliding into Numbers 275
Reporting Status and Progress 278
Adding a Status Bar to Your Pro-

gram 279
Progress Bar 284
Adding Video with the Animation Con-
trol 285
From Here… 287
13 Working with Control Arrays 289
Introducing Control Arrays 290
Control Array Elements 290
Understanding the Advantages of Con-
trol Arrays 290
Creating a Control Array 291
Adding Control Arrays to a Form 291
Writing Code for a Control Array 294
Removing Elements from a Control Ar-
ray 296
Working with Control Arrays 296
Using a Control Array in Your Pro-
grams 296
Untitled-1 9/1/98, 10:19 AM10
xi
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Parallel Arrays 297
Creating a Menu Item Array 298
Loading and Unloading Controls at
Runtime 299
Creating the First Element of a Control
Array 299
Adding Controls at Runtime 300
Removing Controls at Runtime 301

From Here… 303
14 Creating ActiveX Controls 305
ActiveX Basics 306
Steps Involved in Building ActiveX Con-
trols 306
Development Strategies 307
Creating an ActiveX Control 307
Starting the Address Control
Project 307
Adding Resize Code to the Control 309
Adding a New Property to Your Con-
trol 310
Testing the ActiveX Control 311
Testing with a Project Group 311
Testing with Internet Explorer 313
Compiling Your Control 314
Creating the OCX File 315
Testing the Compiled Control 315
Distributing the Control to Another
Machine 315
Enhancing an ActiveX Control 317
Setting Up the Base Control 318
Enhancing the Base Control 319
Testing the Limited Character Text
Box 322
Choosing a Toolbox Icon 323
Using the ActiveX Control Interface
Wizard 323
Adding the Wizard to Visual Basic 323
Selecting and Creating Properties 324

Mapping Properties 326
Finishing the Code 327
Using the Property Pages Wizard 330
Creating the Pages 330
Adding Properties to the Pages 331
Using the Property Pages in your Appli-
cations 332
Creating a User-Drawn ActiveX Con-
trol 332
Starting the Project 333
Creating the User Interface 333
Creating the Properties of the
Button 335
Setting Up the Button’s Events 337
Creating Property Pages for the
Button 337
Testing the Color Button in a
Program 338
From Here… 339
15 Extending ActiveX Controls 341
Using the Ambient Object to Maintain Uni-
formity 342
Setting Up an Ambient Object
Example 342
Keeping Track of the Ambient
Colors 343
Properties of the Ambient Object 343
Introducing the Extender Object 344
Building the Calculator Control 345
Creating the Control 345

Creating the Interface 346
Setting Up the Operation Property 347
Coding Methods and Events 348
Testing the Control 349
Creating Property Pages 350
Creating Property Page Objects 350
Placing Controls on the Property
Pages 351
Implementing the SelectionChanged
Event Procedure 352
Implementing the Change Event Proce-
dures 352
Implementing the ApplyChanges Event
Procedure 353
Untitled-1 9/1/98, 10:19 AM11
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
xii
Special Edition Using Visual Basic 6
Connecting the Property Page to the
Control 353
Using Your Property Page 354
Handling Multiple Control Selec-
tions 355
Control Error Handling 355
From Here… 357
16 Classes: Reusable
Components 359
Understanding Classes 360
Object-Oriented Programming 360
Classes in Visual Basic 361

Building Class Modules 361
Starting a New Class Module 362
Adding Properties to the Class 362
Adding Methods to the Class 365
Declaring and Using Objects 366
Adding Your Own Events 367
Creating an ActiveX DLL 368
Creating an ActiveX Project 368
Working with Multiple Projects 368
Setting the Instancing Property 371
Enums 372
Creating Classes That Contain Collec-
tions 373
Standard Collection Properties and
Methods 373
Creating a New Collection for Grouped
Actions 374
Using the Class Builder 376
From Here… 377
IV Visual Basic Interfaces 379
17 Multiple Document Interface
Applications 381
Introducing MDI Applications 382
Characteristics of MDI Parent
Forms 383
Characteristics of MDI Child
Forms 384
Creating a Simple MDI Program 384
Setting Up a Parent Form 384
Setting Up a Child Form 386

Running the Program 387
Creating Multiple Instances of a
Form 388
Setting Up the Basic Form 388
Creating Forms Using Object Vari-
ables 390
Using the Keywords Me and
ActiveForm 390
Initializing an Instance of a Child
Form 391
Working with Menus 391
Managing the Children 392
Using Automatic Organization 392
Maintaining a Window List 393
Creating a Sample Application—an MDI
Contact Manager 395
Creating the MDI Form 396
Setting Up the Customer Child
Form 397
Creating the Search Form 398
Creating the Heart of the Program 399
Running the Program 400
Optimizing Your MDI Application 400
Creating an MDI Application Frame-
work 401
Creating the MDI Parent Tem-
plate 402
The MDI Child 405
From Here… 407
18 Proper Interface Design 409

Designing Effective Forms 410
Keep Forms Neat and Uncluttered 410
Pay Special Attention to Data Entry
Forms 411
Use the Right Control for the Job 412
Third-Party Controls 413
Multiple Forms 413
User PC Differences 415
Untitled-1 9/1/98, 10:19 AM12
xiii
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Dealing with User Expectations 417
The List Box 417
Effective Menus 420
Handling Multiple Instances of Your
Application 420
Perceived Speed 422
From Here… 423
19 Using Visual Design Elements 425
Using Graphics 426
Graphics Controls 426
Graphics Methods 432
Working with Text and Fonts 439
Text Box Behavior 439
Working with Fonts and Colors 441
From Here… 446
V Advanced Programming
Topics 525
20 Accessing the Windows API 449

Understanding the Windows API 450
Using the Windows API in Visual
Basic 452
Using the API Viewer 452
Creating a Wrapper Function 454
Creating a Wrapper Class 455
Useful API Calls 457
Fun API Calls 457
Finding and Controlling Other
Windows 458
Waiting on a Program to Finish
Running 461
Callbacks and Subclassing 463
From Here… 467
21 Working with Files 469
File Functions in Visual Basic 470
Using Dir to Find and List Files 470
File-Manipulation Functions 473
Launching Other Programs with the
Shell Function 475
Locating Files Relative to Your Applica-
tion 475
Working with Text Files 477
Sequential Text Files 477
Reading from a Sequential Text
File 478
Writing to a Sequential Text File 480
Random Files—Creating Your Own File
Format 482
Creating a Record Type 482

Opening a Random Access File 483
Adding Records with Put 483
Retrieving Records with Get 483
Random Access with Seek 484
INI Files 484
Understanding INI Files 484
Using INI Files in Visual Basic 485
From Here… 487
22 Using OLE to Control Other
Applications 489
Working with Word Objects 490
The Microsoft Word Object
Library 491
Creating Application and Document Ob-
jects 492
Saving, Opening, and Printing Docu-
ments 494
Working with Text 494
Other Useful Features 495
Word.Basic 496
Working with Excel 496
Creating Excel Objects 497
Setting Cell and Range Values 497
Using the OLE Container Control 497
Creating an Embedded Object at
Designtime 498
Creating an Embedded Object at
Runtime 499
Creating a Linked Object 499
From Here… 500

Untitled-1 9/1/98, 10:19 AM13
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
xiv
Special Edition Using Visual Basic 6
23 Master’s Toolbox 501
Caller ID with Visual Basic 502
Requirements for Using the Sample
Program 502
VB Techniques You’ll Be Using 503
Setting Up the Communications
Control 505
Checking for Calls 507
Building a Screen Saver in Visual
Basic 509
Setting Up the Main Form 510
Adding Animation 511
Interacting with Windows 512
SQL Server to Access Database Table
Export Program 512
Building the Sample Program 513
Understanding the Sample Pro-
gram 513
Using the Windows API to Create Trans-
parent Images 519
From Here… 524
VI Visual Basic and
Databases 525
24 Database Basics 527
Designing a Database 528
Design Objectives 528

Key Activities in Designing Your
Database 529
Organizing the Data 529
Using Indexes 534
Using Queries 536
Implementing Your Design 537
Using Visual Data Manager 537
Creating the Database File 538
Adding a New Table 539
Making Changes to the Fields in Your
Table 540
Adding an Index to the Table 541
Returning to the Visual Basic Design
Window 542
Viewing or Modifying the Structure of a
Table 542
Renaming or Deleting a Table 542
Copying a Table 542
Creating a Database with Other Tools 543
Using Microsoft Access 543
Third-Party Database Designers 543
Why Use a Program Instead of Visual Data
Manager? 543
From Here… 544
25 The Data Control and Data-Bound
Controls 545
Understanding the Data Control 546
What Is the Data Control? 546
Adding a Data Control to Your
Form 547

The Two Required Properties 548
Getting Acquainted with Bound Con-
trols 550
What Do These Controls Do? 550
Adding Controls to Your Forms 551
Using a Bound Control to Display
Data 552
Creating a Simple Application 553
Setting Up the Form 553
Navigating the Database 554
Using Code with the Data Control 555
Adding and Deleting Records 555
Creating Forms Automatically 557
Setting Up the Data Form Wizard 557
Getting to the Source of Your Data 559
Choosing the Binding Type 560
Choosing Fields with the Data Form
Wizard 560
Control Selection 561
From Here… 562
Untitled-1 9/1/98, 10:19 AM14
xv
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
26 Using Data Access Objects
(DAO) 565
Introduction to DAO 566
Setting Up a DAO Project 567
Opening an Existing Database 568
Deciding Which Recordset Type to

Use 569
Using Tables 570
Using Dynasets 571
Using Snapshots 573
Using a Forward-Only Recordset 574
Placing Information Onscreen 575
Accessing Information from Database
Fields 575
Displaying Data in the Sample Pro-
gram 575
Positioning the Record Pointer 576
Using the Move Methods 577
Using the Bookmark Property 578
Using the Find Methods 579
Setting the Current Index in a
Table 581
Using the Seek Method 581
Using the PercentPosition and
AbsolutePosition Properties 584
Using Filters, Indexes, and Sorts 585
Setting the Filter Property 585
Setting the Sort Property 586
Creating a New Index 586
Considering Programs That Modify Mul-
tiple Records 587
Using Loops 587
Using SQL Statements 589
Understanding Other Programming Com-
mands 590
Adding Records 590

Editing Records 591
Updating Records 591
Deleting Records 592
Introducing Transaction Processing 592
From Here… 594
27 Using Remote Data Objects
(RDO) 595
Database Access Philosophies 596
Working with ODBC 596
Understanding ODBC Drivers 597
Setting Up an ODBC Data Source 597
The Remote Data Objects 603
Comparison of RDO to DAO 603
Accessing a Database with RDO 605
Using the RemoteData Control 607
Comparing the RDC and the Data Con-
trol 607
Setting Up the RDC 608
From Here… 609
28 Using ActiveX Data Objects
(ADO) 611
Introducing ADO 612
Data Connection Methods 612
Installation 612
Setting Up a Data Source 613
Using the ADO Data Control 614
Setting Up the ADO Data Control 615
Connecting the ADO Data Control to a
Data Source 616
Displaying Data 618

Changing the Record Source from
Code 619
Using the DataGrid Control 619
Getting Data into the Grid 620
Setting Up the DataGrid 621
Splitting Up the Grid 623
Customizing the Grid’s Layout 624
Customizing the Grid with Code 626
Using ActiveX Data Objects 627
Making the Connection with ADO 627
Working with Recordsets 630
The Command Object 636
Disconnected Recordsets 637
Creating a Disconnected
Recordset 637
Untitled-1 9/1/98, 10:19 AM15
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
xvi
Special Edition Using Visual Basic 6
Reconnecting a Recordset 638
Uses of a Disconnected Recordset 639
From Here… 640
29 Creating Reports 641
Creating a Simple Report 642
Setting Up the Data Source 642
Adding a Data Report to Your
Project 643
Setting Up the Data Report 644
Displaying the Report 647
Enhancing Your Data Reports 648

Predefined Report Fields 648
Adding Graphics 649
Printing and Exporting 651
Function Fields 652
Using Crystal Reports 653
Creating a New Report 654
Customizing Your Report 657
Using the Crystal Reports Control 658
From Here… 661
VII Visual Basic and the
Internet 663
30 Using VBScript 665
Introduction to VBScript 666
Enhancing the Internet with
VBScript 666
VBScript on the Web Server 667
VBScript in the Browser 668
Tools Used with VBScript 671
The VB Scripting Engine 671
Host Application 671
Text Editor 672
Advanced Web Tools 672
The VBScript Language 673
Working with Variants Only 673
Using Objects for Added Power 674
Accessing the File System 675
Using VBScript in Internet Explorer 676
Events and Procedures 677
Forms 679
Using ActiveX Controls 681

The Windows Scripting Host 682
Running Scripts 683
Useful Objects and Methods 684
From Here… 685
31 Active Server Pages 687
Introduction to Active Server Pages 688
Active Server Pages Versus Standard
HTML 688
Virtual Directories 690
Creating ASP Files 693
Creating a Simple ASP File 693
Using Server-Side Scripting Tags 693
Simple but Dynamic Web Pages 694
Using Include Files 696
Database Access with Active Server
Pages 697
Querying a Database 698
Updating Information in a Data-
base 701
The ASP Objects 704
Managing Security with the Session
Object 705
Controlling Output with the Response
Object 707
Retrieving Data with the Request
Object 709
The Server Object 710
The Application Object and
GLOBAL.ASA 710
Using Your Own ActiveX DLL with

ASP 711
The IIS Application Project 711
Creating an IIS Application 712
Running an IIS Application 713
WebClass Instancing 714
Using an HTML Template
WebItem 715
Using a Custom WebItem 717
From Here… 718
Untitled-1 9/1/98, 10:19 AM16
xvii
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
32 ActiveX Documents 719
Understanding ActiveX Documents 720
What Is an ActiveX Document? 721
What Are the Advantages of Using
ActiveX Documents? 722
Creating Your First ActiveX Docu-
ment 722
Starting an ActiveX Document
Project 723
Creating the Interface of the Docu-
ment 724
Adding Code to the Document 726
Testing Your ActiveX Document 726
Compiling Your Document 729
Exploring the UserDocument Object 729
Understanding the Key Events of a
UserDocument 730

Creating and Storing Properties for a
UserDocument 730
Working with the Methods of the
UserDocument 731
Using the Hyperlink Object in Your Docu-
ment 733
Using the ActiveX Document Migration
Wizard 733
Running the ActiveX Document Migra-
tion Wizard 734
Looking at the Results of the Wizard’s
Work 736
Creating a More Complex Document 737
Programming Additional Docu-
ments 737
Using and Displaying Forms from the
Document 738
From Here… 738
33 Visual Basic and Other Uses of the
Internet 741
Adding Browser Functionality to Your
Application 742
Creating a Browser on a Form 742
Launching the Browser from Your
Application 744
Programming E-Mail 747
Logging on to E-Mail 748
Sending a Message 749
Accessing the Contents of a
Message 750

Using the Internet Transfer Control 751
Retrieving HTML 751
Transferring Files 753
From Here… 755
VIII Appendixes 759
A Introduction to the Development
Environment 759
Understanding the Environment’s Key
Features 760
Starting Up 761
The Visual Basic Work Area 762
Using the Menu Bar 763
Accessing Functions with the
Toolbars 764
Organizing Visual Basic’s Controls 766
The Canvas of Your Programs 769
Controlling Your Forms and Con-
trols 770
Using the Project Window 771
Where Work Gets Done 772
Customizing Your Environment 773
B Packaging Your Applications 775
Compiling Your Program 776
Optimizing Your Code 776
Setting the Project Name, Title, and
Icon 777
Preparing to Create a Setup Pro-
gram 778
Packaging a Standard EXE Project 778
Creating a Standard EXE Package 778

A Closer Look at the Setup Pro-
cess 785
Packaging ActiveX Components 788
Internet Download 788
Untitled-1 9/1/98, 10:19 AM17
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
xviii
Special Edition Using Visual Basic 6
Scripting Options 789
Files Created for the Internet 790
From Here… 791
C SQL Summary 793
Defining SQL 794
What SQL Does 794
The Parts of the SQL Statement 795
Using SELECT Statements 796
Defining the Desired Fields 796
Specifying the Data Sources 801
Using ALL, DISTINCT, or
DISTINCTROW Predicates 802
Setting Table Relationships 803
Setting the Filter Criteria 806
Setting the Sort Conditions 810
Using Aggregate Functions 811
Creating Record Groups 813
Creating a Table 815
Using Parameters 815
SQL Action Statements 816
Using the DELETE Statement 816
Using the INSERT Statement 817

Using the UPDATE Statement 818
Using Data-Definition-Language State-
ments 818
Defining Tables with DDL State-
ments 818
Defining Indexes with DDL State-
ments 819
Using SQL 820
Executing an Action Query 820
Creating a QueryDef 821
Creating Dynasets and Snapshots 821
Using SQL Statements with the Data
Control 822
Creating SQL Statements 823
Using the Visual Data Manager 823
Using Microsoft Access 825
Optimizing SQL Performance 825
Using Indexes 826
Compiling Queries 826
Keeping Queries Simple 826
Passing SQL Statements to Other Database
Engines 827
From Here… 827
Index 829
Untitled-1 9/1/98, 10:19 AM18
xix
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Credits
EXECUTIVE EDITOR

Brad Jones
ACQUISITIONS EDITOR
Kelly Marshall
DEVELOPMENT EDITOR
Christopher Nelson
MANAGING EDITOR
Jodi Jensen
PROJECT EDITOR
Maureen Schneeberger McDaniel
COPY EDITOR
Chuck Hutchison
Kelli Brooks
INDEXER
Bruce Clingaman
TECHNICAL EDITORS
Mark Hurst
Simon Mordzynski
PRODUCTION
Marcia Deboy
Jennifer Earhart
Cynthia Fields
Susan Geiselman
Untitled-1 9/1/98, 10:19 AM19
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
xx
Special Edition Using Visual Basic 6
To Julia, Larry, Ben, and Brent for all of your loyal support.
- Brian Siler
To my lovely wife Tina and our beautiful daughter Lauren, for making me
smile every single day. You make me better at everything I do.

- Jeff Spotts
Untitled-1 9/1/98, 10:19 AM20
xxi
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
About the Authors
Brian Siler works as a senior programmer analyst for a major hotel corporation, developing its
executive information system in Visual Basic. Brian is a graduate of the University of Memphis
with a B.S. in Computer Science. He has developed applications using Visual Basic, C, HTML,
and SQL on a variety of platforms including PCs, AS/400, UNIX, and Vax. He was the co-author
of Que’s Special Edition Using Visual Basic 5, Second Edition, and Que’s Platinum Edition
Using Visual Basic 5. Brian may be contacted via e-mail at

Jeff Spotts is a Senior Business Systems Analyst for Federal Express Corporation. By night,
he teaches Visual Basic programming courses at State Technical Institute at Memphis. He also
develops custom-designed software systems for individuals and businesses. His specialty is
creating database applications using Visual Basic as a front-end interface to a variety of data-
base engines. He has been involved with computer hardware and software since the late 1970s,
and has been programming with Visual Basic since just after its introduction. Jeff was a co-
author of Special Edition Using Visual Basic 5, Second Edition. He may be contacted via e-mail
at

Untitled-1 9/1/98, 10:19 AM21
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
xxii
Special Edition Using Visual Basic 6
Acknowledgments
The authors wish to acknowledge the steadying influence of our Acquisitions Editor, Kelly
Marshall, and our Product Director, Chris Nelson, for helping to keep this project afloat and on
track in occasionally stormy seas. We would also like to acknowledge our co-workers (both

full-timers and the contractors who make the real money) for their continued advice and sup-
port.
Untitled-1 9/1/98, 10:19 AM22
xxiii
Contents
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
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 an Executive Editor for the Programming team at Macmillan Computer Publishing, I wel-
come 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:

Mail: Executive Editor
Programming
Macmillan Computer Publishing
201 West 103rd Street
Indianapolis, IN 46290 USA
Untitled-1 9/1/98, 10:19 AM23
P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
xxiv
Special Edition Using Visual Basic 6

Untitled-1 9/1/98, 10:19 AM24
1
P2/V4/SWG2 SEU VB6 1542-2 7.23.98 Marcia INTRO LP#1
Introduction
In this chapter
Fundamental Visual Basic Programming 2
Working with Visual Basic Components 3
Creating Application Interfaces 4
Advanced Visual Basic Programming 4
Database Programming Techniques 5
Internet Programming 5
Additional References 5
Source Code and Programs Used in This Book 6
Conventions and Special Elements Used in This Book 6
INTRODUCTION
Untitled-2 9/1/98, 10:20 AM1

×