Daily Access Tips

Answering your common Microsoft Access questions…
  • rss
  • Home
  • About
  • Contact
  • Newsletter
  • Video Tutorials
  • Help Request Form

How To Pass A Value From One Place To Another In Microsoft Access

June 14, 2009 | 7:42 pm

Here are three questions that have been asked of me regarding how to pass a value from one place to another in MS Access:

  1. I have multiple queries and need a form for a date to be entered once. Rather than have to enter the same dates for 16 queries. I would like to set them up with a temporary variable somehow so that each query could read the date and the user would only have to enter the date range once.
  2. I have a query and want to establish criteria based on a combo box on a Microsoft Access form. When I leave the form’s combo box blank I want to include everything in the query output otherwise just the item in selected in the combo box.
  3. What is the best way for me to get the value of a variable from a subroutine into another subroutine or a query?

I commonly get asked how to pass a value from one location in Microsoft Access to another. In VBA, Visual Basic for Applications, code a value that is passed or referenced later in code is called a variable. If it is referenced later out side of the form or code module were it was first given a value, then we need the variable value to be visible broadly throughout the application. This public variable (called “strFilter ” below), then needs to be placed in a code module that you create in Access. A simple function is then needed to retrieve the value of your public variable that you have declared in that code module you created. At the end of this article you will find a video tutorial demonstrating how to do this. Note that versions of Access 2007 or later have something called “tempvars” which serve this purpose with some additional benefits. You can find an article on this at http://www.vb123.com.au/toolbox/09_access/needforspeed.htm.


Create a new code module where you will declare your public variables and write a function to get them when they are needed.

Declare the public variable in a code module using this single line of code in the declaration area at the top of the code module you just created:

Public qrytesting as Variant

Note that the word “Public” allows this value to be visible from anywhere in the Microsoft Access programming.

Now create the public function in this code module to get the value of the public variable:

‘This function requires the name of the public variable it is to retrieve, in this case “strFilter”

Public Function GetPublicVariable(VariableName As String) As Variant
Select Case VariableName
Case “strFilter”
GetPublicVariable = strFilter
Case Else
MsgBox “You need Variable Name on GetPublicVariable function”
End Select
End Function

You set the public variable like this:
strFilter = [NameOfFormControl]
or
strFilter = some value

or

strFilter = another variable

This code is usually placed in the after update event of a form control.

You will retrieve the value of the set public variable with this line of code:

GetPublicVariable(“strFilter”)

GetPublicVariable(“strFilter”) can be used as the query criteria of the query above called “qrytesting”.

Regarding: “When I leave the form’s combo box blank” you could use this combination in the
Access query grid’s criteria cell:


Like GetPublicVariable(“strFilter”) & “*”
The Like operator will give you anything starting with the value that is currently being held
in the public variable call “strFilter”, however if nothing is stored there, a null value,
then Microsoft Access wild card value, the *
asterisk, would be used for retrieving all values.


A simpler function to retrieve the public variable could have been written as seen below, but
it would not have been reusable for other public variables.


Public Function GetStrFilter() as Variant
GetStrFilter = strFilter
End Function

from youtube

I hope this has been useful.  Let me know.

Bob Heifler


Bookmark and Share

Categories
Access Custom Functions, Access Forms, Access Queries, Programming Code
Comments rss
Comments rss
Trackback
Trackback

« Avoid Higher Computer Consultant Costs By Using 11 Point Checklist Use Wizard to Link Microsoft Access to MS Access Data (Part 4 of 4) »

3 Responses to “How To Pass A Value From One Place To Another In Microsoft Access”

  1. Bob Green says:
    October 3, 2009 at 1:56 pm

    I have used the simpler version. I like you multiple use version and the ability to use the wildcard. Excellent coding demonstration and explanation, as usual!

  2. Anonymous says:
    October 3, 2009 at 2:29 pm

    Hi Bob, two points:
    1) “GetPublicVariable(”strFilter”) can be used as the query criteria of the query above called “qrytesting”.” There’s no query “above” called qrytesting.
    2) “This function requires the name of the public variable it is to retrieve, in this case “strFilter””
    If you compare it with your video, you’ll see that the name of the public variable is not “strFilter” but rather qrytesting because that’s what you’ve declared.

  3. admin says:
    October 4, 2009 at 7:59 am

    Thanks for catching this.

Leave a Reply

Click here to cancel reply.

Categories

  • Access Custom Functions
  • Access Forms
  • Access Queries
  • Access Tables
  • Miscellaneous
  • Programming Code

Recent Posts

  • Access MVP Recognition
  • Microsoft Access 2010 Release News
  • Microsoft Access Security All Versions
  • Not For Null

Twitter Money On Autopilot

Get Twitter Followers Fast & Easy! 100% FREE Plus $1000's in free bonuses!

Bob Heifler

  • About
  • Contact
  • Newsletter
  • Video Tutorials

Access Resources

  • Microsoft Access Demonstration Files
  • Move data from Access to Excel
  • MyAccessProgram.com
  • Services Offered
  • Tips and Best Practices
  • Video Tutorials

Free Twitter Autopilot!

Get Twitter Followers Fast & Easy! 100% FREE Plus $1000's in free bonuses!

more help…

Microsoft Access Help

Bookmark and Share
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox