Solved

Pass variable from one sub to another

Posted on 2013-02-01
5
243 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 - Access MVP) 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now