A Visual Basic 6 Programmer's Toolkit
Total Page:16
File Type:pdf, Size:1020Kb
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 Operating System ...... 183 Chapter 12: Windows Help 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 More 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 .............................