Solved

Calling Form in Global Function

Posted on 2002-03-08
5
159 Views
Last Modified: 2010-05-02
    In our global functions in code modules, we very often need to extract parameters from variables in the form that called the function, like so:
 
Public Function Foo(frmCaller As Form) As Boolean
   Dim x as Integer
   x = frmCaller.intDataInForm
   ... more code ...
End Function
 
     I'm hoping this is a need that is so common that there is a slick way to handle it. It seems dopey to specify "frmCaller" in so many global functions.
 
     Any thoughts?
0
Comment
Question by:GebhartBob
5 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
Comment Utility
one way to do it:

in your module do:

Public curForm as Form

Public Function Foo(frmCaller As Form) As Boolean
  Dim x as Integer
  x = frmCaller.intDataInForm
  ... more code ...
End Function

in your form code:

set curForm to current Form

public sub command1_click()
   
   set curForm = Me
   call Foo()

end sub    

0
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
Short of passing all the necessary variables as parameters, there really isn't a cleaner way of doing it.

bobbit31's approach is what I sometimes take if my code happens to be in a class instead of a global code module:


My class might have this code:

Public curForm as Form

Public Function Foo() As Boolean
 Dim x as Integer
 x = curForm.intDataInForm
 ... more code ...
End Function

Then, in my code I might have:

Dim oTemp as New MyClass

Set oTemp.curForm = Me
If oTemp.Foo then.....

0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
There are many ways to pass parameters or access form's properties/methods.
Early bound solution would be to define some interface, implement that interface in form(s) and pass "Me" to function:

' in Form:
Implements IFormData
Private Property Get IFormData_intDataInForm() As Integer
     IFormData_intDataInForm = 100
End Property
' to call function, use:
     res = Foo(Me)

' in bas module:
Public Function Foo(frmCaller As IFormData) As Boolean
     x = frmCaller.intDataInForm
End Function

' class IFormData (interface)
Option Explicit
Public Property Get intDataInForm() As Integer
End Property
0
 

Author Comment

by:GebhartBob
Comment Utility
    Thanks, everybody.
0
 

Author Comment

by:GebhartBob
Comment Utility
    Finally, the light dawns on this subject:
 
     If you declare in your standard code module (.bas) the following:
 
     Public frm As Form
 
     Then in the Form_Activate Event of every Form module include as the first line the following:
 
     Set frm = Me
 
     The global variable "frm" will always contain the current Form object, so that references to data, Subs and Functions declared as Public in Forms modules can be coded like this:
 
     frm.Variable = (Whatever)
     result = frm.Function(par1, par2, ... parN)
 
     It's simple, and it works very nicely.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now