From the January 2016 Issue of Prestwood eMag
Access Language Details:
Access VBA Custom Routines (Sub, Function)
Posted 11/11/2008 on 11/11/2008 and updated 1/29/2009
Access Code Snippet:


Languages Focus: Custom Routines

For non-OOP languages, a custom routine is a function, procedure, or subroutine and for pure OOP languages, a custom routine is a class method. Hybrid languages (both non-OOP and OOP) combine both.

Access VBA Custom Routines

Access VBA is a non-OOP language with some OOP features. It offers both Subs and Functions. A Sub does not return a value while a Function does. When Subs and Functions are used in a class module, they become the methods of the class.

Syntax Example:
Sub SayHello(ByVal pName As String)
  MsgBox ("Hello " & pName)
End Sub
Function Add(pN1 As Integer, pN2 As Integer) As Integer
  Add = pN1 + pN2
End Function

Access VBA Sub & function Example

If you define the following sub and funtion:

' Subs and Functions
Sub DoSomeCode(ByRef pMessage)
    MsgBox (pMessage)
End Sub
Function GetLastDOW(ADate, AWeekDayConst)
    While Weekday(ADate) <> AWeekDayConst
        ADate = DateAdd("d", -1, ADate)
    GetLastDOW = ADate
End Function

You can call them. For example, the following code is from the click event of a button:

Private Sub Toggle6_Click()
    DoSomeCode ("Hello3")
    MsgBox "Last Monday was " & GetLastDOW(Date, vbMonday)
End Sub
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile For service: 916-726-5675
Copyright (C) Prestwood IT Solutions.
All Rights Reserved.
Printed 7/6/2020