Using functions

C

C Tate

From time to time I see people pointing to various useful functions one can
use in Access (eg, calculating somebody's age). How does one use these
functions? Are there any sites with instructions? Thanks in advance.
 
D

Dan Artuso

Hi,
Basically you put the functions in a standard module, which makes
them available from anywhere in your code.
Functions will almost always have a return value and will require you
to pass certain parameters.

You call them the same way you call native functions:
Dim dte as Date
dte = Date()

Of course that one requires no parameters, but something like DLookup does.
DLookup is just a function that you call.

Here's a sample of a user defined function. The code below goes in a standard module:
 
D

Dan Artuso

Hi,
Sorry about that.
The code below goes in a standard module. It returns a string with the characters reversed.

Public Function Reverse(strIn As String) As String
Dim strTemp As String
Dim str2 As String
Dim i As Integer
str2 = strIn
For i = 1 To Len(strIn)
strTemp = strTemp & right(str2, 1)
str2 = Left(str2, Len(str2) - 1)
Next
Reverse = strTemp
End Function

You would call it like this:
Dim myString as String
Dim myReversedString as String

myReversedString = Reverse(myString)
 
C

C Tate

Many thanks for taking the time to reply. However, I think you are operating
at a much higher level than I, as a beginner, can [yet!] understand!!

I am assuming I just paste this code into a new module but I am not at all
sure about this Dim bit! It sounds a bit like code and I don't know how to
use that.
 
C

Chris

Functions are little pieces of code that return a value.
Some functions require additional information (arguments),
some do not.

Examples:

Date() returns the current Date
Time() returns the current time

Dlookup("FirstName","Employee","EmployeeID=25") looks in
the Employee Table, finds EmployeeID25, and returns the
value in the FirstName field.

Where to Use:

You can use functions in alot of places. You can use them
in a query by adding a column:

CurrentDate:Date()

You can use them as the control source for a report or
form control. In the properties window, under Control
Source, type:

=Date()
=Dlookup("FirstName","Employees","EmployeeID=25")

Use the Access Help for more information about what
functions are available.


Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top