views:

122

answers:

1

I have a set of VBA classes in an MS Access database. I have an xml string with data I want to create new classes with.

Other than setting each property individually, is there an easy way to deserialize the XML into my object?

I've seen the code using the TypeLib library

Public Sub ISerializable_Deserialize(xml As IXMLDOMNode)

  Dim tTLI As TLIApplication
  Dim tInvoke As InvokeKinds
  Dim tName As String
  Dim tMem As MemberInfo

  tInvoke = VbLet

  For Each tMem In TLI.ClassInfoFromObject(Me).Members

     tName = LCase(tMem.Name)

     CallByName Me, tMem.Name, VbLet, xml.Attributes.getNamedItem(tName).Text

  Next tMem
End Sub

but this doesn't seem to work with the standard class modules. I get a 429 error:

ActiveX Component Cannot Be Created

Can anyone else help me out? I'd rather not have to set each propery by hand if I can help it, some of these classes are huge!

+2  A: 

You never instance tTLI in that code & and later refer to it as just TLI so it wont work, the 429 error may be because the TypeInfo library isn't registered, did you add it as a reference?

If you did the following will work:

Dim TLI As TLIApplication
Dim II As InterfaceInfo
Dim MI As MemberInfo

Set TLI = New TLIApplication
Set II = TLI.InterfaceInfoFromObject(Me)

For Each MI In II.Members
    If MI.InvokeKind = InvokeKinds.INVOKE_PROPERTYPUT Then
        Debug.Print MI.Name
        TLI.InvokeHook Me, MI.Name, InvokeKinds.INVOKE_PROPERTYPUT, "PROPVALUE"
    End If
Next

You can replace InvokeHook with CallByName if you wish.

Alex K.
Private classes in an Access database aren't included in any public type library, so I don't think this is ever going to work, even with the typos fixed as you have suggested. I don't think `InterfaceInfoFromObject(Me)` can succeed because the relevant interfaces for `Me` aren't public.
MarkJ
InterfaceInfoFromObject works via IDispatch::GetTypeInfo() which works in Access for me .. as to how .. who knows?
Alex K.
Thanks Alex.It's still not working, it's failing on the Set TLI = New TLIApplicationline now with the same error. I've referenced the typelib reference, (tlbinf32.dll) is this the right one?Ben.
oharab
Bah! looks like tlbinf32 wasn't registered. A simple regsvr32 "c:\WINDOWS\system32\TLBINF32.DLL"and I'm in businesss.Thank you very much.Ben
oharab
Wow! OK, I was wrong. +1 Alex, that's a great technique.
MarkJ