Showing results 1 to 2 of 2

Thread: Working with 'OnlineVariables'

  1. #1
    robertf Gast

    Default Working with 'OnlineVariables'

    OnlineVariables are containers to recognize variable changes.
    This is the most efficient way to handle variable vlaue changes within VBA or external applications...

    [highlight=vb]Dim WithEvents zOLV As OnlineVariable
    Const strOLV As String = "MyOnlineVariableContainer"

    'initialize event on runtime startup
    Private Sub Project_Active()
    'get object if already exists
    Set zOLV = thisProject.OnlineVariables.Item(strOLV)
    If zOLV Is Nothing Then
    'if not exitst, create a new OnlineVariable container
    Set zOLV = thisProject.OnlineVariables.CreateOnlineVariables( strOLV)
    'add variables to the container
    zOLV.Add "Variable1"
    zOLV.Add "Variable2"
    zOLV.Add "Variable3"
    End If
    'activate 'VariableChange' event
    zOLV.Define
    End Sub

    'event is fired when a variable value changes
    Private Sub zOLV_VariableChange(ByVal obVar As IVariable)
    Dim vValue As Variant
    vValue = obVar.Value
    'ignore error values (i.e. on startup)
    If IsError(vValue) Then Exit Sub
    Debug.Print obVar.Name & " = " & vValue
    End Sub

    'release objects on closing the runtime
    Private Sub Project_Inactive()
    'deactivate 'VariableChange' event
    If Not zOLV Is Nothing Then zOLV.Undefine
    Set zOLV = Nothing
    End Sub[highlight]

  2. #2
    robertf Gast

    Default Re: Working with 'OnlineVariables'


    This Sample uses an Array to store the actual Variable-Values for the best performance on reading variable values...

    The vba code has to be placed within the "thisProject" Classmodule.

    Dim WithEvents zOLV As OnlineVariable
    Dim arrValues() As Variant
    
    '|---------------------------------------------------------------------|
    '| init onlinevariable to fill an array with variable values on change |
    '|---------------------------------------------------------------------|
    Private Sub Project_Active()
        ReDim arrValues(1, 2)
        Set zOLV = thisProject.OnlineVariables.CreateOnlineVariables("MyOLV")
        If Not zOLV Is Nothing Then
            zOLV.Add "WIZ_VAR_10"
            arrValues(0, 0) = "WIZ_VAR_10"
            zOLV.Add "WIZ_VAR_11"
            arrValues(0, 1) = "WIZ_VAR_11"
            zOLV.Add "WIZ_VAR_12"
            arrValues(0, 2) = "WIZ_VAR_12"
            zOLV.Define
        Else
            MsgBox ("OnlineVariable ERROR, please restart!")
        End If
    End Sub
    
    '|---------------------------------------------------------------------|
    '| release objects                                                     |
    '|---------------------------------------------------------------------|
    Private Sub Project_Inactive()
        If Not zOLV Is Nothing Then zOLV.Undefine
        Call thisProject.OnlineVariables.DeleteOnlineVariables("MyOLV")
    End Sub
    
    '|---------------------------------------------------------------------|
    '| fill an array with the actually change variable values...           |
    '|---------------------------------------------------------------------|
    Private Sub zOLV_VariableChange(ByVal obVar As IVariable)
    Dim vValue As Variant
    Dim i As Integer
    Dim bAdded As Boolean
    
        vValue = obVar.Value
        If IsError(vValue) Then Exit Sub
        'update value within the array
        arrValues(1, GetArrayIndex(obVar.Name)) = vValue
    End Sub
    
    '|---------------------------------------------------------------------|
    '| get index of array entry by the name of the variable                |
    '|---------------------------------------------------------------------|
    Function GetArrayIndex(strVarName As String) As Integer
    Dim i As Integer
        For i = 0 To UBound(arrValues, 2)
            If arrValues(0, i) = strVarName Then
                GetArrayIndex = i
                Exit Function
            End If
        Next i
    End Function
    
    '|---------------------------------------------------------------------|
    '| get value of array entry by the name of the variable                |
    '|---------------------------------------------------------------------|
    Function GetArrayValue(strVarName As String) As Variant
    Dim i As Integer
        For i = 0 To UBound(arrValues, 2)
            If arrValues(0, i) = strVarName Then
                GetArrayValue = arrValues(1, i)
                Exit Function
            End If
        Next i
    End Function
    
    '|---------------------------------------------------------------------|
    '| Read Value by VariableName from array...                            |
    '|---------------------------------------------------------------------|
    Sub ReadValue()
        MsgBox "WIZ_VAR_10 = " & GetArrayValue("WIZ_VAR_10")
    End Sub

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •