V8i VBA - linking element to database

I'm working through the Learning Microstation VBA book.  I'm trying to connect an element to an external database so that I can assign information to it for reporting later.

The code in the text is this: 

Function CreateDatabaseLink(Mslink As Long, Entity As Long, DatabaseType 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

with the exception that I have replaced "LinkType" in the book with "DatabaseType" - although the results are the same either way.  

In both cases, I have an element selected in the drawing, and when I run DatabaseLinkA(), Microstation crashes.

Any idea what's going on?

Layne

Parents
  • Hi Layne,

    Unknown said:
    Any idea what's going on?

    I agree with Jon the most important is to find where exactly MicroStation crashes. The mentioned combination of F8 and OnError is the best what to try.

    There can be more different reasons why the code crashes, because your code is not error-proof at all. At least you have to test what GetLocatedElement returns, if the element is not locked, from a reference file etc.

    With regards,

      Jan

  • Thank you, gentlemen. I'll work that into it and see what shakes out.

    Layne Olivo

  • Public Sub ReportError(ByVal procName As String)
        MsgBox "Error " & CStr(Err.Number) & " : " & Err.Description & vbNewLine & "Was caused by " & Err.Source, vbOK Or vbExclamation, "Error in " & procName
    End Sub
    
    
    Function CreateDatabaseLink(Mslink As Long, _
            Entity As Long, LinkType As MsdDatabaseLinkage, _
            IsInformation As Boolean, DisplayableAttributeType As Long) _
            As DatabaseLink
    End Function
    
    Sub DatabaseLinkA()
        On Error GoTo err_DatabaseLinkA
            Dim myElem As Element
            Dim myLink As DatabaseLink
            Set myElem = CommandState.GetLocatedElement(True)
            'Debug.Assert Not myElem Is Nothing
            Set myLink = CreateDatabaseLink(1, 1, msdDatabaseLinkageOleDb, True, 0)
            'Debug.Assert Not myLink Is DatabaseLink
            myElem.AddDatabaseLink myLink
            myElem.Rewrite
        Exit Sub
    err_DatabaseLinkA:
        ReportError "DatabaseLinkA"
    End Sub


    Ran it through the debug mode and it crashed when it got to the "myElem.AddDatabaseLink myLink" line
    the Debug.Assert lines are commented out because they didn't return anything.

    Layne Olivo

Reply
  • Public Sub ReportError(ByVal procName As String)
        MsgBox "Error " & CStr(Err.Number) & " : " & Err.Description & vbNewLine & "Was caused by " & Err.Source, vbOK Or vbExclamation, "Error in " & procName
    End Sub
    
    
    Function CreateDatabaseLink(Mslink As Long, _
            Entity As Long, LinkType As MsdDatabaseLinkage, _
            IsInformation As Boolean, DisplayableAttributeType As Long) _
            As DatabaseLink
    End Function
    
    Sub DatabaseLinkA()
        On Error GoTo err_DatabaseLinkA
            Dim myElem As Element
            Dim myLink As DatabaseLink
            Set myElem = CommandState.GetLocatedElement(True)
            'Debug.Assert Not myElem Is Nothing
            Set myLink = CreateDatabaseLink(1, 1, msdDatabaseLinkageOleDb, True, 0)
            'Debug.Assert Not myLink Is DatabaseLink
            myElem.AddDatabaseLink myLink
            myElem.Rewrite
        Exit Sub
    err_DatabaseLinkA:
        ReportError "DatabaseLinkA"
    End Sub


    Ran it through the debug mode and it crashed when it got to the "myElem.AddDatabaseLink myLink" line
    the Debug.Assert lines are commented out because they didn't return anything.

    Layne Olivo

Children