Solved

Pass variable from one sub to another

Posted on 2013-02-01
5
247 Views
Last Modified: 2013-02-08
Is it possible to pass a variable from one sub from another.

i.e. if i have a string x in a before update event, how can i use the value in x in after update event?

Regards
0
Comment
Question by:SweetingA
  • 4
5 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 38845691
In the Declarations section of your Form Module, create a Global Variable:

Private X As String

Then ... once you set X in the BU event - it will still be available in the AU event.

mx
0
 
LVL 75
ID: 38845695
global
0
 
LVL 11

Expert Comment

by:datAdrenaline
ID: 38845696
you can declare a module level variable.  When you do that you can use the variable in any procedure within that module.

For example:

Option Explicit
Option Compare Database

Private strMyStringVariable As String

Private Sub Form_BeforeUpdate(Cancel As Integer)
    strMyStringVariable = "Hello World!!"
End Sub

Private Sub Form_AfterUpdate()
    MsgBox strMyStringVariable
End Sub

Open in new window


Or ... you can use an unbound Text Box control on your Form object, then hide it.  You can then set the .Value property to the value you want.  You can than access the .Value from just about anywhere in the application, as long as your Form is opened.
0
 
LVL 75
ID: 38845704
And better yet, if you have A2010, you can use the TempVars() collection - which will not get reset (aka hosed) if you have an error that resets your code.
They are easy to use.  Kind of a Global Variable on steroids!
0
 
LVL 75
ID: 38845717
Short tutorial on TempVars by Access MVP Juan Soto Here


mx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question