Showing results for 
Search instead for 
Do you mean 
Reply

How can I access the "User Attributes" through VB?

2016-05-20 11_23_55-LMS Test.Lab Desktop Advanced - 2016-04-29 XBB BR10 VCB17 - Sirene.png

I’m trying to set/change the "User Attributes" of a Test.Lab project. So far I only managed to load an attribute list file (*.dtp) using the IBook::LoadAttributesList member:

 

TestLabBook.LoadAttributesList "Z:\Attribute List.dtp", 0, 1

 

I would like to run thought each attribute and update it after the list was loaded.

6 REPLIES

Re: How can I access the "User Attributes" through VB?

The User Attributes of a project can be fairly easily accessed by a VB program because they are part of the "properties" of the project. This can be seen in the Navigator worksheet, if you right-click on the active project, you can select "Properties...", and then the Details tab. This will show you all the properties of the project, including the User Attributes. Note that the names of the User Attributes always have a "UA::" prefix.

To access these properties using Windows Automation, you use the "GetProperties" method of the Test.Lab IDatabase object. This will give you all the properties of the project in an AttributeMap object, which is a fundamental Test.Lab object for holding name-value pairs. The contents of the AttributeMap (i.e. the User Attributes) can be read by looping through each item in the AttributeMap, or changed by using the Add, Remove, or Replace methods of the AttributeMap object.  When finished, the modified properties can be put back into the project using the IDatabase.AddProperties method.

The VB code below shows an example of how User Attributes can be added or modified in a project.  Note that this could also be done for User Attributes that are contained in a Section (instead of a Project).

Dim gTL As LMSTestLabAutomation.Application
Dim gTLDatabase As LMSTestLabAutomation.IDatabase

Public Sub StoreMetadata_Project()
'**************************************************************************
' Store the Meta Data to user attributes inside the active project.
'**************************************************************************
Dim attr As LMSTestLabAutomation.AttributeMap
gTLDatabase = gTL.ActiveBook.Database

' Get the current properties of the Project (or Section)
attr = gTLDatabase.GetProperties("") '<- Use this to store to the Project
'attr = gTLDatabase.GetProperties(MySectionName) '<- Use this to store to a Section

' Add your attributes;
' if the attribute already exists, the 'attr.Add' will do nothing,
' so we also do 'attr.Replace' to replace an existing attribute.
' The attribute name must start with "UA::" in order to be a "User Attribute"
attr.Add("UA::" & myFieldName1, myvalue1)
attr.Replace("UA::" & myFieldName1, myvalue1)

attr.Add("UA::" & myFieldName2, myvalue2)
attr.Replace("UA::" & myFieldName2, myvalue2)

attr.Add("UA::" & myFieldName3, myvalue3)
attr.Replace("UA::" & myFieldName3, myvalue3)

' Put the modified properties back in the Project (or Section)
gTLDatabase.AddProperties("", attr)
'gTLDatabase.AddProperties(MySectionName, attr)

End Sub

 

 

Re: How can I access the "User Attributes" through VB?

[ Edited ]

So I finally had some time to fiddle with this, and while testing I found that the add tribute command is not working for me. It is not creating a new attribute, it's only changing the attributes already created. I'm using the version 15A.

Would you mind checking my code to se if I'm using it right?

Dim tl As LMSTestLabAutomation.Application
Dim TLdB As LMSTestLabAutomation.IDatabase
Dim attr As LMSTestLabAutomation.AttributeMap

Sub Button1_Click()
   
    Set tl = New LMSTestLabAutomation.Application
    Dim TestLabBook As LMSTestLabAutomation.IBook
    Dim TestLabPort As LMSTestLabAutomation.DataWatch
    Dim TestLabData As LMSTestLabAutomation.IData
    Dim BlockPathInData As LMSTestLabAutomation.IData
    Dim BlockPort As LMSTestLabAutomation.DataWatch
    Dim BlockData As LMSTestLabAutomation.IBlock
    Dim XUnitLabel As String
    Dim YUnitLabel As String
    Dim xval() As Double
    Dim yval() As Double
    Dim val() As Double
    Dim xxx As Variant
    Dim i As Integer

    On Error GoTo end_sub

    Set TestLabBook = tl.ActiveBook
    Set TLdB = TestLabBook.Database

    Set attr = TLdB.GetProperties("") '<- Use this to store to the Project
    'Set attr = TLdB.GetProperties("section name") '<- Use this to store to a Section
 
    For i = 1 To 17
        myvalue1 = Cells(1 + i, 3).Value       ' Attribute Value
        If myvalue1 <> "" Then
            Debug.Print Cells(1 + i, 3).Value
            myFieldName1 = Cells(1 + i, 2).Value            ' NOME DO ATRIBUTO
            If myFieldName1 <> "" Then
                Debug.Print Cells(1 + i, 2).Value
                attr.Add "UA::" & myFieldName1, myvalue1
                attr.Replace "UA::" & myFieldName1, myvalue1
                'TLdB.AddProperties "section name", attr, 1
                TLdB.AddProperties "", attr, 1
            End If
        End If
    Next i

    MsgBox "OK"

end_sub:

    Set TestLabPort = Nothing
    Set TestLabData = Nothing
   
End Sub

 
Thank you!

Re: How can I access the "User Attributes" through VB?

Your code seems to be working ok for me.

You can see if the attribute has been created and stored in the project if you go to the Navigator worksheet, right click on the Project, and select "Properties...". Then click the "Details" tab and you should see all your user attributes (including the "UA::" prefix).

ProjProperties.png

Perhaps you are expecting the new User Attributes to appear in the Documentation worksheet?  That will not happen unless the attributes have already been created there.  It is possible for User Attributes to exist in the project, but not be visible in the Documentation worksheet.

 

Re: How can I access the "User Attributes" through VB?

If it helps, there is some information here about creating User Attributes: LMS Test.Lab Documentation tab and User Attributes

 

The user attributes, once created, should be stored in a file called "UserAttributes.txt" in the directory "C:\LMS\UserConfiguration\login\LMS Test.Lab 16A\Configuration".  

Re: How can I access the "User Attributes" through VB?


 

Perhaps you are expecting the new User Attributes to appear in the Documentation worksheet?  That will not happen unless the attributes have already been created there.  It is possible for User Attributes to exist in the project, but not be visible in the Documentation worksheet.

 


Oh, I did not know that, I thought the project attributes would only exist if they were available/visible in the Documentation worksheet, that's very interesting.

I was hoping the newly created attributes would appear in the documentation worksheet, although I was investigating the members of the IDatabase and it doesn't seem to give me this option. Still, this is just a little inconvenience, not primordial to the workflow I had in mind.

T
he rest of the code will sure help us a lot, thank you for your help! It has been some time since I last wrote a VBA code, so I really appreciate that you took the time to write a sample code.

Re: How can I access the "User Attributes" through VB?

If you want your user attributes to always appear in the Documentation worksheet, the best way to do it is to create a project template that includes those attributes.  That way, whenever a user creates a new project he can use that template (by clicking "File / New...") and the attributes will already be there.

To create such a project template:

  • start with an empty project
  • add the attributes you want to the Documentation worksheet
  • add any other settings you may want to be in the project
  • click "File / Save As Template..."

Another approach is to use "Tools / Load Attributes List" to load a previously saved list of attributes.  Note that the Documentation worksheet has to be active to do this.  (there is also a VB command to do this).