Variables & Procedures (declarations, scope, passing info examples):   Return to List

Option Compare Database
Option Explicit
Private strPrivate As String
Public gstrPublic As String
Public strModule As String

Public Sub LocalVariable()
    Dim strLocal As String
    strLocal = "'Local variable in LocalVariable procedure'"
    MsgBox strLocal
    Call GetLocal(strLocal)' See below
End Sub
Public Sub GetLocal(strA As String)
    MsgBox strA & " passed as an argument to GetLocal"
End Sub

Public Sub StaticVariable()
    Dim strNonStatic As String
    Static sstrStatic As String
    strNonStatic = strNonStatic & " nonstatic"
    sstrStatic = sstrStatic & " static"
    Debug.Print strNonStatic
    Debug.Print sstrStatic
End Sub

Public Static Sub StaticProcedure()
    Dim strNonStatic As String
    Static sstrStatic As String
    strNonStatic = strNonStatic & " nonstatic"
    sstrStatic = sstrStatic & " static"
    Debug.Print strNonStatic
    Debug.Print sstrStatic
End Sub

Public Sub PassingLiteral()
    MsgBox "Literal"
    Call GetData("'Literal'")' See below
    MsgBox "Returning from the GetData procedure"
End Sub

Public Sub GetData(A As String)
    MsgBox A & " is passed to the GetData procedure as an argument."
End Sub

Public Sub PassingVariableByRef()
    Dim strVariable As String
    strVariable = "'Variable to be passed to another procedure'"
    MsgBox strVariable
    Call GetVariableByRef(strVariable)' See below
    MsgBox strVariable
End Sub

Public Sub GetVariableByRef(ByRef strA As String)
    MsgBox strA & " is passed as an argument of the procedure."
    strA = "Variable received. Thank you"
End Sub

Public Sub PassingVariableByVal()
    Dim strVariable As String
    strVariable = "'Variable to be passed by value to another procedure'"
    MsgBox strVariable
    Call GetVariableByVal(strVariable)' See below
    MsgBox strVariable
End Sub

Public Sub GetVariableByVal(ByVal strA As String)
    MsgBox strA & " is passed by value as an argument of the procedure."
    strA = "Variable received. Thank you"
End Sub

Public Sub GetMultiply()
    Dim intX As Integer, sngY As Single
    Dim dtmX As Date
    Dim strX As String, strY As String
    intX = 2
    sngY = 3
    dtmX = #5/11/2001#
    strX = "2"
    strY = "Three"
    MsgBox "Numbers of different data type " & Multiply(intX, sngY)' See below
    MsgBox "Number and date " & Multiply(dtmX, sngY)
    MsgBox "Number and convertible string " & Multiply(strX, sngY)
    MsgBox "Number and non-convertible string " & Multiply(intX, strY)
End Sub

Public Function Multiply(X, Y)
    Multiply = X * Y
End Function

Public Function ChangeCaption(objectname As Object)
    objectname.Caption = "My Caption"
End Function

Public Function Product(X, Y, Optional Z)
    Product = X * Y
    If IsMissing(Z) Then Exit Function' We'd leave at this point instead of proceeding further
    Product = Product * Z
End Function

Public Sub PublicScoping()
    gstrPublic = "'Public'"
    Call SamePublic
    Call OtherPublic' Don't see this one anywhere??
End Sub
Public Sub SamePublic()
    MsgBox gstrPublic & " from the same module.", , "SamePublic"
End Sub

Public Sub PrivateScoping()
    strPrivate = "'Private'"
    Call SamePrivate
    Call OtherPrivate
End Sub

Public Sub SamePrivate()
    MsgBox strPrivate & " from the same module.", , "SamePrivate"
End Sub

Public Sub LifeTimeModule()
    strModule = "'Module-level variable exists until you close the database.'"
    MsgBox strModule, , "LifeTimeModule"
End Sub

Public Sub StillThere()
    MsgBox strModule, , "StillThere"
End Sub



Note to Webmaster