Making the Class Function visible from the module.

  • Thread starter Thread starter Morris
  • Start date Start date
M

Morris

Hello,

I've got this logging function fncLogFile defined in code for Form1 (so
in Project Explorer it's placed under Microsoft Office Access Class
Objects.

Now the code at some points calls some code from the module (placed
under Module section in Project Explorer). Now in this module I've this
line of code, where execution stops:

Call fncLogFile("Error " & Err.Number & ": " & Err.Description)

Is it because it's out of scope for module code? how can I make it
visible?

the function definition is this:

Private Function fncLogFile(command As String)
 
You'd be best off putting fncLogFile in a module so that it's accessible
from everywhere in the application.
 
Morris said:
Hello,

I've got this logging function fncLogFile defined in code for Form1
(so in Project Explorer it's placed under Microsoft Office Access
Class Objects.

Now the code at some points calls some code from the module (placed
under Module section in Project Explorer). Now in this module I've
this line of code, where execution stops:

Call fncLogFile("Error " & Err.Number & ": " & Err.Description)

Is it because it's out of scope for module code? how can I make it
visible?

the function definition is this:

Private Function fncLogFile(command As String)

First, if the function is defined as Private, it's not going to be
visible outside of the module containing it under any circumstances.
You'll need to define it as Public. But that alone won't be enough. To
call a function defined in a class module, you need to start with a
reference to an instance of that class module. (That's a bit of an
oversimplification, as Access will *create* an instance of a form's
class module when you refer to it, if there isn't one loaded. However,
you should not allow that to happen.) So, if Form1 is supposed to be
already loaded whenever this function will be called, you could write:

Call Forms!Form1.fncLogFile( _
"Error " & Err.Number & ": " & Err.Description)

From the sound of it, though, the function should not be defined in the
form's module at all. It sounds to me like you may want to call this
function from various places, in which case the function ought to be
defined in a standard module instead, if not in a globally accessible
class module of its own. The latter has some advantages, but for
simplicity's sake, I'd go with defining it in a standard module.
 
Douglas said:
You'd be best off putting fncLogFile in a module so that it's accessible
from everywhere in the application.

So module scope has sort of priority over class scope?
As you say - If I declare it public in the module scope it would be
visible from both - module and all forms in this application?

Cheers,
Morris
 
It's not really a case of module scope having priority over class scope. The
issue is that when it's in a class, you have to reference the class in order
to use public methods within it, like Dirk showed how in his response. Also,
you'd only be able to call it if the form is open: the class isn't
substantiated otherwise.

Yes, if you declare it as public in a module, you'll be able to call it from
all other modules and forms in the application.
 
Back
Top