Avatar of repco
repco
 asked on

Call subroutine from another form using string ms access

hi i cannot find this solution anywhere!! i'm trying to call on a subroutine from another form.

for example form A contains command7_click ( i made it public sub routine) and i want to call that command7_click from another form (form b). i can easily call it by using forms!formA.command7_click but the command7_click is a string on formb as i'm trying to pass what subroutine will be used to form b.
usecommand = "command7_click"

how can i call command7_click from FormB using string variable?

application.run command7_click will not work.
forms!formA.usecommand does not work
form!formA(usecommand) does not work
eval(usecommand) does not work
please help!
Microsoft AccessVisual Basic Classic

Avatar of undefined
Last Comment
Joe Howard

8/22/2022 - Mon
crystal (strive4peace) - Microsoft MVP, Access

to call from somewhere other than the current module, the procedure needs to be PUBLIC not Private (as it is by default)

Public command7_click 'etc

As long as you are changing the code, please FIRST: give the button a good name like cmd_Whatever WHERE Whatever is a logical description ... then delete the initial procedure that was created with the old name.

First get the code to work with an absolute name then experiment with giving it a variable name. Here is one way to call
Call Forms.MyFormname.MyProcedure       'where MyProcedure would be cmd_Whatever_click (what is now command7_click)

Open in new window

PatHartman

Code in a form can only be run when the form is open.  When you want to share code among forms, put it in standard modules and pass in a form reference so the sub can access controls on the calling form.  This also requires that you use consistent names on all the forms where you want to share the code or you can pass in all the data fields as arguments.
crystal (strive4peace) - Microsoft MVP, Access

Pat has a valid point about generalizing what you can. Occasionally, I do leave code behind a form and make it public,  when it mainly manipulates controls as opposed to calculating.  What do your procedures do?

Pat also mentioned using arguments. Why do you have DIFFERENT procedures? Why not have one procedure and make it more flexible? It is easier to pass parameters (arguments) than it is to have variable procedure names (as you are discovering)

What circumstances cause one procedure to execute and not another?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
repco

ASKER
thank you for your comments. what i'm doing is trying to call on a subroutine from a different form but using a string variable as the subroutine name. i have tried creating a testme subroutine on the form and setting it as public but i cannot access it. i get error application defined or object defined error.

in form b
dim usemodule as string
usemodule = "testme" < this it the name of the subroutine in form A
forms!forma.usemodule does not work
forms!forma.testme does work but i need to use it as usemodule since that usemodule name changes depending on the form im working with.

basically this formB is an authorization form that will not let the user continue until a password is put it, it would then resume the command clicked on formA by calling on its subroutine. this authorization form formB is universal so different forms and command clicks are passed to it. then calling on its subnroutine. forms(prevform).(prevsubroutine) < this does not work

i hope this makes more sense.
Ark

You can use CallByName function:
CallByName Forms.MyFormname, "command7_click", vbMethod

Open in new window

PatHartman

I would not use a form to control the validation code.  Put the validation code in a standard module and call the procedures in that module from each form when you need to check security.  This procedure can either use an input box or open a small form to collect the password.  Your validation function then just returns true or false to the calling form and the form returns the error message or continues.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
repco

ASKER
PatHartman when opening a small form to collect the password i cannot return to the original form with the true false message and then resume the function because its already been stopped since a new form has been opened.
repco

ASKER
callbyname does not seem to work
ASKER CERTIFIED SOLUTION
PatHartman

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Joe Howard

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: PatHartman (https:#a42084739)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

MacroShadow
Experts-Exchange Cleanup Volunteer
Your help has saved me hundreds of hours of internet surfing.
fblack61