Access VBA procedure

Biggles1
Biggles1 used Ask the Experts™
on
Hi,

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!

Thanks,

Biggles1
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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.
Distinguished Expert 2017

Commented:
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.
Software & Systems Engineer
Commented:
just make the method you want on FormB public and refer to it simply from FormA by entering Form_FormB.YourMethod
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Biggles1Founder/CEO

Author

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 Developer

Commented:
Caveat: The Form_FormB.YourMethod will create a new hidden instance, when FormB is not opened.
Distinguished Expert 2017

Commented:
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)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial