?
Solved

Pass variable from one sub to another

Posted on 2013-02-01
5
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

762 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