retrieving DAO.Property objects

Add-in Express™ Support Service
That's what is more important than anything else

retrieving DAO.Property objects
 
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


Posted 25 Jan, 2006 18:10:52 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
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.
Posted 26 Jan, 2006 10:55:24 Top
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.

Posted 26 Jan, 2006 14:29:52 Top
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.

Posted 26 Jan, 2006 14:47:31 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
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
Posted 27 Jan, 2006 07:36:38 Top