Solved

Pass variable from one sub to another

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

13 Experts available now in Live!

Get 1:1 Help Now