Trouble passing user-defined type to form in OpenArgs

M

Max Moor

Hi All,

I have a form that is opened by another form, to display some extended
data. I need to pass a user-defined structure and an SQL string to the
form being opened.

In a VBA module (not part of a form or report), I have my structure
defined as:

Public Type MyType
strOne As String
strTwo As String
<etc, etc>
End Type



In the "master" form, I have the code:

Dim typMyRecord As MyType
Dim strSQL As String
Dim avarOpenArgs(2) As Variant
Dim strOpenArgs As String

<some code that inits typMyRecord and strSQL>

avarOpenArgs(0) = typMyRecord
avarOpenArgs(1) = strSQL
strOpenArgs = Join(avarOpenArgs, "\")

DoCmd.OpenForm "frmSlaveForm", acNormal, , , , acDialog, strOpenArgs



Finally, in the"slave" form, I have:


Private gtypMyRecord As MyType
Private gstrSQL As String


Private Sub Form_Open(Cancel As Integer)

Dim avarOpenArgs As Variant
Dim strOpenArgs As String

If Not IsNull(Me.OpenArgs) Then
strOpenArgs = Me.OpenArgs
avarOpenArgs = Split(strOpenArgs, "\")
gtypMyRecord = avarOpenArgs(0)
gstrSQL = avarOpenArgs(1)
End If

End Sub



I've used this method to pass multiple string before, but when I
try it with my structure, I get a compile error saying:

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

I thought I understood this error, but I thought I did have the
type defined in a public module. Of course, regardless of what I think,
I still get the error. Can anyone help me get my structure and string
passed to my form?

Thanks, Max
 
A

Allen Browne

Max, I think OpenArgs can only be a string, not an array of strings or
user-defined type.

Could you achieve the same result by concatenating the values together into
a delimited string, e.g. "item1;item2;item3;...", and then in the form's
Open event, use Split() to parse them into a variant array?
 
M

Max Moor

Max, I think OpenArgs can only be a string, not an array of strings or
user-defined type.

Could you achieve the same result by concatenating the values together
into a delimited string, e.g. "item1;item2;item3;...", and then in the
form's Open event, use Split() to parse them into a variant array?

Hi Allen,
I may end up doing that. I was just hoping I could get it to work
with the typedef. It seems a lot cleaner. Mostly, I just didn't
understand why I'd get the coersion error when I had defined the type in a
public fashion.

- Max
 

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