Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need format and basic instructions for creating custom Functions

Status
Not open for further replies.

CptCrusty1

Programmer
Feb 17, 2005
216
US
Can one of you great and powerful Access gods give me the general idea of how to create a custom function in Access.

I'd like to pass it a value, then have it return the value once its been crunched....


Public Function Yadda_yadda(i as integer)


End


Thats how it starts, but how do I pass the value back?
 
Public Function Yadda_yadda(i as integer)
dim x as integer
x = i * 10
Yadda_yadda = x
End


You could also specify the return data type with
Public Function Yadda_yadda(i as integer) as double
dim x as double
x = cdbl(i) * 10
Yadda_yadda = x
End


Hope this helps.
 
That is a function?

Oh Well!

Functions should be created in a Standard Module.(not in the code behind a form)

Public Function FnSomeThing(YourValue)
FnSomeThing = YourValue*2
end Function

To call the Function and pass a parameter(code in the calling form)

Dim Result,YourValue as interger
YourValue = 5
Result = FnSomeThing(YourValue)

Result will equal 10

Remember a Function only returns a single value for single or multiple parameters

 
Functions should be created in a Standard Module.(not in the code behind a form)

Please expand on your theory behind this...

Danny

Never Think Impossible
 
Hi.... dmksas

Not proposing that I'm an expert at anything.

Creation of Public Functions in Form code can be done..
But must be qualified when called, Form > Function

There a also considerations as to wether the Form is open or closed (I've had difficulties from time to time)

Being placed in a Standard Module requires no qualification.
(This has resulted in no problems)

There are always many cat skinning processes, do whatever works best for you.

I've found that placing Public Functions in a Standard Module makes those Functions painlessly available throughout the project regardless of Form functionality.
 
Yes... But

Creating every function as public in a standard module will increase the memory that your database needs to allocate, also not qualifying a correct return variable will also declare your function as variant, which again increases the amount of memory needed.

Private Functions behind forms that need to utilise them will only allocate the memory temporarily, and thus will be freed once the form is closed.

Rather than this..

Code:
Function MyFunc(pParameter as string)
 'Do Something
End Function

Do

Code:
Function MyFunc(pParameter As String) As Integer
 'Do Something
End Function

However if you are only interested in perform calculations or manipulation on the function's parameter then declare this as a sub.

Danny

Never Think Impossible
 
lewds said:
Functions should be created in a Standard Module.(not in the code behind a form)

lewds said:
I've found that placing Public Functions in a Standard Module makes those Functions painlessly available throughout the project regardless of Form functionality.

I think that is what caused the confusion. Public functions should definitely be in a Global Module, Private Functions in the appropriate location, wherever that may be.

 
dmksas, sometimes it may be necessary (or at least preferable) to have a Variant return type - for example when a Null might be returned.
 
earthandfire..

True, but as I have always been instructed, always include a relevant return value to minimise the memory allocation.

Danny

Never Think Impossible
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top