Book a Demo

Author Topic: Add TaggedValues to an element in VBA  (Read 4611 times)

elgringogordo

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Add TaggedValues to an element in VBA
« on: October 22, 2018, 08:29:43 pm »
 I'm trying to write a VBA import using code written by Geert Bellekens.  Most of it is pretty much done for me so is very easy so far.  I'm just trying to expand it a little now and I'm struggling.  So far I've just dumped new Requirement elements into an existing package.  I wanted to add tagged values to these elements and I'm running into problems.

This is my code

    Dim myElement As EA.element
    Dim aTaggedValue As EA.taggedValue

    Set myElement= eaConn.addOrUpdateElement(parentPackage, ELEMENT_TYPE, name, STEREOTYPE, description)
    aTaggedValue = myElement.TaggedValues.AddNew("Test", "String")
                 
    aTaggedValue.Value = "Test"
    aTaggedValue.Update

myElement is created but I get an error on the TaggedValues.AddNew line

Run Time Error '91'
Object variable or With Block not set

Can anyone tell me what I'm doing wrong please ?

Excel vba, EA version 14.

Many thanks.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Add TaggedValues to an element in VBA
« Reply #1 on: October 22, 2018, 08:54:39 pm »
I don't know what eaConn.addOrUpdateElement (probably one of Geert's framework operations) is doing, but most likely it does not return an EA.Element object.

q.

elgringogordo

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: Add TaggedValues to an element in VBA
« Reply #2 on: October 22, 2018, 09:09:02 pm »
I'll do a bit more investigation but it does seem to...

Public Function addOrUpdateElement(parentPackage As EA.package, elementType As String, name As String, STEREOTYPE As String, description As String) As EA.element
    Dim myelement As EA.element
    'try to find existing class with the given name
    'this will only work correctly if there is only one element with the given name, and if it is a class
    Set myelement = getElementByName(parentPackage, name)
    If myelement Is Nothing Then
        'no existing class, create new
        Set myelement = parentPackage.elements.AddNew(name, elementType)
    End If
    'set properties
    myelement.STEREOTYPE = STEREOTYPE
    myelement.Notes = description
    'save class
    myelement.Update
    'refresh elements collection
    parentPackage.elements.Refresh
    'return class
    Set addOrUpdateElement = myelement
End Function

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Add TaggedValues to an element in VBA
« Reply #3 on: October 22, 2018, 09:43:20 pm »
That does not look too bad. Have you checked that the result is something meaningful? Print its object id, guid and name. And look whether it's in the model where you expect it (in the passed package).

q.

elgringogordo

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: Add TaggedValues to an element in VBA
« Reply #4 on: October 22, 2018, 10:46:06 pm »
Apologies this is the first time I've used VBA in about 10 years.  It's a basic syntax error on my part! It needs to be

SET   aTaggedValue = myElement.TaggedValues.AddNew("Test", "String")

Doh!

Apologies and thanks...

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Add TaggedValues to an element in VBA
« Reply #5 on: October 23, 2018, 12:48:40 am »
Geert would have seen that at once. I'm also a part-time VB user. Very low part.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add TaggedValues to an element in VBA
« Reply #6 on: October 23, 2018, 02:03:30 am »
I didn't see it at once, but that is indeed one of the classic gotcha's.
Still gets me once in a while, and I write VBscript code at least once a week.

Geert