Last month our function contained the following code
(if you need to review check out August
06):
Public Function CubitsToInches(dblCubits As
Double) As Double
CubitsToInches = 30 * dblCubits
End Function
The value 30 is our conversion factor, and it works
wonderfully for me since it represents the length of
my cubit. What about Andre the Giant who has a
cubit of 54 inches, or Mildred the Midget who has a
cubit length of 12.5 inches? We could hard code
separate functions for those varying lengths, but
wouldn't life be easier if we could just tweak our
function to handle cubits of different lengths? That's
just what we'll do! And to make life even easier, once
we've entered the length of a person's cubit, we
never want to worry about it again. This situation
calls for a cubit table – like the following.
Our table has three fields
- An AutoNumber primary key called CubitKey – I'm
a fanatic about primary keys; see Keys are
Critical.
- A Person field to record the person's name
- A cubit length (CubitLen) field where we'll record
the length of the cubit for each person.
Once we populate the table it looks like this:
Now we can tweak our function code to handle
variable cubit lengths for individuals from the cubit
length table.
Our revised function looks like this
Function CubitsToInches(dblCubits As Double,
strPerson As String) As Double
Dim dblInchesPerCubit As Double
dblInchesPerCubit = DLookup
("CubitLen", "tblCubit", "Person = '" & strPerson & "'")
CubitsToInches = dblInchesPerCubit *
dblCubits
End Function
Compared to our earlier function we've made just a
few changes:
- We've added another argument (see last month's
issue for an explanation of arguments)
strPerson As String to the function. This code
allows a user to select a person and associated cubit
length to be used for the conversion. We're asking
that this value comes in as a text string, hence the
argument As String
- We've added a new variable Dim
dblInchesPerCubit As Double. This variable will
contain the conversion factor (the inches per cubit
for each individual). For example, Andre’s
conversion value (inches per cubit) is 54; for Mildred
it will be 12.5.
- We've added a line to lookup the value of inches
for the person requested: dblInchesPerCubit =
DLookup("CubitLen", "tblCubit", "Person = '" &
strPerson & "'")
This lookup function is a powerful way of easily
getting a value from a table. It's part of a much
larger discussion of domain functions, so I won't go
into it too deeply here, but basically it takes three
arguments
- the field to lookup, in our case CubitLen
- the name of the table tblCubit and
- the criteria: "Person = '" & strPerson
& "'"
The criteria is the only tricky part, so I'll break that
down for you. It says give me the record where the
field "person" is equal to the variable
strPerson. In other words, if the conversion is
to be done for Mildred, go to the record where the
field Person is equal to "Mildred."
We also have some punctuation to deal with. Each of
the three arguments: Field Name, Table Name, and
Criteria are surrounded by double quotes, and our
criteria field Person is a text field so that the person
we are looking for must also be in quotes, but
because the criteria field as a whole is in double
quotes, when we designate the person we use single
quotes. Finally since we're using a variable
strPerson we also are doing concatenation –
hence our use of the ampersand.
I recognize that this is not terribly friendly or easy –
many users have trouble with dlookup statements
and I've not fully covered them here. I'll be doing
that in a future issue. For your purposes in the mean
time when you use a dlookup statement use the
Access Help system to get an explanation. Though
the help system is not terribly good, it will give you a
start.