A Visual Basic 6 Programmer's Toolkit

Hank Marquis and Eric A. Smith

APress Media, LLC A Visual Basic 6 Programmer's Toolkit Hank Marquis and Eric A. Smith

Additional material to this book can be downloaded from http://extras.springer.com All rights reserved. No part of this work may be reproduced or transmitred in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without prior written permission of the copyright owner and the publisher.

Library of Congress Control Number: 00-13357 4

ISBN 978-3-540-78081-6 ISBN 978-1-4302-5125-5 (eBook) DOI 10.1007/978-1-4302-5125-5

10 9 8 7 654 3 2 1

Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

Copy Editor: TSI Graphics Production: TSI Graphics Indexer: Carla Johnson Cover Design: Derek Yee Design

The information in this boak is distributed an an ·~ ls" basis, withaut warranty. Althaugh every precautian has been taken in the preparatian af this wark, neither the authar nar Apress shall have any liability ta any persan ar entity with respect ta any lass ar darnage caused ar alleged ta be caused directly ar indirectly by the infarmatian cantained in this wark. HANK MARQUIS This book is dedicated to my wife Nur and my children Adriana and Rolfe.

ERICSMITH To my wife, ]odi Brief Contents

Acknowledgments ...... xiii About the Author ...... xv

Chapter 1: Arrays and Array Manipulation ...... 1

Chapter 2: Strings and String Handling ...... 23 Chapter 3: Fun with Numbers, Numeric Processing and Logic ...... 41 Chapter 4: Working with Dates and Times ...... 59 Chapter 5: Data Structures: Versatile Veh1des for Data Storage ...... 69 Chapter 6: Windows Files, Directories and Drives ...... 89 Chapter 7: Persistence: Remernhering What You Can't Afford to Forget ...... 115 Chapter 8: Localization: The Art of Talking Like a Native ...... 127 Chapter 9: Working with Networks ...... 141 Chapter 10: Reliable and Flexible Data Validation ..... 161

Chapter 11: Working with the ...... 183 Chapter 12: Windows Systems-From the Inside .. 195 Chapter 13: Multiple Document Interface Forms ...... 201

Chapter 14: Database Programming ...... 229 Chapter 15: Active Data Objects ...... 249 Chapter 16: Objects, Objects and Objects ...... 269 Chapter 17: WebCiasses: A New Way to Program for the Web ...... 289

Index ...... 313

V Detailed Contents

Acknowledgments 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 xiii

About the Author 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 xv

1: Arrays and Array Manipulation 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

(REATING AND USING ARRAYS IN VISUAL BASIC o o o o o 0 0 0 0 0 0 0 0 0 0 0 0 0 1

Sorting Arrays 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4

WORKING WITH SORTED ARRAYS 0 0 0 0 0 0 0 0 0 0 o o o o o o 0 0 0 0 0 0 0 0 0 0 0 10

Finding an Element with a Specific Value in a Sorted Array 0 0 0 0 0 1 0

Inserting an Element into a Sorted Array 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11

Deleting an Element from a Sorted Array 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12

WORKING WITH DATA IN AN ARRAY 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13

Finding Maximum and Minimum Values in an Array Range 0 0 0 0 14

Array Arithmetic 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15

More on Array Arithmetic 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 7

Copying a Range from One Array to Another 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 8

Persisting Array Data 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

PERFORMANCE HINTS 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o 0 0 0 0 0 0 0 0 0 0 0 0 20

2: Strings and String Handling 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23

WHAT ISA STRING? 0 0 o 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o o o 0 0 0 0 0 0 0 0 0 0 0 0 23

Examining Strings 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24

ParsingStrings 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30

Changing Strings 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 34

3: Fun with Numbers, Numeric Processing, and Logic 0 0 41

BINARY ANALYSIS OF NUMBERS o o o o o o o o o o o o o o o o 0 0 0 0 0 0 0 0 0 0 0 0 41

Negative Numbers 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42

Binary Digits in Visual Basic Numbers 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 43

HEXADECIMAL HOCUS POCUS 0 0 0 0 0 0 0 0 0 0 0 0 0 o o o o o o o 0 0 0 0 0 0 0 0 0 45

MORE ON BIT FLAGS 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o o o o 0 0 0 0 0 0 0 0 4 7

Bytes from Words 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49

Words from Longs 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 50

BINARY-(ODED DECIMAL 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o o o o o o o 51

Next Steps for Warking with BCD Strings 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 52

METRICS 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 52

SPELLING ÜUT NUMBERSo 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 53

vii 4: Working with Datesand Times ...... 59 DATE ARITHMETIC ...... 60 Leap Years ...... 60 Calculating Days ...... 61 FORMATTING DATES ...... 62 JULIAN DATES ...... 64 Next Steps ...... 66 WORKING WITH TIME ...... 66 SUMMARY ...... 68

5: Oata Structures: Versatile Vehicles for Oata Storage ...... 69 DATA STRUCTURES IN VISUAL BASIC ...... 69 Arrays ...... 69 User-Defined TYPes ...... 69 Enumerated TYPes ...... 71 Classes ...... 72 Key-Item Pairs ...... 76 HASHING ...... 80 Creating the Data Structures Needed for Hashing ...... 80 Hashing the Data ...... 82 Searching the Hash Table ...... 84 Testing the Hashing Solution ...... 86

6: Windows Files, Directories, and Drives ...... 89 FILES, DIRECTORIES, AND DRIVES ...... 89 WINDOWS APPLICATION PROGRAMMING INTERFACE ...... 90 FILES ...... 91 File Existence ...... 9 1 Finding a File ...... 92 Getting a Unique Temporary Filename ...... 93 Changing a File's Date and Time Stamp ...... 95 Retrieving a Short Filename ...... 99 Recycling Instead of Deleting ...... 99 WINDOWS DIREGORIES ...... 101 Windows System Directory ...... 1 02 Windows Directory ...... 1 02 Temporary File Directory ...... 1 03 WINDOWS DISK DRIVES ...... 104 Determining the Number of Drives ...... 1 04 Detecting Drive TYPe ...... 1 06 DriveSpace ...... 107

viii Drives Larger Than 2 GB ...... 11 0 Drive Serial Number ...... 111 Reading and Writing the Drive Name ...... 113

7: Persistence: Remembering What You Can't Afford to Forget ...... 115 PERSISTENCE ...... 115 INI FILES ...... 116 Reading a String Value from an INI File ...... 116 Reading a Numeric Value from an INI File ...... 118 Writing INI File Data ...... 11 9 Deleting INI Entries ...... 120 THE ...... 120 Reading from and Writing to the Registry ...... 12 1

8: Localization: The Art of Talking Like a Native ..... 127 LOCALIZATION ...... 12 7 ACTIVEX (OMPONENTS THAT ADAPT ...... 127 From Visual Basic to Self-Localizing ActiveX Controls ...... 128 Creating Resource Files ...... 130 Creating Self-Localizing ActiveX DLLs and Documents ...... 131 Implementing Language DLLs ...... 133 Setting Default Fonts ...... 136 Handling Other Languages ...... 140

9: Working with Networks ...... 141 ACCESSING A NETWORK ...... 141 Determining Whether a Network Is Present ...... 141 Reading the Username ...... 142 Reading the Computer Name ...... 143 Setting the Computer Name ...... 143 Finding the Next Free Network Drive ...... 144 (ONNECTING TO A NETWORK RESOURCE ...... 145 Disconnecting from a Network Resource ...... 146 Network Dialogs ...... 14 7 THE API: ACCESS TO THE INTERNET WITHOUT A ßROWSER ...... 149 Installing the Run-Time Files ...... ISO Opening an Internet Channel ...... 151 Connecting to the Internet Server ...... 152 Connecting to the Data Sink ...... 152 Posting Form Data ...... 153 Reading the Response ...... 155 Closing All the Handles ...... 156 Putting the Pieces Together: A Complete Data-Source Program . 156

ix 10: Reliable and Flexible Data Validation ...... 161 VALIDATION OF NUMERIC DATA ...... 161 IsNumeric ...... 161 1)'peName ...... 162 Var1)'pe ...... 163 VALIDATION OF DATES ...... 164 IsDate ...... 165 VALIDATION OF TEXT ENTRIES ...... 166 Text Length ...... 16 6 Text Content ...... 16 7 VALIDATING ÜTHER TYPES OF DATA ...... 167 PUTTING IT ALL TaGETHER ...... 168 BuHding the Data-Entry Form ...... 168 Setting Essential Properlies ...... 169 Enforcing Character Validation ...... 1 71 Reformatting Input Data ...... 1 7 3 Credit Card Validation ...... 1 7 6 Wrapping Up ...... 18 1

11: Working with the Operating System ...... 183 RETRIEVING SYSTEM INFORMATION ...... 183 DETERMINING THE AMOUNT OF MEMORY ...... 183 DETERMINING HOW WINDOWS STARTED ...... 185 GETTING AND SETTING ENVIRONMENT VARIABLES ...... 186 Getting an ...... 18 7 Setting an Environment Variable ...... 18 7 Deleting an Environment Variable ...... 188 SHUTTING DOWN AND RESTARTING WINDOWS ...... 188 Shutting Down Windows ...... 189 Restarting Windows ...... 189 RETRIEVING HARDWARE INFORMATION ...... 189 Determining the CPU 1)'pe ...... 189 Determining the Number of CPUs ...... 1 91 Determining CPU Performance Capabilities ...... 1 91 RETRIEVING THE FLAVOR OF WINDOWS AND THE OPERATING SYSTEM VERSION ...... 192 Identifying /98 ...... 193 Identifying Windows NT ...... 193 Identifying the Operating System Version ...... 193

12: Windows Help Systems-From the Inside ...... 195 WINHELP ...... 195 DISPLAYING A HELP PAGE ...... 196 (LOSING A HELP WINDOW ...... 198 (ONTEXT-SENSITIVE HELP ...... 198

X SHOWING A SPECIFIC HELP PAGE ...... 199 DISPLAYING HELP IN APOP-UP WINDOW ...... 200 SEARCHING HELP ...... 200 MAKING SURE YOUR HELP FILE IS THE ÜNE THATS VISIBLE ...... 202 DISPLAYING THE HELP WINDOW WHERE YOU WANT ...... 202 PUTTING IT ALL TOGETHER ...... 204

13: Multiple Oocument Interface Forms ...... 207 MOl FORM ßASICS ...... 207 SHARING SYSTEM RESGURCES ...... 209 Sharing in the Real World ...... 2 12 MANAGING MENUS ...... 215 Shared Menu Items ...... 2 16 Semishared Menu Items ...... 220 Custom Menu Items ...... 224

14: Oatabase Programming ...... 229 DATABASE FEATURES IN VISUAL BASIC ...... 229 OLE DB Support ...... 229 Active Data Objects (ADO) ...... 230 The Data Environment ...... 230 The ADO Data Control ...... 230 Enhanced Data Binding ...... 230 The Query and Database Designer ...... 230 The Data Report Designer ...... 231 Data Sources ...... 231 The Data View Window ...... 231 The DataRepeater Control ...... 231 Data Form Wizard Enhancements ...... 231 The Data Object Wizard ...... 232 ADO-Aware Data Controls ...... 232 USING THE DATA ENVIRONMENT DESIGNER ...... 232 Creating the Data Environment ...... 232 Adding a Query ...... 234 Binding a Data Grid to the Data Environment ...... 23 7 Binding a Record Viewer to the Data Environment ...... 238 Binding a Data Report to the Data Environment ...... 240 USING THE 0ATALIST AND DATA(OMBO (ONTROLS ...... 242 ßUILDING A DATABASE ANALYZER ...... 244

15: Active Oata Objects ...... 249 'S UNIVERSAL DATA ACCESS STRATEGY ...... 249 More on OLE DB ...... 249 ODBC ...... 250 Remote Data Service ...... 250

xi ACTIVE DATA ÜBJECTS ...... 251 The Cannectian Object ...... 251 The Recordset and Field Objects ...... 254 The Cammand and Parameter Objects ...... 259 The Errors Callectian ...... 2 63 ADVANCED ADO TECHNIQUES ...... 264 Passing Recardsets ...... 2 65 Using Output Parameters ...... 267

16: Objeds, Objeds, and More Objeds ...... 269 (REATING SERVICE ÜBJECTS ...... 269 (REATING A NETWORK SERVICES ÜBJECT ...... 269 Retrieving the Username ...... 269 Cannecting a Netwark Drive ...... 2 70 Discannecting a Network Drive ...... 272 Putting It All Tagether ...... 2 7 2 (REATING A REGISTRY SERVICES ÜBJECT ...... 272 Understanding the Windaws Registry ...... 2 7 3 Adding the WinAPI Declaratians ta the Registry Service Object . 2 7 6 Adding the Class Properties ...... 279 Adding the Class Methads ...... 2 82 Putting It All Tagether ...... 286

17: WebCiasses: A New Way to Program for the Web . 289 INTRODUCTION TO WEBCLASSES ...... 289 ßUILDING A WEB(LASS APPLICATION ...... 290 USING AN HTML TEMPLATE ...... 294 (REATING WEBITEMS AND (USTOM EVENTS ...... 297 Using Webltems ...... 297 Using Custam Events ...... 299 (OMBINING WEB( LASSES WITH ACTIVE DATA ÜBJECTS ...... 301 (REATING DYNAMIC WEB(LASS EVENT PROCEDURES ...... 305 THE UPSIDE AND DOWNSIDE OF WEB(LASSES ...... 311

Index ...... 313

xii Acknowledgments

MANY HAVE CONTRIBUTED TO THIS BOOK. We WOUld like to thank the people Apress and all the behind-the-scenes support people for making this project possible.

xiii About the Authors

HANK MARQUIS

HANK MARQUIS is Chief Technical Offleer and faunder of Celexis, Inc. Hank is weil known to the software developrnent cornrnunity, having written nurnerous features, articles and opinions in rnany industry publications. Hank has spoken at several trade shows, including the Visual Basic Insiders Technical Surnrnit, and was the Cornponent Colurnnist for Microsoft BackOffice/CTO rnagazine. He is a past chairrnan of the Cornponent Vendor Consortiurn (CVC) Quality Cornrnittee and a faunder of the CVC. Hank is a co-author of the Visual Basic 6. 0 Bible.

ERICSMITH

ERic SMITH is an MCSD and MCSE and has written, edited contributed to ten books covering Visual Basic prograrnrning and web technologies. He is an independent consultant and trainer based in the Washington, DC area, and specializes in developing Active Server Pages, SQL Server and Visual Basic. He also rnaintains several developer web sites, including VB Techniques (http:! /www.vbtechniques.corn) and ASP Techniques (http:! /www.asptechniques.corn). These web sites provide service after the sale, as weil as original content and reviews, for his readers. In his spare , he serves as a volunteer Ernergency Medical Technician with the Fairfax County Fire & Rescue Departrnent. Contact hirn at [email protected].

Note: Piease visit http:/ /vbtechniques.corn for revised code and inforrnation.

XV