Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass variable from form code to macro. 1

Status
Not open for further replies.

barboza

Programmer
Oct 20, 2007
7
US
How can one pass a variable form form code to macro code.

StrA is a variable with the value of 8 used in the vba code of a form - frmCustody

We have a macro called Test

how can macro test run the line:

MsgBox strA

and have the value 8 appear in the message box.

Thanks,

B

 
Hi,

A sure-fire way is to define strA in a module.
It doesn't matter where you assign 8 to strA - the macro will pick it up.

This is due to 'scoping': a module defintion allows ALL objects to 'SEE' strA then.

However, this is probably not the right way to do this.

I personally do not agree with using macro's in any way, shape or form, therefore I have no idea how you pass a parameter to a macro (or even know if it's possible).

Don't use macro's - for God's sake

ATB

Darrylle

Never argue with an idiot, he'll bring you down to his level - then beat you with experience.
 
Hi Darryl,

Please forgive my using the wrong term for Macro - my familiarity is with vba in excel. All macros in excel are in vba, and that is all I write in. Just used the wrong term.

You are indeed correct, it is a matter of scope.

In excel/vba any code called from a form has access to variables declared in the form -

Dim strA would be passed to any code called from that form.

Apparently Access declaration of variables is different than in excel.

I always try to declare variables as minimally as possible.

I went from Dim to Public to Global before finally getting the variable to go from the form to the code in Run ("Test") that is in Module1.

In any event, I am pretty much bummed that I had to "go Global" to get variables passed from a module to a form and from a form to a module.

Thanks for your help.

B
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top