Everton
Everton

Reputation: 15

How to create a global function and pass variables to it in VBA Access?

In my program I am trying to cut down on repetitive code by putting it into global functions.

In my current setup I am calling the function like:

Call [Global variable].Close

(global variable is the name of the class module).

This function is looks like;

 Public Function Close()
  DoCmd.Close
  Cancel = True
End Function

Unfortunately this wont compile properly. What am I doing wrong?? (As an aside I also want to pass variables to and from this function and want to know how this would be done).

Thanks for your help.

Upvotes: 1

Views: 4298

Answers (1)

ThunderFrame
ThunderFrame

Reputation: 9461

If you're using a class module, you need an instance of that module before you can call its members. That requires either creating the class module as pre-declared (which involves exporting the code, editing in Notepad, and re-importing), or creating an instance with the New keyword:

Dim myClassInstance As MyClass
Set myClassInstance = New MyClass
myClassInstance.Close

But, depending on your use-case, you might better off with the function being available in a standard module - then you can just call it from anywhere, including from within a query.

Upvotes: 1

Related Questions