Explaining Funcions

G

GreySky

Here's my version of the answer.

Q. The basic concepts of functions in English?

A. To take various inputs, and return a result.

example:
' input variable ' output type
Function SayHi(intHowManyTimes as Integer) As String

' declare variables
dim a as integer
dim strHi as string

' loop as many time as requested
' and say Hi that many times in a row
for a = 1 to intHowManyTimes
if strHi = "" then
strHi = "Hi!"
else
strHi = strHi & " Hi!"
end if
next a

' return the result
SayHi = strHi

End Function

To use this function, you would say:
strAnswer = SayHi(5)

And strAnswer would then = "Hi! Hi! Hi! Hi! Hi!"


Q. When to use [], (), !. This translates to: When to use
brackets, parentheses, and bang.

A1. Use [] around field names or table names that
otherwise could be misinterpreted. These is most commonly
associated with field names containing spaces or
illegals. For example Forms!frmMyForm doesn't need
brakets; however, Forms![My Form With Spaces] absolutely
does.

A2. Use parentheses to specify operator precedence (such
as perform subtraction before multiplication). Also,
parentheses are commonly used to refer to elements of
arrays or elements within the "default collection" of an
object. For example: i(1) = 2nd element of array i. Forms
(1) = 2nd form in the forms collection (i.e., 2nd item of
the default collection for the object Forms).

A3. Use bang to denote forms, controls, and field names.
It's short-hand and somewhat databasey. For example:
Forms!frmMyForm!txtMyControl will refer to the text box
txtMyControl on the form frmMyForm. You could just as
easily use the pure-VB style of Forms("frmMyForm").Controls
("txtMyControl"). Bang also is used to refer to field
names in recordsets. For example: rs!field1. Again, the
VB style is rs.fields("field1").

I this helps clarify your muddle.

David Atkins, MCP
 

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