[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Calling Form in Global Function

Posted on 2002-03-08
Medium Priority
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?
Question by:GebhartBob
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Accepted Solution

bobbit31 earned 200 total points
ID: 6851087
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    

LVL 18

Expert Comment

ID: 6851509
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.....

LVL 15

Expert Comment

ID: 6851832
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

Author Comment

ID: 6857439
    Thanks, everybody.

Author Comment

ID: 7063282
    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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

649 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