Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
Data Arrays
Now that you've mastered list boxes and combo boxes, you will have little trouble understanding data arrays. An array is nothing more than a list of variables. Regular nonarray variables, as opposed to arrays, have separate names such as the following:
curSales98 sngTaxRate intCount blnIsRecorded
Variables in an array all have the same name. Therefore, an array that holds a list of 10 division sales figures might be named curDivSales. Your program must be capable of distinguishing between an array's variables, and with the single name, this distinction might seem impossible. Nevertheless, as with list boxes, your program can distinguish between array variables by using a subscript. The subscript works just like an index value. The first value in the array would be subscripted as curDivSales(0) (subscripts start at 0 unless you use the Option Base 1 statement in a general module to start the array's subscripts at 1). The second value in the array would be curDivSales(1), and so on.
An array is a list of items with the same name and type.
Figure 10.6 illustrates how an array such as the 10-element curDivSales resides in memory.
Figure 10.6 Distinguishing array elements with subscripts.
To declare an array, you use Dim or Public just as you declare regular nonarray variables. In the declaration, specify the number of elements (in parentheses) that the array is to hold. The following Dim statement declares the 10-element Currency array named curDivSales:
Dim curDivSales(10) As Currency
Here's the great benefit that arrays give you over separate variable names: When you need to work with a group of variables, if you don't use an array, you must list each variable. Therefore, if you want to add all the divisions' sales figures and they are stored in separate nonarray variables, you would have to code something like this:
curTotal = curDivSales0 + curDivSales1 + curDivSales2 + _ curDivSales3 + curDivSales4 + curDivSales5 + _ curDivSales6 + curDivSales6 + curDivSales7 + _ curDivSales8 + curDivSales9
An array makes stepping through and totaling the data much simpler. Here is the code that uses a For...Next loop to add the items in an array:
curTotal = 0 ' Zero out the total ' Step through the elements For intCtr = 0 To 9 curTotal = curTotal + curDivSales(intCtr) ' Add elements Next intCtr ' curTotal now holds the sum of all 10 values
With only 10 variables, the array doesn't seem to offer a lot of space advantages or coding shortcuts. But what if there were 1,000 variables that you needed to track and total? By making a simple change to the For...Next loop, you can easily add together all 1,000 elements, like this:
curTotal = 0 ' Zero out the total ' Step through the elements For intCtr = 0 To 999 curTotal = curTotal + curDivSales(intCtr) ' Add elements Next intCtr ' curTotal now holds the sum of all 1,000 values
Suppose you need to ask the user for several values, such as the names of children in a class. By declaring a string array, a For...Next loop makes getting the names simple, as you can see here:
For intCtr = 1 To 10
strChildName(intCtr) = InputBox("What is the next child's name?")
Next intCtr
Control Arrays | Next Section

Account Sign In
View your cart