Using-Visual-Basic
Total Page:16
File Type:pdf, Size:1020Kb
Using Visual Basic Using Visual Basic Books Available By both authors: BP327 DOS one step at a time BP337 A Concise User's Guide to Lotus 1-2-3 for Windows BP341 MS-DOS explained BP346 Programming in Visual Basic for Windows BP352 Excel 5 explained BP362 Access one step at a time BP387 Windows one step at a time BP388 Why not personalise your PC BP400 Windows 95 explained BP406 MS Word 95 explained BP407 Excel 95 explained BP408 Access 95 one step at a time BP409 MS Office 95 one step at a time BP415 Using Netscape on the Internet* BP420 E-mail on the Internet* BP426 MS -Office 97 explained BP428 MS -Word 97 explained BP429 MS -Excel 97 explained BP430 MS -Access 97 one step at a time BP433 Your own Web site on the Internet BP448 Lotus SmartSuite 97 explained BP456Windows 98 explained* BP460 Using Microsoft Explorer 4 on the Internet* BP464 E-mail and news with Outlook Express* BP465 Lotus SmartSuite Millennium explained BP471 Microsoft Office 2000 explained BP472 Microsoft Word 2000 explained BP473 Microsoft Excel 2000 explained BP474 Microsoft Access 2000 explained BP478 Microsoft Works 2000 explained BP486 Using Linux the easy way* BP487 Quicken 2000 UK explained* BP488 Internet Explorer 5 explained* BP491 Windows 2000 explained* BP493 Windows Me explained* BP498 Using Visual Basic By Noel Kantaris: BP258 Learning to Program in C BP259 A Concise Introduction to UNIX* BP284 Programming in QuickBASIC BP325 A Concise User's Guide to Windows 3.1 ii Using Visual Basic Using Visual Basic by P.R.M. Oliver and N. Kantaris Bernard Babani (publishing) Ltd The Grampians Shepherds Bush Road London W6 7NF England www.babanibooks.corn iii Using Visual Basic Please Note Although every care has been taken with the production of this book to ensure that any projects, designs, modifications and/or programs,etc.,contained herewith, operateina correct and safe manner and also that any components specifiedarenormallyavailable in GreatBritain,the Publishers and Author(s) do not accept responsibility in any way for the failure (including fault in design) of any project, design, modification or program to work cor-ectly or to cause damage to any equipment thatit may be connected to or used in conjunction with, or in respect of any other damage or injury that may be so caused, nor do the Publishers accept responsibility in any way for the failure to obtain specified components. Notice is also given that if equipment that isstill under warranty is modified in any way or used cr connected with home -built equipment then that warranty may be void. © 2001 BERNARD BABANI (publishing) LTD First Published - February 2C01 Reprinted - June 2001 Reprinted - November 2001 Reprinted - December 2001 Reprinted - April 2002 Reprinted - August 2002 Reprinted - December 2002 British Library Cataloguing in Publication Data: A catalogue record for this book is available from the British Library ISBN 0 85934 498 3 Cover Design by Gregor Arthur Printed and Bound in Great Britain by Cox & Wyman iv Using Visual Basic v Using Visual Basic Preface Visual BASIC has become the most popular 'dialect' of BASIC in use today on IBM and compatible computers. The originalversionof BASIC (which stands forBeginner's All-purpose Symbolic Instruction Code) was first developed as a teaching language at Dartmouth College in1964. In 1978 a'standard BASIC' was adopted as aresultof recommendations on the minimum requirements of the language. BASICA, written by Microsoft for use with the IBM PCs, and GWBASIC (its equivalent form running on compatibles), was an enhanced version of standard BASIC,embodying nearly 200 commands. These were bundled with pre -DOS 5 versions of the operating system, but users of MS-DOS 5 and higher had access to a cut -down version of Microsoft's QuickBASIC, known as QBASIC. QuickBASIC was Microsoft'sfirst compiled version of BASIC, the earlier ones being interpreted languages. With an interpreted language each and every statement of code has to be interpreted by a separate program called the interpreter before the program is actually run. This happens each time a statement is encountered, even ifit appears within a loop. With a compiled language, on the other hand, a separate program, called the compiler, is used to check the whole program for errors and then compiles itinto the machine specific code that will actually be executed by the computer at run time. Statements within loops are only checked once, which makes a compiled program far more effic ent than an interpreted one. Visual BASIC is now very different from these early versions. Itis an event driven, or Object Oriented, compiled language that uses all of Windows' visual features.It also includes most of the features builtinto QuickBASIC, so earlier programs can be easily adapted to run on Visual Basic. Aswell as being a stand-aloneWindows 'programming environment', a slightly modified version of Visual Basic is alsoincludedwith MicrosoftOffice applications as VBA, or Visual Basic for Applications. vi Using Visual Basic About this Book Using Visual Basic isloosely based on our earlier book, Programming in VisJal BASIC for Windows, and is a guide to programming in a 1,Nindows environment using Microsoft's Visual Basic. For this book we used Version 6.0 of Visual Basic, on a PC running under Windows Me. The reader is not expected to have any familiarity with the language as both the environment and statements are intrcduced and explained with the help of simple programs. The user is encouraged to builc these, save them, and keep imp-oving them as more complex language statements and commands are encountered. The verysizeof VisualBasic anditsprogranming environment, can be very daunting to a new user, so this systematic approach should make leaning very much Easier. The first three Chapters give an overview of Visual Basic and the graphic based environment it uses. Forms and the more simple controls that go with them are introduced, but no attempt is made to explain how to use Microsoft Windows itself.Itis assumed that if you want to create programs that work with Windows, you will be fam liar with tle interface itself.If you do need to know more about the Windows environment, then we suggest you select an apprcpriate book from the 'Books Available' list - these are all published by BERNARD BABANI (publishing) Ltd. Chapters 4-7 cover the programming language and low it is entered into your PC, dealing with the basic Visual Basic vii Using Visual Basic statements which control program flow, input and output, and leading to the concepts of strings and arrays. In Chapter 8 we return to some of the more powerful intrinsic Visual Basic controls that allow you to produce the sort of Windows programs that you can buy. The next chapter covers functions and procedures which expand the programming capabilities of the user beyond the beginner's level.Chapter 10 dealsentirelywithdiscfilehandling techniques and should be of special interest to those who need to process large quantities of data. The two main types of data files are discussed in some detail, namely sequential and random access types. How to easily use the Windows file handling procedures is also covered. A chapter then introduces how Visual Basic can interact with Microsoft's Office applications, Word, Excel, and the database Access. The lastchapter gives an overviewofthepowerful debugging features of the program, and describes how to create, compile and package your application programs with the Visual Basic wizards. A glossary of mainly Visual Basic terms is included, which should be used with the text where necessary. For reference purposes, appendices also detail the Visual Basic naming conventions, user -defined formats, event procedures and main keyword listings and descriptions. Like the rest of our computer series, this book was written with the busy person in mind. It is not necessary to learn all thereisto know about a subject, when reading a few selectedpages canusually dothe same thingquite adequately. Using this book, it is hoped that you will be able to come to terms with Visual Basic and start producing programs of your own in the shortest possible time. Good luck and enjoy yourself, because it can be fun. If you would like to purchase a Companion Disc for any of our books listed on page ii,apart from those marked with an asterisk, containing the file/program listings which appear in them, then fill in the form at the back of the book and send it to Phil Oliver at the address given. VIII Using Visual Basic About the Authors Phil Oliver graduated in Mining Engineering at Camborne School of Minesin 1967 and since then has specialised in most aspects of surface mining technclogy, with a part cular emphasis on compute- related techniques. He has worked in Guyana, Canada, several Middle Eastern and Central Asian countries, South Africa and the United Kingdom, on such diverseprojectsas:theplanning and management of bauxite, iron,goldandcoalmines;rockexcavation contracting in the UK; international mining equipment sales and international mine consulting.In 1988 he took up a lecturing position at Camborne School of Mines (part of Exeter University) it Surface Mining and Management. He retired from full-time lecturing in 1998 to spend more time writing, consulting, anc developing Wet sites. Noel Kantaris graduated in Electrical Engineering at Bristol University and after spending three years in the Electronics Industry in London, took up a Tutorsnip in Physics at the University of Queensland. Research interests in Ionospheric Physics, led to the degrees of M.E. in Electronics and Ph.D. in Physics. On return :o the UK, he took up a Post -Doctoral Research Fellowship in Radio Physics at the University of Leicester,andthen in 1973a lecturingposition in Engineering at the Camborne School of Mines, Cornwall, (part of Exeter University), where between 1978 and 1997 he was also the CSM Computing Manager.