Is it possible to initialize a function like do for variables?

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 09:31
Joined
Mar 22, 2009
Messages
936
Hi,

Sometimes we need a one-line function to just get the database path or things like that we cannot do on a query or on a Macro Object.

Like:

Function GetDatabasePath() = currentproject.path

possible?
 
Not like that because that is syntactically neither here nor there.

You can initialize values of functions by passing them through the function parameters (the thingies inside the brackets following the function name), but a function does not keep its internal values unless you do something very special

Do a tutorial on functions (and VBA functions especially).
 
you would write it as

Code:
 Public Function GetDatabasePath() as String
  GetDatabasePath() = currentproject.path
 End Function
and put it in a module

then to use in code e.g.

if GetDatabasePath Like "C:\*" then ....

or in a query

Code:
 SELECT *, GetDatabasePath AS DatabasePath
 FROM myTable
 
you can do it .. .almost as you entered it.
put this in a module

now use getdatabasepath()
note there is no trailing slash.

Code:
 Function GetDatabasePath as string
 GetDatabasePath() = currentproject.path
 end function

snap with CJ
 
Beware of the scope of the variables and the location of your definition. That code will work if the function definition and usage are in the same CLASS module OR if the function, defined anywhere, gets its value from the declaration area of a GENERAL module. The point being that function declarations and function bodies, being VBA code, are subject to the same scope/visibility rules as any other VBA routine.
 

Users who are viewing this thread

Back
Top Bottom