Solved

Pass variable from one sub to another

Posted on 2013-02-01
5
240 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
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…

911 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

21 Experts available now in Live!

Get 1:1 Help Now