U ContentsSi p e s c i a i l nE d i t g Using i o n Visual Basic® 6 Untitled-1 1 9/1/98, 10:18 AM P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1 ii Special Edition Using Visual Basic 6 Untitled-1 2 9/1/98, 10:19 AM P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1 U ContentsSiii p e s c i a i l nE d i t g i Using o n Visual Basic® 6 Brian Siler and Jeff Spotts Untitled-1 3 9/1/98, 10:19 AM 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 4 9/1/98, 10:19 AM P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1 Contents v 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 5 9/1/98, 10:19 AM 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 6 9/1/98, 10:19 AM P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1 Contents vii Table of Contents Introduction 1 Getting Information From the User 23 Adding a TextBox Control 23 Fundamental Visual Basic Program- Labeling Your Program’s Controls 28 ming 2 Adding a Command Button 29 Working with Visual Basic Components 3 Changing a Form’s Properties 30 Creating Application Interfaces 4 Saving Your Work—Again… 31 Advanced Visual Basic Programming 4 Coding Your Program’s Actions 31 Responding to Events 31 Database Programming Techniques 5 Specifying Event Procedures 33 Additional References 5 Writing Program Code 34 Source Code and Programs Used in This Running Your Program 36 Book 6 From Here… 38 Conventions and Special Elements Used in This Book 6 3 Visual Basic Building Blocks 39 Forms 40 I Getting Started with Visual Parts of a Form 40 What Do Forms Do? 41 Basic 9 Using Controls 41 What Are Controls? 41 1 Starting Out with Visual Basic 11 Control Functions 42 What Is a Computer Program? 12 Exploring Properties 42 Property Basics 43 Computer Programs and Programming Common Properties 44 Languages 13 Using Properties to Control an Object’s Visual Basic Is a Smart Language 14 Size 44 Using Properties to Adjust an Object’s The Importance of Designing Your Position 44 Program 14 Changing Properties at Runtime 45 How Design Fits into the Programming Using Properties to Control User Inter- Process 15 action 48 Program Design in a Nutshell 15 Referencing Forms and Controls from From Here… 16 Your Code 49 A First Look at Methods and Events 50 2 Creating Your First Program 17 Taking Action with Methods 50 Responding to Actions with Events 51 Creating Your Program’s User Inter- How Properties and Methods Are Re- face 18 lated 51 Getting Started 18 Saving Your Work 20 Form Properties Revisited 52 Displaying a Form 55 From Here… 57 Untitled-1 7 9/1/98, 10:19 AM 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 Calling an Event Procedure from Controls 59 Code 101 Understanding Event Sequences 102 Introduction to the Intrinsic Controls 60 Multiple Events for Each Action 102 Working with Text 62 Determining the Order of Events 103 Displaying Text with a Label Con- From Here… 106 trol 63 Entering Text with a Text Box 65 6 Giving More Control to the User: Controls for Making Choices 66 Menus and Toolbars 107 The Command Button 67 Check Boxes 68 Creating a Menu Bar 108 Option Buttons 68 Common Menus 109 The List Box 70 Setting Up the Main Items 110 The Combo Box 75 Multiple-Level Menus 112 Grouping Menu Items 114 Special-Purpose Controls 78 Modifying the Menu 115 Scrollbars 78 Adding Access Keys and Shortcut Keys The Timer Control 81 for Quick Access 116 Frames 83 Writing Code for the Menu Items 119 Working with Multiple Controls at Optional Settings 119 Designtime 85 Creating Pop-Up Menus 122 Selecting Multiple Controls 86 Creating the Menu to Be Dis- Using the Properties Window 87 played 123 Using the Form Editor Toolbar 87 Activating a Pop-Up Menu 124 Using the Format Menu 89 Multiple Controls in a Frame 90 Using Toolbars in Visual Basic 124 Toolbar Basics 125 Working with the Controls Collection 90 Getting the Images for Your Changing All Controls 90 Toolbar 126 Changing Selected Controls 91 Creating a Standard Toolbar 127 From Here… 91 Creating the Toolbar’s Buttons 129 Starting a Toolbar Example 131 Enabling the Buttons with Code 132 II Programming with Visual Creating a Toolbar with Code 133 Basic 93 Allowing the User to Customize the Toolbar 135 5 Responding to the User with Event Using the CoolBar Control 136 Procedures 95 From Here… 137 Introducing Events 96 7 Using Dialog Boxes to Get Handling Events in Your Programs 97 Information 139 Determining When an Event Has Oc- curred 97 Keeping the User Informed 140 Types of Events 98 Understanding the Message Box 140 Writing Event Procedures 100 Displaying a Message 141 Untitled-1 8 9/1/98, 10:19 AM P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1 Contents ix Returning a Value from the MsgBox What’s Different About Constants 171 Function 143 How to Use Constants 171 Demonstrating the MsgBox Func- Constants That Visual Basic Sup- tion 146 plies 171 Creating Your Own Constants 172 Getting Information from the User 146 Setting Up the InputBox Function 146 From Here… 172 Values Returned by InputBox 147 9 Visual Basic Programming Using Built-In Dialog Boxes 148 Fundamentals 173 General Usage of the CommonDialog Control 148 Writing Statements 174 Testing the CommonDialog Con- trol 149 Using Assignment Statements 175 The File Dialog Boxes 150 Using Math Operations 176 The Font Dialog Box 152 Addition and Subtraction 177 The Color Dialog Box 154 Multiplication and Division 178 The Print Dialog Box 155 Exponentiation 181 The Help Dialog Box 157 Operator Precedence 181 Creating Your Own Dialog Boxes 157 Working with Strings 182 Creating a Custom Dialog Box 157 String Concatenation 183 Using Form Templates for Other Dialog Determining the Length of the Boxes 157 String 185 From Here… 158 Changing the Case of a String 185 Searching a String 187 8 Using Variables and Constants to Extracting Pieces of a String 189 Store Information 159 Getting Rid of Spaces 191 Replacing Characters in a String 191 Introduction to Variables 160 Working with Specific Characters 193 Naming Variables 160 Strings and Numbers 194 Types of Variables 161 Formatting Results 195 Variable Declarations 163 Specific Formatting Functions 195 Explicit Declaration 163 Using the Format Function 197 Implicit Declaration 164 Manipulating Date Values 200 Fixed-Length Strings 165 From Here… 202 Variable Arrays 166 10 Controlling the Flow of Your Program Determining Where a Variable Can Be Code 203 Used 166 Creating Variables That Are Available Making Decisions in Your Program 204 Everywhere 167 Using the If Statement 204 Keeping a Variable Local 168 Working with the False Condition 205 Using Static Variables 168 Working with Multiple If State- Using the Option Explicit Statement 169 ments 206 Using Select Case 207 Untitled-1 9 9/1/98, 10:19 AM P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1 x Special Edition Using Visual Basic 6 Working with Loops 209 III Visual Basic Program For Loops 210 Components 243 Do Loops 211 Enumeration Loops 214 12 Microsoft Common Controls 245 Debugging Your Programs 214 Stepping Through Your Code 216 Introduction to the Common Controls 246 Working in the Immediate Win- dow 217 The ImageList: A Fundamental Common Tracking Variable Values 218 Control 248 Setting Up an ImageList at Error Trapping 219 Designtime 248 Using the On Error Statement 219 Setting Up an ImageList with Labeling Code Lines 219 Code 249 Controlling Program Flow After an Error 220 Organizing Your Data 250 Determining the Type of Error 221 Using the ListView Control 251 Using the TreeView Control 258 From Here… 222 Using the TabStrip Control 261 11 Managing Your Project: Sub Accepting User Input 266 Using an ImageCombo Control 266 Procedures, Functions, and Multiple The UpDown Control 269 Forms 223 Working with Dates 271 Sliding into Numbers 275 Using Procedures and Functions 224 Working with Procedures 224 Reporting Status and Progress 278 Working with Functions 231 Adding a Status Bar to Your Pro- Determining the Scope of Procedures gram 279 and Functions 232 Progress Bar 284 Reusing Functions and Proce- Adding Video with the Animation Con- dures 233 trol 285 Working with Multiple Forms 235 From Here… 287 Adding New Forms to Your Pro- gram 235 13 Working with Control Arrays 289 Adding Code Modules to a Project 236 Accessing the Forms and Modules of a Introducing Control Arrays 290 Project 236 Control Array Elements 290 Understanding the Advantages of Con- Managing Components in Your trol Arrays 290 Project 237 Managing Program References 238 Creating a Control Array 291 Controlling Your Controls 238 Adding Control Arrays to a Form 291 Adding Forms, Modules, and Classes Writing Code for a Control Array 294 to the Project 238 Removing Elements from a Control Ar- Removing Pieces 241 ray 296 Controlling How Your Program Starts 241 Working with Control Arrays 296 Setting the Startup Form 241 Using a Control Array in Your Pro- Using Sub Main 241 grams 296 From Here… 242 Untitled-1 10 9/1/98, 10:19 AM P2/V4/SWG2 SEU VB6 7.23.98 Marcia FM LP#1
Description: