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
Layne Olivo
Unknown said:The Debug.Assert lines are commented out because they didn't return anything
Then there's no need to comment them out!
Debug.Assert only asserts when some assumed condition goes wrong. It's a programmer's tool.
Regards, Jon Summers LA Solutions
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Answer Verified By: Layne
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" linethe Debug.Assert lines are commented out because they didn't return anything.
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
Hi Bruce,
Unknown said:I believe you must have an active database connection for things like this to work
I thought the same, but suprisingly the connected database is not required. From MicroStation VBA help file: In fact, these operations are function even MicroStation does not have a relational database attached.
At least it makes the situation easier as we can expect the problem doesn't exist because of not connected DB ;-)
Regards,