We launched a new IT training category! Check out the 140+ courses now.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Defining an array

From: Up and Running with VBA in Access

Video: Defining an array

Most of the variables you deal with when you write Access VBA code will be single variables, such as item prices, shipping rates, and so on; however, if you must deal with a set of values of the same type, such as a set of shipping rates, you can do so by creating an array. In this movie, we'll use the code in the Define Arrays code module, so I'll double-click that over in the Navigation pane to open it. And I have a very simple subroutine. Basically, I have the DefineArrays subroutine named, and then I create a variable called ChargesCount, and that is an integer.

Defining an array

Most of the variables you deal with when you write Access VBA code will be single variables, such as item prices, shipping rates, and so on; however, if you must deal with a set of values of the same type, such as a set of shipping rates, you can do so by creating an array. In this movie, we'll use the code in the Define Arrays code module, so I'll double-click that over in the Navigation pane to open it. And I have a very simple subroutine. Basically, I have the DefineArrays subroutine named, and then I create a variable called ChargesCount, and that is an integer.

An array is a set of values of the same type. And you define an array like you would any other variable, but you add a number, or numbers, in parentheses after the variable's name to indicate the number of elements in the array. So let's say, for example, that you ship products using one of six different shipping rates, and that rate is based on the package's weight. To create an array to hold those six rates, you would use this command. And typing below the ChargesCount As Integer line, it would be "Dim cur." That's the indicator I use for a currency variable.

And then type ShippingCharges. Then a left parenthesis and the number 5, then a right parenthesis, a space, "as Currency," and Return. Now notice that the number in the parentheses is one less than the number of elements. That's because the first element has the number 0 as its identifier. VBA, like most programming languages, starts counting at 0. That means an array with six items will have those items numbered 0 through 5.

If you want to number array item starting at 1, you can. To do that, you click in the declaration section and type "Option Base 1" and then Return. If you do that, make sure that you put comments elsewhere in your code so that your colleagues will know that's what you've done. In most cases, you should stay with 0 unless there's a good reason not to, so I'll go ahead and erase Option Base 1 by selecting it and then pressing Delete a couple of times. Now the next question is, how do you populate the array? There are a number of ways you can do that.

You can use a DoWhileLoop to read values in from a table, for example, or you can assign the values directly. So I'll click below the two declaration statements, press Enter, give myself a little bit of room to work. Now let's say that I want to create a variable called ShippingTypes. So for that it would be Dim strShipTypes, and again, that's all one word. And I will declare it as a variant.

Now note that even though I have named ShippingTypes as a string, or at least used my indicator to indicate that it will contain string values, I have to define the variable as a variant. The reason is that anytime you create an array, you must use the variant variable type. That's okay, because a variant can handle any type of data within it. With the ShipTypes variable defined as a variant, now I can define the array values. So for that it would be strShipTypes, then equal, and then a space, and the keyword Array, then a left parenthesis, and a series of series of values that you'd want to put into the array.

All of the strings must be within the sets of double quotes. So ("Overnight","TwoDay","ThreeDay", "Ground"), because that's the last item I want to put in, and press Enter. Now let's say that I want to display one of those values in a message box. To do that, type in "MsgBox," a space, and then variable name, which is strShipTypes, then a left parenthesis and then the number of the item.

Now remember that unless you change it, the first item is number 0. So I'll type in a 0 and then a right parenthesis and a down arrow so that we can see the line without interference. So I have a message box that displays the string ShipTypes array element 0, and that should be Overnight. So I'm going to press F5 to run the code. I see Overnight in the message box. The procedure I showed you assumes that you know how many items will end up in your array, but let's suppose you're creating an array from user input and you don't know how many items they'll enter.

In that case, you can create a dynamic array and change the array's size using a Redim statement later in the program. To define a dynamic array, you use a statement such as this one. You would say Dim curShippingCharges, but then instead of having a number inside of the parentheses, you would just leave it blank. So that means that you are defining it as an array, but you don't know how many elements will be in it. Now you can use what's called a Redim statement later on in your program to indicate the size.

So let's say that I want to work with the ShippingCharges. So I'll type Redim, a space, then curShippingCharges, and we'll make it 5. End the parenthesis and press Enter. So that redefines it. The problem is that if you re-dimension an array, Access deletes the existing values in the array unless you use the Preserve keyword after Redim. So, for example, if I wanted to keep my existing values in the ShippingCharges array, I would need to type Preserve after Redim, Preserve, and then press the down arrow.

Doing so would keep existing values inside of the array. Arrays help you maintain and look up data quickly without having to look up values in your tables. Maintaining these values in memory speeds up program execution and it simplifies your programming job significantly.

Show transcript

This video is part of

Image for Up and Running with VBA in Access
Up and Running with VBA in Access

67 video lessons · 13410 viewers

Curt Frye
Author

 
Expand all | Collapse all
  1. 2m 23s
    1. Welcome
      58s
    2. What you should know before watching this course
      40s
    3. Using the exercise files
      45s
  2. 20m 32s
    1. Introducing Access Automation
      3m 45s
    2. Running macros
      2m 30s
    3. Working with data macros
      4m 15s
    4. Running a macro by clicking a command button
      6m 12s
    5. Managing macro security settings
      3m 50s
  3. 27m 54s
    1. Introducing object-oriented programming
      3m 11s
    2. Examining the Access Object Model
      4m 29s
    3. Working in the Visual Basic Editor
      2m 59s
    4. Creating, exporting, and deleting code modules
      6m 59s
    5. Creating a subroutine
      1m 41s
    6. Creating a function
      4m 12s
    7. Adding comments to your code
      3m 0s
    8. Running a VBA procedure
      1m 23s
  4. 19m 46s
    1. Managing errors in VBA code using On Error statements
      5m 18s
    2. Stepping through a subroutine or function
      3m 51s
    3. Setting breakpoints in your code
      2m 56s
    4. Verifying output using temporary message boxes
      4m 38s
    5. Watching a value in a routine
      3m 3s
  5. 43m 35s
    1. Introducing Access data types
      3m 23s
    2. Declaring variables and requiring declaration before use
      6m 21s
    3. Managing variable scope
      5m 5s
    4. Defining constants and static variables
      5m 23s
    5. Creating a calculation using mathematical operators
      5m 12s
    6. Displaying a calculated result in a message box
      7m 24s
    7. Defining an array
      6m 21s
    8. Defining and using an object variable
      4m 26s
  6. 28m 37s
    1. Repeating a task using a For...Next loop
      4m 15s
    2. Stepping through all items of a collection using a For...Each loop
      4m 42s
    3. Repeating a task using a Do...While loop
      4m 15s
    4. Repeating a task using a Do...Until loop
      3m 42s
    5. Performing a task when conditions are met using an If...Then statement
      6m 40s
    6. Selecting actions using a Case statement
      5m 3s
  7. 40m 0s
    1. Opening a form
      6m 18s
    2. Opening a report
      4m 45s
    3. Opening a table
      3m 30s
    4. Opening a query
      3m 24s
    5. Closing an object
      3m 59s
    6. Closing a database or quitting Access
      3m 52s
    7. Sounding beeps and displaying the hourglass cursor
      4m 6s
    8. Running a menu command
      2m 28s
    9. Printing the active database object
      3m 1s
    10. Displaying or hiding warnings
      4m 37s
  8. 48m 26s
    1. Displaying every row in a table (Recordset)
      3m 42s
    2. Adding a new record to a table
      3m 34s
    3. Editing values in an existing table row
      6m 23s
    4. Preserving data integrity using transactions
      4m 37s
    5. Displaying a table property using the TableDef object
      4m 14s
    6. Closing a Recordset
      3m 39s
    7. Deleting the current record
      4m 49s
    8. Finding records within a table
      8m 32s
    9. Moving within a Recordset
      5m 12s
    10. Counting the records in a Recordset
      3m 44s
  9. 16m 30s
    1. Summarizing values in a table field
      3m 22s
    2. Finding the first or last value in a table field
      2m 58s
    3. Looking up a value in a table field
      3m 31s
    4. Create a progress bar using the SysCmd object
      6m 39s
  10. 30m 7s
    1. Allowing or disallowing additions, deletions, and edits
      5m 9s
    2. Manipulating form filters
      6m 15s
    3. Setting the caption and background picture
      5m 25s
    4. Requerying and repainting forms
      4m 40s
    5. Discovering a record source
      3m 3s
    6. Setting the ScrollBars property
      2m 52s
    7. Rendering a form or reporting visible or invisible
      2m 43s
  11. 53s
    1. Additional resources and final thoughts
      53s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Up and Running with VBA in Access.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.