UDT's in Com

W

Wanderer

I am writing a control (ocx) in VB6 to use in Excel VBA but I get Type Mismatch errors regarding my
UserDefinedType when using the control in VBA. For simplicity assume this code in the control...

'''''''''''''''''''''''''''''''BEGIN OCX CODE'''''''''''''''''''''''''''''

Public Type ToolStructure
Index as Integer
Diameter as Single
Description as String
End Type

Public Function GetToolInfo(Indx as Integer) as ToolStructure
If Indx < 1 then
GetToolInfo.Index = -1 'ACTS AS A 'NOT FOUND' FLAG
Else
GetToolInfo.Integer = Indx
GetToolInfo.Diameter = .5 'HARDCODED DATA FOR TESTING
GetToolInfo.Description = "Test"
End If
End Function

''''''''''''''''''''''''''''''''''''END OCX CODE'''''''''''''''''''''''''''''''''''''

Now I compile this and I can use it in VB apps with no problem. Sample code in the application would
look like this.

''''''''''''''''''''''''''''''''''BEGIN APPLICATION CODE''''''''''''''''''''''''''

Private Sub cmdTest_Click()
Dim X as TestOCX.ToolStructure
X = TestOCX1.GetToolInfo(3) 'HARDCODED DATA FOR TESTING
MsgBox X.Description
End Sub

''''''''''''''''''''''''''''''''''''END APPLICATION CODE'''''''''''''''''''''''''''''

This same code in VBA gives a "Type Mismatch" error on this line
X = TestOCX.GetToolInfo(3)

My actual UDT has many elements. I would hate to have to use a long list of

Property Get Diameter(ByVal Indx as Integer) as Single
Property Get Flutes(ByVal Indx as Integer) as Integer
Property Get Description(ByVal Indx as Integer) as String
etc etc

as it would be quite cludgy for the developer (not to mention decreased performance accessing
everything one by one). Can VBA just not handle UDT's or am I doing something wrong?

Thanks,
Robb
 
K

Karl E. Peterson

Wanderer said:
My actual UDT has many elements. I would hate to have to use a long
list of

Property Get Diameter(ByVal Indx as Integer) as Single
Property Get Flutes(ByVal Indx as Integer) as Integer
Property Get Description(ByVal Indx as Integer) as String
etc etc

as it would be quite cludgy for the developer (not to mention
decreased performance accessing everything one by one). Can VBA just
not handle UDT's or am I doing something wrong?

UDTs are not a known datatype in the COM realm. The easiest way out of
this, albeit rather clumsy, is to create a class that exposes each UDT
element as a public variable, then use this secondary class as the public
type of the exposed property in the first class. This, of course, adds the
capability to validate element assignments, which once you start doing
you'll never stop. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top