Linking Microstation Elements to a Database Records

I`m trying to follow the book Learning MS VBA, I`m currently up to page 564,

Linking Microstation Elements to Database Records.

I`m not sure if the book is missing something

i.e. certain steps to be followed first or based on what has already been learnt I should automatically know where I have gone wrong?

Anyway I get the following error.

Compile error:

Expected user-defined type, not project

After running the following code

Function CreateDatabaseLink(Mslink As Long, _

Entity As Long, _
LinkType As MsdDatabaseLinkage, _
IsInformation As Boolean, _
DisplayableAttributeType As Long) As DatabaseLink

End Function

Sub DatabaseLinkA()

Dim myElem As Element
Dim myLink As DatabaseLink
Set myElem = CommandState.GetLocatedElement(True)
Set myLink = CreateDatabaseLink(1, 1, msdDatabaseLinkageOleDb, _
True, 0)
myElem.AddDatabaseLink myLink
myElem.Rewrite
End Sub

I know a similar problem has been answered previously regarding page 564 (Learning MS VBA) however I was unable to relate it to my error message.

Any ideas?

  • Hi,

    please, when posting any code, always use Syntaxhighlighter tool (yellow pencil icon) with a properly set language (VB in your case) and don't post the code as a normal text. It's ugly and hard-to-read. The highlighter will ensure the code is displayed and colorized properly.

    There are important information missing in your post:

    • What MicroStation version do you use?
    • On what line the error message is reported?

    Unknown said:
    however I was unable to relate it to my error message.

    Did you try to search Internet for this error? There are plenty of discussions about the same problem including possible reasons and solutions.

    Unknown said:
    Any ideas?

    You did not provide your mvba or at least screen capture how your project looks like and how is organized. Together with missing information on what line the error appeared, it cannot be answered fully.

    I guess the problem is that you named your project or module the same as some data type. E.g. if your project name is "DatabaseLink", VBA engine cannot distinguish between DatabaseLink as a data type and DatabaseLink as module name, because both can be used in the code, but in different situations.

    With regards,

      Jan

  • Unknown said:
    Linking Microstation Elements to Database Records

    This article about MicroStation and external databases may help.

     
    Regards, Jon Summers
    LA Solutions

  • Thank you, this article helped me understand MSCATALOG and MSLINK.

    Before my above error message, which I only assume now that I didn't follow the steps to the letter in chapter 27 of the book (I`m thinking I didn't add a reference file!) anyway moving on, what I was trying to achieve was to select an element in microstation and create a database link in which I achieved by using the following code.

    I finally found the Syntax highlighter tool in the advanced reply editor thanks for the tip!

    also

    My ustation version is  08.11.09.459

    Sub DatabaseLinkA()
        Dim myElem As Element
        Dim myLink As DatabaseLink
        Set myElem = CommandState.GetLocatedElement(True)
        Set myLink = CreateDatabaseLink(1, 1, msdDatabaseLinkageOleDb, _
                        True, 0)
        myElem.AddDatabaseLink myLink
        myElem.Rewrite
    End Sub




    I noticed in the code the fields for MSLINK=1 and Entity=1, shown below



    Set myLink = CreateDatabaseLink(1, 1, msdDatabaseLinkageOleDb, _ True, 0)





    If I wanted to add another link (in this case MSLINK=2) to an element, I found that by changing the code to




     Set myLink = CreateDatabaseLink(2, 1, msdDatabaseLinkageOleDb, _
                        True, 0)




    My question is how do I select and element and assign a different (unique) MSLINK to it, without having to change the code each time?



  • Unknown said:
    My question is how do I select and element and assign a different (unique) MSLINK to it, without having to change the code each time?

    Have you succeeded in connecting your app. to an external database?

    Your DGN elements have an ID (MSLINK) that identifies the row in a table in your database.  Query the database table for the highest MSLINK value, then increment that and create the new element linkage.

     
    Regards, Jon Summers
    LA Solutions

  • Yes I have been able to connect my app. to my (access) database, what you are saying above makes sense, I guess I just need to read up on how I achieve the outcome. I`m very new to learning how to code and using VBA but thus far thoroughly enjoying learning it.
  • Unknown said:
    I just need to read up on how I achieve the outcome

    First, learn about Microsoft ADO

    1. Create a test DB that you can discard if you mess things up
    2. Use Excel as a VBA programming tool
    3. Write some VBA code that reads the DB
    4. Write some VBA code that updates the DB

    If that all works, copy your VBA code to MicroStation VBA and test it there.

    I recommend that you start with Excel because then you won't be sidetracked by irrelevant MicroStation issues.  Also, there are heaps of Excel VBA examples on the web that include DB examples.

    To obtain the largest MSLINK from a DB table you need to write an SQL statement like the following and put it into your ADO code...

    SELECT mslink FROM tablename ORDER BY mslink ASC

    Then get the first record and increment.

     
    Regards, Jon Summers
    LA Solutions