Avatar of Biggles1
Flag for United States of America asked on

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!


Microsoft AccessVBA

Avatar of undefined
Last Comment

8/22/2022 - Mon

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 Tsioumpris

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
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

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)