Example: Storing the Last Values Entered

This example shows how to use the TDConnection object to implement persistent data between actions. The lifetime of a variable in a routine is only for the routine run. Therefore, persistent data must be stored if it must be available later. It is recommended that you use the ALM API to store persistent data whenever possible instead of using external objects, files, or the registry.

In this example, a user-defined function SW_KeepLastValue uses the Settings object to save the values typed into the fields BG_DETECTION_VERSION, BG_USER_01, and BG_USER_03 when a user posts a defect. These values are retrieved and assigned as default values when this user adds a new defect.

The user-defined function is called with the SET action from Bug_CanPost, before a new defect is posted by the user. The values in the fields are stored.

Function Bug_CanPost() 
        ' Initialize the function's return value to 
	 ' avoid unpredictable behavior.
        Bug_CanPost = True 
        If Bug_Fields("BG_BUG_ID").Value = "" Then
            SW_KeepLastValue ("SET")
        End If
End Function

The function is called with the GET action from the Bug_New event procedure. When a user adds a new defect, the values stored in the fields for this user are entered into these fields.

Sub Bug_New()
        SW_KeepLastValue ("GET")
End Sub 

Depending on the action passed as a parameter, the user-defined function SW_KeepLastValue stores the values of the fields in the common settings table for the current user, or reads the values from the Settings object and assigns the values to the appropriate fields.

Sub SW_KeepLastValue(action)
Dim tdc, vals, flds
Dim uset, pairs, pair
Dim bld
On Error Resume Next
        bld = ""
        Set tdc = TDConnection
        Set uset = tdc.UserSettings 
        If action = "SET" Then
            flds = Array("BG_DETECTION_VERSION", _
            "BG_USER_01", "BG_USER_03")
            vals = ""
            For i = 0 To UBound(flds)
                If vals <> "" Then vals = vals & ";"
                vals = vals & flds(i) & "=" & _
            'Open category KeepLValueSetting
            uset.Open ("KeepLValueSetting")
            'Setting KeepValueFields in category KeepLValueSetting
            uset.Value("KeepValueFields") = vals
        End If 'SET
        If action = "GET" Then
            uset.Open ("KeepLValueSetting")
            vals = uset.Value("KeepValueFields")
            If vals <> "" Then
                pairs = Split(vals, ";")
                For i = 0 To UBound(pairs)
                    pair = Split(pairs(i), "=")
                    If UBound(pair) = 1 Then
                        Select Case pair(0)
                            Case "BG_USER_03"
                                bld = pair(1)
                            Case Else
                                If Bug_Fields(pair(0)).Value = "" Then
                                   Bug_Fields(pair(0)).Value = pair(1)
				 End If
                        End Select
                        If Bug_Fields("BG_DETECTION_VERSION").Value _
			 <> ""
                        And bld <> "" Then
                            SW_SetLists_VersionsBuilds _
                            "BG_DETECTION_VERSION", _
                            Bug_Fields("BG_USER_03").Value = bld
                            If Err.Number <> 0 Then Err.Clear
                        End If 'Bug_Fields
                    End If 'UBound(pair)
            End If 'vals <> ""
        End If 'GET
        PrintError ("Keep Last Value (" & action & ")")
        On Error GoTo 0
End Sub