[VBA]: Cannot add database link to element

Hi

I am attempting to add a Database Link to an element in Microstation. In this Database Link I store an Mslink for later retrieval. However, my attempt seems to be unsuccessful. This puzzles me, as it worked a few weeks ago, and I have not changed my code at all.

The following code snippet shows how I am adding the database link:

Dim dbLink as DatabaseLink
Dim mslinkTrack as Long

(...)

Element.RemoveAllDatabaseLinks
Set dbLink = CreateDatabaseLink(mslinkTrack, 0, msdDatabaseLinkageOledb, True, 0)
Element.AddDatabaseLink dbLink

, where Element is the graphical element in my ILocateCommandEvents routine.

The code is executed without any errors. When I then select the element and open the Info box, there is no OLEDB linkage shown. So it is clear, that the database link has in fact not been added to the element.

I am really desperate for a solution, as the database link is essential for my other functions to work.

Any help is greatly appreciated :)

  • Unknown said:
    Element.AddDatabaseLink dbLink

    Did you remember to Rewrite the element after modifying it?

     
    Regards, Jon Summers
    LA Solutions

  • Hi Jon.

    I have already tried the Rewrite call after adding the database link. This didn't help, thus I removed that particular line of code again.
    I still cannot comprehend why it suddenly does not work. It worked fine a few weeks ago, and I haven't changed anything in this part of my code.
  • Unknown said:
    I have already tried the Rewrite call after adding the database link. This didn't help, thus I removed that particular line of code again

    You must Rewrite an element after modifying it, otherwise the change is only temporary.

    Unknown said:
    I haven't changed anything...

    Programmers (myself included) often say that.  Then I discover that there's another Jon Summers who edits my code while I'm asleep, and makes all sorts of mistakes.

     
    Regards, Jon Summers
    LA Solutions

  • Well, this time I can say for sure, that I have not changed anything (and that it has worked without the Rewrite), because one of my colleagues has this version of the code, and it worked for him also, a few weeks ago, but does not work now.

    He is not developing the code (he doesn't know how to, and doesn't know the password), and I have not sent him anything since (we have checked the date of the .MVBA that he is running). That is exactly why I am so puzzled.

    Is there any chance, that a recent update could have resulted in the need for a slightly different syntax or structure? I experienced this last time Microsoft Access was updated. Suddenly, my SQL queries wouldn't execute with the date format dd/mm/yyyy that I have ALWAYS used. Now it requires dd.mm.yyyy. And no, I have not changed language or format settings on my PC ;)

  • Just shooting something out here, but have you checked to see if there are any system restore points between the time this worked and the time it didn't? If there are, there could have been a system update (or something similar to a sub-system, group policy, etc.) along the way that changed your environment, which could have some impact regarding this.

      

  • Hi S?ren,

    I tested your code in the lastest version of MicroStation V8i, as Jon mentioned, without .Rewrite method call, the database linkage can't be written into model. When I add the .Rewrite method, your code can work fine.

    BTW, Element is a keyword in MVBA, it isn't a good idea to use it as an object name.

    Sub DbTest()
        Dim dbLink As DatabaseLink
        Dim mslinkTrack As Long
        Dim oElem As Element
        
        mslinkTrack = 100
        Set oElem = ActiveModelReference.GetElementByID(DLongFromLong(22289))
        oElem.RemoveAllDatabaseLinks
        Set dbLink = CreateDatabaseLink(mslinkTrack, 0, msdDatabaseLinkageOleDb, True, 0)
        oElem.AddDatabaseLink dbLink
        oElem.Rewrite
    End Sub

    HTH, YongAn



    Answer Verified By: Phil Chouinard 

  • Hi Yongan,

    I have now added the Rewrite once again, and things seem to work now. This is really strange - maybe I didn't do the Rewrite immediately after adding the databaselink, until now.

    Regarding the Element it is not an object name - it's the element pointed out in an ILocateCommandEvent.. I saw no reason to declare an object of type Element and assign my element to this object, when working in this isolated ILocateCommandEvent environment :) .

    So - everything seems to work now. Thank you all, for your efforts.