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
Unknown said:CreateDatabaseLink(1, 1, msdDatabaseLinkageOleDb, True, 0)
That tells VBA to create an MSLink having link ID 1 and entity ID 1.
The entity ID is the numeric ID of a table in your database MSCatalog table. If that table doesn't exist, you must create it.
Read more about MicroStation VBA and databases.
Regards, Jon Summers LA Solutions
Layne Olivo
Settings->Database->Setup shows that it is connected
I've tried to attach the error log.exception.log
As it happens, I can actually write to the database from VBA...
Unknown said:Dim myElem As Element ... myElem.Rewrite
If you step through your code (F8), do you see where it's failing?
Put some Debug.Assert and Debug.Print statements in there to monitor what's happening. For example...
Debug.Assert Not myElement Is Nothing
Put an error reporter in there to trap run-time problems. For example...
Sub DatabaseLinkA () On Error Goto err_DatabaseLinkA ... Exit Sub err_DatabaseLinkA: ReportError "DatabaseLinkA" End Sub 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
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,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
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,
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.