Access VBA procedure


I have two forms open simultaneously  frmA and frmB
frmA has the focus
I want a procedure in frmA (which has the focus) to launch a procedure which resides in frmB (I assume that frmB will get the focus) and that is actually what I need to happen.

Seems simple enough?  Not for me!


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Getting the syntax right is not that simple, but not that hard also..

FormB code-behind:

Option Compare Database
Option Explicit

Public Sub AMethod(ASomeParameter As String)

  MsgBox ASomeParameter
  Me.Caption = ASomeParameter

End Sub

Open in new window

Important: the method must be declare as Public.

FormA code-behind:

Option Compare Database
Option Explicit

Private Sub btnInvokeFormBMethod_Click()

  Forms("FormB").AMethod "Calling from FormA.."

End Sub

Open in new window

Just add appropriate error handling when invoking the method.
This sounds more complicated than it needs to be and it causes a dependency that may be unnecessary and therefore dangerous.  If you have a common piece of code, best practice would be to put it in a standard module and call it from multiple places.  Can you give us more details on why this internal automation is necessary because unless formA opened formB by using a where argument, formA wouldn't even know what record formB was displaying.
John TsioumprisSoftware & Systems EngineerCommented:
just make the method you want on FormB public and refer to it simply from FormA by entering Form_FormB.YourMethod

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get Blueprints for Increased Customer Retention

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Biggles1Author Commented:
Pat and John are absolutely correct and I have used this method  before.  The reason I need the to launch my procedure from frmA to run in FrmB, is rather complicated, but stems from the application being created by someone who is no longer available.  

FrmB contains a very complex and lengthy set of procedures which I would have to re-write.  It's a question of time and effort:  Re-writing all the procedures in FrmB or simply adding a few lines of code that is launched from frmA.  

I have tested those few lines of code extensively in frmB and it works fine, but my users would need an addition step for this to work.

I'm using John's solution which seems to work so far.
ste5anSenior DeveloperCommented:
Caveat: The Form_FormB.YourMethod will create a new hidden instance, when FormB is not opened.
Are you absolutely certain that you are referencing the record you think you are?  All references to an open form reference the "current" record whatever that is.  When the form first opens, the current record is the first record returned by the RecordSource.  After that, you are referencing what ever record the form moved to.  This is a really bad idea but apparently no one ever teaches coupling and cohesion any more.

The better solution won't actually take that long.  Simply move the common code from the form's class module to a standard module.  Make the procedures public.  If the code references form fields, you would change the references in the standard module from Me. to frm.  When you call the common code, you would pass in a reference to the form object.

Call CommonCode(Me)

Then the header of the common code would be

Public Proc CommonCode(frm as Form)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.