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

Creating a subroutine

From: Up and Running with VBA in Access

Video: Creating a subroutine

When you read about VBA programming you might see the writers refer to subroutines and functions. Even though some authors use the two terms synonymously, they actually mean different things. The generic name for both subroutines and functions is a procedure. To demonstrate how to create the subroutine, I'll press Alt+F11 to open up the Visual Basic Editor and then click Insert > Module. You create a subroutine by opening a code module and then typing Sub, then a space, and then the subroutine's name.

Creating a subroutine

When you read about VBA programming you might see the writers refer to subroutines and functions. Even though some authors use the two terms synonymously, they actually mean different things. The generic name for both subroutines and functions is a procedure. To demonstrate how to create the subroutine, I'll press Alt+F11 to open up the Visual Basic Editor and then click Insert > Module. You create a subroutine by opening a code module and then typing Sub, then a space, and then the subroutine's name.

The subroutine's name can have no internal spaces, and it has to start with a letter. So in this case I will make it Welcome, then type left and right parenthesis--in this case with nothing between them--and then press Enter. When you press, Enter Access creates the subroutine, adds an End Sub statement, and gives you a blank line where you can start typing your code. In this case, I'll have a display or message box that says, "Welcome to the Shipment Tracking database." So MsgBox and don't worry; we'll cover message boxes later. Then the space, left parenthesis, double quotes, and then I'll just type in "Welcome to the Shipment Tracking database," then a period, and double quotes, and a right parenthesis.

Now when I press F5 Access runs the macro and displays my message box. Subroutines are the backbone of Access programming. You use them to manipulate database objects and add functionality for your users.

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 · 13122 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 preferencesfrom 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.