• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 83
  • Last Modified:

Access VBA procedure

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
0
Biggles1
Asked:
Biggles1
1 Solution
 
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.
0
 
PatHartmanCommented:
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.
0
 
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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.
0
 
ste5anSenior DeveloperCommented:
Caveat: The Form_FormB.YourMethod will create a new hidden instance, when FormB is not opened.
0
 
PatHartmanCommented:
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)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now