• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1202
  • Last Modified:

User-Defined type error

Hi,

I'm writing some VBA code that utilizes UDTs and passes them as arguments to other sub procedures. I don't have any class modules and am containing all the code within the one standard module. Problems  arise on a couple of fronts: 1 - when I try to fill an array of UDTs, I get the 'Only user-defined types defined in public object modules can be coerced....' compile error.
2 - when I try to pass an array of the UDT to a sub procedure ,the compiler complains that there's a type mismatch and doesn't seem to recognise the the array I'm passing.

The relevant code is as follows:

Public Type Details
    A As Variant
    ......
    x As Variant
   
End Type

Public Type Multiple
    CID As Variant
    code As Variant
End Type

....
Dim Dets, TwWk1(), TwWk2(), TwWk3() As Details
Dim Ii, Jj, Kk, Ll, Zz, ji, ki, li, Wk1, Wk2, Wk3 As Integer
Dim AllList(), FstArr(), SecArr(), ThrArr(), mult As Multiple


Select Case (Ii)    
        Case 1
            Wk1 = Wk1 + 1
            ReDim Preserve TwWk1(Wk1)
            TwWk1(Wk1 - 1) = Dets  ------ this assignment throws the 'Only user-defined...' error
        Case 2
            Wk2 = Wk2 + 1
            ReDim Preserve TwWk2(Wk2)
            TwWk2(Wk2 - 1) = Dets
        Case 3
            Wk3 = Wk3 + 1
            ReDim Preserve TwWk3(Wk3)
            TwWk3(Wk3 - 1) = Dets            
    End Select

Call outputdets(TwWk1(), Ii)--------this is where the type mismatch error occurs.

Sub outputdets(DetArr() As Details, Ii As Integer)
....
end sub


I've written similar code in other VBA modules without any problems, are there any specific scenarios where these errors are raised in bug free code or am I just missing something blindingly obvious? I must mention that the code also contains a couple of declared functions that reference the shell32.dll, could this have anything to do with it at all?

500 big ones to any helpful suggestions....
0
smydo
Asked:
smydo
  • 3
  • 2
1 Solution
 
Ryan ChongCommented:
try define your User-Defined Type in a module instead?

create a new module, then put:

Public Type Details
    A As Variant
    ......
    x As Variant
   
End Type

Public Type Multiple
    CID As Variant
    code As Variant
End Type
0
 
smydoAuthor Commented:
I didn't see how that would make a difference and it didn't, as it turned out. I know there would be an argument for making the UDT a class and working at from that angle but I just don't see how this particular method won't work - it's very frustrating.
0
 
Arthur_WoodCommented:
the error message : "'Only user-defined types defined in public object modules can be coerced" is telling it like it is.  You MUST define a Class module in order to do what you are trying to do.  That is what the phrase "PUBLIC OBJECT MODULE" means.

AW
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
smydoAuthor Commented:
Arthur, that error message doesn't appear in code I had written previously using exactly the same techniqu for filling an array of UDTs.This works perfectly, so I don't see any reason why it shouldn't work now, here it is:


Public Type Details
    A As Variant
    ...
    U As Variant
   
End Type

Dim RecentArray() As Details
Dim OldArray() As Details


If ActiveSheet.Name = "Uk car" Then
            OldArray(I) = Dets
        Else
            RecentArray(I) = Dets
        End If
0
 
Arthur_WoodCommented:
ok, now I see where the error lies:

These lines are NOT doing what you think they are:

Dim Dets, TwWk1(), TwWk2(), TwWk3() As Details
Dim Ii, Jj, Kk, Ll, Zz, ji, ki, li, Wk1, Wk2, Wk3 As Integer
Dim AllList(), FstArr(), SecArr(), ThrArr(), mult As Multiple


in each line, only the LAST variable is being declared as you think it is, the others are ALL being declared as VARIANT.  VB and VBA do not support multiple declaration statements.  These lines MUST read, either as:

Dim Dets  As Details, TwWk1() As Details, TwWk2() As Details, TwWk3() As Details
Dim Ii As Integer, Jj As Integer, Kk As Integer, Ll As Integer, Zz As Integer, ji As Integer, ki As Integer, li As Integer, Wk1 As Integer, Wk2 As Integer, Wk3 As Integer
Dim AllList() As Multiple, FstArr() As Multiple, SecArr() As Multiple, ThrArr() As Multiple, mult As Multiple

or as:

Dim Dets  As Details
Dim TwWk1() As Details
Dim TwWk2() As Details
Dim TwWk3() As Details
Dim Ii As Integer
Dim Jj As Integer
Dim Kk As Integer
Dim Ll As Integer
Dim Zz As Integer
Dim ji As Integer
Dim ki As Integer
Dim li As Integer
Dim Wk1 As Integer
Dim Wk2 As Integer
Dim Wk3 As Integer
Dim AllList() As Multiple
Dim FstArr() As Multiple
Dim SecArr() As Multiple
Dim ThrArr() As Multiple
Dim mult As Multiple

AW



0
 
smydoAuthor Commented:
Excellent Arthur, much obliged to you sir - still thinking I was programming in C++....All the best mate.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now