CodeProject.Allforms("FormName").Properties raise an error

Josef P.

Well-known member
Local time
Today, 23:40
Joined
Feb 2, 2023
Messages
993
Hi!

In an add-in (https://github.com/AccessCodeLib/better-access-charts/tree/main/access-add-in ... BetterAccessCharts.accda ) I use CodeProject.AllForms to get the AccessObject of a form in the add-in. The AccessObject is captured correctly.
But when I try to read the properties from it, I get the error -214746725 (Method 'Properties' of object 'AccessObject' failed).
The problem occurs only when the add-in file is opened as an add-in (i.e. when CurrentDb <> CodeDb).
If the add-in file is opened directly, the code runs without errors.

What could be the cause of the error?
Are the properties protected from access from "outside" (not CurrentProject)?

Code from BacChartConfigurationTools.cls
Code:
Public Function GetFormProperty(ByVal FormName As String, ByVal PropertyName As String, _
                  Optional UseCodeProject As Boolean = False) As BAC_Properties

    Dim ao As AccessObject
    Dim Prop As AccessObjectProperty
    Dim PropJson As String
    Dim ChartProperties As BAC_Properties

    If UseCodeProject Then ' special: eg start demo form in add-in
        Set ao = CodeProject.AllForms(FormName)
    Else                   ' default ... use CurrentProject
        Set ao = CurrentProject.AllForms(FormName)
    End If

    For Each Prop In ao.Properties ' <-- err raised:
                                   '    Err.Number = -2147467259,
                                   '    Err.Description = "Method 'Properties' of object 'AccessObject' failed"
        If Prop.Name = PropertyName Then
            PropJson = Trim(Nz(Prop.Value, vbNullString))
            Exit For
        End If
    Next

    If Len(PropJson) = 0 Then
        Set GetFormProperty = Nothing
        Exit Function
    End If

    With New BAC_JsonConverter
        Set ChartProperties = .Json2Properties(PropJson)
    End With

    Set GetFormProperty = ChartProperties

End Function

/edit:
ao.Isloaded works.
locals.png


This is a special case, of course, because you rarely access the properties of an AccessObject within an add-in. ;)

Josef
 
Last edited:
is the property not available in:
forms!fMyForm.[objectProperty]
or
forms!fMyForm.controlname.[objectProperty]
 
It is about the properties of the AccessObject reference.
Code:
CodeProject.AllForms(FormName).Properties
 
Last edited:
An example to show the error:
.. just extract files into a folder and open Demo_Start_this.accdb. DemoAddIn.accda does not need to be in the add-in directory. (I open the add-in directly via Application.Run in this example.)
 

Attachments

Last edited:
What could be the cause of the error?

The properties might not exist if you don't run the add-in correctly. "Properties" can be both intrinsic and externally defined. Generically speaking, things like a control's .Top and .Left values are properties but would not show up under the word "Properties" in the tree diagram like you showed us in post #1. On the other hand, if you add non-standard properties to an object, the list of such properties WOULD be displayed in the same way that an Item is listed - as a numbered list with a name and value for each number, and you would access the value using .Property( X ) where ( X ) represents the index of the created property.

So the question is, which kind of property were you seeking?

EDITED to remove the incorrect interpretation of ( X ) as some kind of icon.
 
Last edited:
Briefly about the code content: I store a string as an entry in the AccessObject.Properties. The access works as long as CodeDb = CurrentDb.
But if I then open the add-in file as an add-in (i.e. CodeDb <> CurrentDb), accessing the property 'Properties' triggers a runtime error.
The problem is not the existence of a property, but the access to the properties collection.

Sometimes this error message is in the Locals window: "The expression you entered has an invalid reference to the Parent property"
locals2.png


I assume that the problem is not fixable.
Is also not tragic, since I can alternatively save the chart settings from the add-in also in a table (as already implemented).
But I am always interested in a cause of the error.

BTW:
> if you don't run the add-in correctly
Application.Run CurrentProject.Path & "\DemoAddIn.OpenAddInTestForm" is a possible start of an add-in. Otherwise this option would not exist. ;) However, the problem also occurs when the add-in is opened from the add-in menu.
 
Last edited:
maybe use Access.Form since you are referring to a Form object:
Code:
Private Sub cmdTest_Click()
    
    'Dim ao As AccessObject
    Dim frm As Access.Form
    'Dim props As AccessObjectProperties
    Dim props As Properties
    Dim PropCount As Long
    
    'Set ao = CodeProject.AllForms(Me.Name)
    Set frm = Forms(Me.Name)
    'Set props = ao.Properties ' <--- err raised
    Set props = frm.Properties ' <--- err raised
    PropCount = props.Count
    
    MsgBox "Count: " & PropCount
    
    
End Sub
 
@Josef P., just for the records: I can fully reproduce the issue and understand the context it happens in.
I think, this is a bug in Access and I reported it. Still, I wouldn't hold my breath for any fix, as the scenario is rarely used and will hardly trigger much of a response to the bug report.
 
maybe use Access.Form since you are referring to a Form object
An AccessObject reference is quite different from a Form reference.
I use the AccessObject (from AllForms) because it allows me to store values permanently at runtime.

I can fully reproduce the issue and understand the context it happens in.
Do you also get this message in Locals window: "The expression you entered has an invalid reference to the Parent property"?

Still, I wouldn't hold my breath for any fix, as the scenario is rarely used and will hardly trigger much of a response to the bug report.
I have already used the workaround in the add-in: save the demo settings in a table.
When using the add-in in a 'real' application, the code runs because there is no problem with CurrentProject.AllForms(...).Properties.

As already written, the error does not bother me much, but I am interested in the cause of the error ;)
 
Last edited:
Can you please elaborate what the difference between running an add-in correctly vs. incorrectly is?

Not exactly, because I don't use add-ins for Access. (I have used them for other products.) However, the OP said this:

The problem occurs only when the add-in file is opened as an add-in (i.e. when CurrentDb <> CodeDb).
If the add-in file is opened directly, the code runs without errors.

Whatever is the difference between these two approaches is what I meant.
 

Users who are viewing this thread

Back
Top Bottom