0

Compile Error:

Compile Error: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to a late-bound functions.

I'm new to VBA and I was tasked with debugging some code for a custom screen in Dynamics SL. The first thing I did was to see if it compiled and I got the above message.

When I read the built-in help reference I found the following for the above error:

You attempted to use a public user defined type as a parameter or return type for a public procedure of a class module, or as a field of a public user defined type. Only public user defined types that are defined in a public object module can be used in this manner.

I also went through these similar questions:

They have the same error but I don't see a collection object that the above two questions focused on.

If you may have any idea what may be causing this error please don't hesitate to suggest it.

Code:

Private Sub cpjt_entity_Chk(ChkStrg As String, retval As Integer)
Dim ldDate As Sdate
Dim xStrDailyPost As Sdate

ldDate.val = GetObjectValue("cpe_date")
'xStrDailyPost = DateToStr(ldDate)
'Call MsgBox("Daily Post Date: " & xStrDailyPost, vbOKOnly, "TEST")
serr1 = SetObjectValue("cld_id08", xStrDailyPost) <- Error highlights "xStrDailyPost"

End Sub

Definition for SetObjectValue:

Declare Function SetObjectValue Lib "swimapi.dll" Alias "VBA_SetObjectValue" (ByVal ctlname$, newval As Variant) As Integer

Thank you in advance!

Jonathan Porter
  • 1,365
  • 7
  • 34
  • 62
  • The error is not caused by collections, but may happen while using them because they accept a `Variant` like the error description says. What is `SetObjectValue`, how is its second parameter declared, and where is `Sdate` declared? – GSerg Sep 14 '17 at 20:44
  • Sdate appears to be a user defined type. where is Sdate declared? – Sorceri Sep 14 '17 at 20:48
  • @GSerg, That's a good question. I'm currently looking in a screen object and it's not declared in here. I also looked in the modules related to this screen and no mention of this function in there either. I guess something got lost or deleted. Would it normally be in this screen or a Module? – Jonathan Porter Sep 14 '17 at 20:50
  • 1
    Right click `Sdate` or `SetObjectValue` and select `Definition`. – GSerg Sep 14 '17 at 20:53
  • 1
    @JonathanPorter Sounds like you are missing the class module for Sdate – Sorceri Sep 14 '17 at 20:54
  • @GSerg, Thank you. I found the following for SetObjectValue: Declare Function SetObjectValue Lib "swimapi.dll" Alias "VBA_SetObjectValue" (ByVal ctlname$, newval As Variant) As Integer – Jonathan Porter Sep 14 '17 at 20:56
  • @Sorceri, when I right click on Sdate and click on definition as GSerg suggested I see the error: "Identifier under cursor is not recognized" Does this confirm/contradict your belief that this is related to a missing class module? – Jonathan Porter Sep 14 '17 at 20:58
  • @JonathanPorter yes, there is no class Sdate. You will need to recreate it or use a different type. – Sorceri Sep 14 '17 at 21:00
  • 2
    So you have an external function that accepts a `Variant`, and you are trying to pass a (presumably) used defined type to it. You cannot do that like the error description says. As for not found definition for `Sdate`, it may or may not mean you are missing a reference (`Definition` is not always perfect). Press F2 and search for `Sdate` there to be sure. It might be that you are missing a reference to a library that contains definition for `Sdate`, then you will be able to run that code. – GSerg Sep 14 '17 at 21:01
  • Thank you both for your help! I'll update my question if I can resolve this. – Jonathan Porter Sep 14 '17 at 21:08

1 Answers1

0

You are probably working with code that was originally written with the Dynamics SL (actually it was Solomon IV at the time) Basic Script Language (BSL) macro language instead of VBA.

Regardless... the fix is, pass results of the "val" method of your xStrDailyPost instance of SDate. SO the code should look like:

serr1 = SetObjectValue("cld_id08", xStrDailyPost.val)

I've not actually tested this but I'm pretty sure this will address your issue.

If you want a little more background, "Sdate" is really just a very thin wrapper of an integer (actually I think it's a short, but I've never found I really needed to know for sure). the "Val" method returns the underlying integer in the SDate variable.

Tom Malia
  • 31
  • 3