Up and Running with VBA in Access
Illustration by Richard Downs

Defining constants and static variables


From:

Up and Running with VBA in Access

with Curt Frye

Video: Defining constants and static variables

Normally, when you work with variables in a code module you want the variables values to reflect the current run of the code and then reset to 0 for when the modules run the next time. If you want a variable to retain the same value throughout the code run, you can define it as a concept. If you want the variable to remember its value until you close the database, you can define it as a Static variable. For this movie, we will use the Constant and Static code module. And that is over here in the Navigation pane, so I'll double-click it to display it.
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 your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course Up and Running with VBA in Access
4h 38m Advanced Oct 06, 2011

Viewers: in countries Watching now:

Enhance productivity by automating routine tasks and providing custom functionality not built into Access with a few basics in VBA code. Author Curt Frye introduces object-oriented programming and provides database designers with a foundation in the Access object model and the Visual Basic for Applications (VBA) programming language. The course covers automating tasks with macros, working in the Visual Basic Editor, writing functions, adding logic, reading data, controlling forms, and more.

Topics include:
  • Working with macros and managing macro security settings
  • Creating, exporting, and deleting code modules
  • Running a VBA procedure
  • Debugging VBA code
  • Defining variables, constants, and calculations
  • Displaying a calculated result in a message box
  • Repeating tasks with loops
  • Creating conditional statements
  • Manipulating database objects, such as forms, tables, and queries
  • Adding, editing, and deleting records
  • Looking up values in a table
  • Requerying and repainting forms
Subjects:
Business Developer IT
Software:
Access Visual Basic for Applications
Author:
Curt Frye

Defining constants and static variables

Normally, when you work with variables in a code module you want the variables values to reflect the current run of the code and then reset to 0 for when the modules run the next time. If you want a variable to retain the same value throughout the code run, you can define it as a concept. If you want the variable to remember its value until you close the database, you can define it as a Static variable. For this movie, we will use the Constant and Static code module. And that is over here in the Navigation pane, so I'll double-click it to display it.

I have two subroutines in this code module: the first one displays a delivery charge for regular delivery and then displays a message box indicating the number of regular deliveries, and then the second subroutine does the same thing only for special deliveries. The difference between the two is that a regular delivery costs $20 and a special delivery costs $37. To begin, let's work with the SpecialDelivery subroutine. And I'll remove the comment from the Constant assignment line.

And how that works is it uses the Constant or Const keyword, then the name of the variable, and then the value you're signing as a constant. So the current delivery charge is 37. If you press F5 to run the code, Access displays a message box indicating the charge is 37, and when you click OK, it says that the "Number of special deliveries is 1." And click OK again. Once you assign a variable as a constant, you can't change its value.

So let me type in "curdleCharge" and then = and make it, say, 25 and press Enter. Now when I press F5 to run the code, Access indicates that you cannot assign a value to a Constant. So I'll click the OK button, click the Reset button to get out of editing mode, and then delete the line I just typed in. Now, let's see what happens if I add the comment indicator back to that Constant declaration line.

So I'll just click here to the left of the Const keyword and type a single quote, which indicates its a comment. Now I'm going to go up to this Constant definition line, which is above the first sub declaration, and remove the comment from it, and what that does is it assigns a Constant value of 20 to the DelCharge variable. And because this is above the first subline, it is now a global variable, or in this case a global constant.

So if I were to click in the first subroutine and press F5 to run it, I would get a delivery charge of 20, deliveries is 1. And if I go to the second subroutine and press F5, I also get a deliver charge of 20, and special deliveries is 1. If you define a Constant as a global variable then you can always change that value within another subroutine. So let's say, for example, that I remove a comment marker from the Constant declaration in the SpecialDelivery subroutine, so now what happens is that the value in the SpecialDelivery subroutine is 37--in other words, that's the deliver charge--whereas the value for every other subroutine--there is only one in this case, but you get the point--is 20.

So with the cursor flashing within the SpecialDelivery subroutine, if I press F5, I get a deliver charge of 37. Click OK, and special deliveries is 1. So those are Constants. Now let's talk about Static variables. A Static variable retains its value, regardless of how many times you run the subroutines, until you close the database. So every time that I have run the RegularDelivery or SpecialDelivery subroutine, it's always said that the number of deliveries is 1, regardless of how many times I have done it.

If you make a variable Static, it retains its value. So let's work with the first subroutine, and I will remove the comment marker from the Static lngAllDeliveries line and then press the down arrow. So now that I have declared that the variable is Static, I should see the lngAllDeliveries variable increment because the line right below that indicates that every time the procedure runs 1 should be added to that value. So let's do it a few times. I will press F5 and I get a delivery charge is 20. Click OK.

Number of regular deliveries is 1. Click OK. Now, F5 again. Deliver charge is 20, but the number of regular deliveries is 2. Click OK and just one more time. Charge is 20. Deliveries is 3. That declaration of a Static variable only works within this RegularDelivery subroutine. So if I were to go down to SpecialDelivery and press F5 to run it, the charge is 37--click OK--but the number of special deliveries is 1. So this variable is different from the Static variable that's declared within RegularDelivery. And one thing you can't do is make a variable Static in the Declarations section, so there are no global Static Variables.

Constant and Static variables give you a great deal of flexibility in assigning and to tracking values within a code module. I'm sure you'll find a lot of uses for them.

There are currently no FAQs about Up and Running with VBA in Access.

 
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

* Estimated file size

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 ?

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.