• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Functions Versus Subs

You have already examined some of the differences between functions and subs. Functions return values; subs don’t return anything. Functions can be called from almost any place in Access, but because sub procedures are not used in expressions, you typically call sub procedures from an event (on a form or report), a function, or another sub procedure. For example, because sub procedures do not return a value, they cannot be used on the right side of an expression the same way you use any intrinsic functions such as Left or Len. For the same reason, you cannot call a sub procedure by assigning it to a variable. Furthermore, if you tried to use a sub procedure with an argument in a place that you normally use a function, such as a query, you receive the message Undefined function (subname) in expression. On the other hand, functions can be called from queries, controls, macros, forms, reports, or other procedures because you can use expressions with all these objects. If not explicitly specified otherwise, both Sub and Function procedures are public by default.

✓ To learn more about public procedures, see “What Is Scope?,” later in this chapter.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint