Unanswered: Public UDT Form Property - can't pass value
Moderately tricky - I think I know a solution but wouldn't mind some other minds on the case in case I have missed something.
I have created a public property let statement for a form. This property is for a User Defined Type. I have a procedure that opens the form and then attempts to set the UDT. However, the code errors and I get:
"Only user-defined types defined in public object modules can be
coerced to or from a variant or passed to late bound functions."
I have googled but not really come up with much other than it looks like I might want to wrap up my UDT in a class. Or just create a class and drop the UDT. Anyway - here is the code. Any suggestions welcome.
Public Type MyType
MyFirstVal As String
MySecondVal As String
'The declarations and properties of the form.
'There is a String property just to check the problem is UDTs not property syntax
Private mudtMyType As MyType
Private mstrValue As String
Public Property Let ChangeMyType(udtMyType As MyType)
mudtMyType = udtMyType
Public Property Let CheckPropertySyntax(StringInput As String)
mstrValue = StringInput
'Command button on another form to open the UDT form and set the property
'If the line <frm.ChangeMyType = udtMT> is commented out, code works fine and string property is set.
'Uncomment and the code fails with the above error message.
'I've used <Dim frm As Form_UDTForm> to see if I could get round this late binding thing. DoCmd.OpenForm elicits the same error
Dim frm As Form_UDTForm
Private Sub cmdUDT_Click()
On Error GoTo Err_cmdUDT_Click
Dim udtMT As MyType
Const FName As String = "UDTForm"
udtMT.MyFirstVal = "Foo"
udtMT.MySecondVal = "Bar"
Set frm = New Form_UDTForm
frm.Visible = True
frm.CheckPropertySyntax = "FooBar"
' frm.ChangeMyType = udtMT
You've basically already answered your question. You're more or less talking about extending the form class. The simplest solution would be to wrap your type in a class module, and then create objects as needed. You could create a "parent_form" property for your class to assign it to a target form, or perhaps create a public or otherwise persistant instance of your class inside your form declarations.
I was mostly being lazy plus this is the first time I have really found a genuine use for UDTs so I was hoping to be able to use one. Never mind - I'll pop toether a class if there is no alternative. Thanks for the help
no reason you can't use it... you just have to declare it somewhere else
What stops you from declaring your object as private static, then exposing it through public subs?
What's my object? You mean the one I have yet to build to house\ replace my UDT? If so, I guess no reason. EDIT - Oh - you mean the UDT. Hmm - yes, that makes better sense than what I currently have.
Although I am starting to back track and think that I might put the data I intended to pass to the form via an UDT into a table. I've had a lot of success putting application information into tables recently (the idea is to make the application as presentation-layer-independant as possible with a view to a future migration to .NET) so I might follow this route instead. Something to ponder.