Graham Charles
Posts: 13
Joined: 2005-07-29
|
I'm having trouble with my add-in keeping Access from terminating successfully if I access a Property object. I've encapsulated the entire call in a routine so I can ensure that the DAO.Property object gets successfully released (using Marshal.ReleaseCOMObject), but it still holds a reference to something and holds Access open. If I call the routine below (no matter the value of bCreate), Access will not quit.
Any advice?
g.
Public Function GetPropertyBoolean( _
ByVal theDB As dao.Database, _
ByVal PropertyName As String, _
Optional ByVal bCreate As Boolean = False) As Boolean
Dim DataType As dao.DataTypeEnum = dao.DataTypeEnum.dbBoolean
Dim daProp As dao.Property
Dim bReturn As Boolean
Try
daProp = theDB.Properties.Item(PropertyName)
Catch ex As System.Exception
If bCreate Then
daProp = theDB.CreateProperty(PropertyName, DataType, False)
theDB.Properties.Append(daProp)
theDB.Properties.Refresh()
End If
End Try
If Not daProp Is Nothing Then
bReturn = CBool(daProp.Value)
End If
Marshal.ReleaseComObject(daProp)
daProp = Nothing
Return bReturn
End Function
|
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Hi Graham.
Try to change the code as shown below:
Public Function GetPropertyBoolean( _
ByVal theDB As dao.Database, _
ByVal PropertyName As String, _
Optional ByVal bCreate As Boolean = False) As Boolean
Dim DataType As dao.DataTypeEnum = dao.DataTypeEnum.dbBoolean
Dim daProp As dao.Property = Nothing
Dim daProps As dao.Properties = Nothing
Dim bReturn As Boolean
Try
Try
Try
daProps = theDB.Properties
daProp = daProps.Item(PropertyName)
Finally
If Not (daProps Is Nothing) Then
Marshal.ReleaseComObject(daProps)
daProps = Nothing
End If
End Try
Catch ex As System.Exception
If bCreate Then
Try
daProp = theDB.CreateProperty(PropertyName, DataType, False)
daProps = theDB.Properties
daProps.Append(daProp)
daProps.Refresh()
Finally
If Not (daProps Is Nothing) Then
Marshal.ReleaseComObject(daProps)
daProps = Nothing
End If
End Try
End If
End Try
If Not daProp Is Nothing Then
bReturn = CBool(daProp.Value)
End If
Finally
If Not (daProps Is Nothing) Then
Marshal.ReleaseComObject(daProp)
daProp = Nothing
End If
End Try
Return bReturn
End Function
Let me know if it doesn't help. |
|
Graham Charles
Posts: 13
Joined: 2005-07-29
|
Nope, using that code, Access still won't quit. However, I've gotten around the problem by storing the data I need in a hidden table in the database; that's working fine.
This comes up for me all the time --- Is there a way to tell what objects are keeping the Office application open? Some way to see the object counters? The Marshal object has lots of methods --- isn't there something there we could use?
g.
|
|
Graham Charles
Posts: 13
Joined: 2005-07-29
|
Here's another line that will force Access to stay open:
If (Not (AccessHost.CurrentDb Is Nothing)) Then
...
Any thoughts on that one? No other way to test if a database is open...
g.
|
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Graham, I tested the code below in the AccessIdle event handler.
It works fine for me. Do you use Access 2003?
Dim db As dao.Database = Nothing
Try
db = AccessHost.CurrentDb
If Not (db Is Nothing) Then
MessageBox.Show(db.Name())
End If
Finally
If Not (db Is Nothing) Then
Marshal.ReleaseComObject(db)
End If
End Try
|
|