Passing Array of User Defined Type to Form

D

dunnerca

I have created a user-defined type in a standard module along with an array
of 12 of this user-defined type. I also have a form containing a command
button. When the command button is clicked, I want a procedure run that uses
the array of user-defined type. However, the Click event procedure in the
form doesn't recognize the array. Am I missing something obvious?
 
R

Rick Rothstein \(MVP - VB\)

I think it would be useful for you to post some of your code. Specifically,
the code used to create the UDT and the array of 12 of them, the code the
fills the array, the Click event code for your button and, if the "procedure
run" is different for the Click event procedure, its code also.

Rick
 
D

dunnerca

Thanks Rick. Here's the code you asked for. Thanks. Sorry for the delay in
answering but I went to bed after posting.

a) Setting up the UD type
Public Type team
teamname As String
code As String
manager As String
m_email As String
End Type

Dim pmhc(1 To 12) As team

b) Code To Fill The Array (takes information from a worksheet;

Sub fill_array(pmhc() As team)

' In the worksheet called "Teams", the data is as follows:
' Column A - numbers 1 to 12
' Column B - Name of team
' Column C - Team manager
' Column D - Team code
' Column E - Manager's email

For x = 1 To 12
' Populate the team name fields;

tmp_range = "b" & Trim(Str(x))
pmhc(x).teamname = Worksheets("teams").Range(tmp_range).Value
Next
For x = 1 To 12
' Populate the team code fields;
tmp_range = "d" & Trim(Str(x))
pmhc(x).code = Worksheets("teams").Range(tmp_range).Value
Next
For x = 1 To 12
' Populate the team manager fields;
tmp_range = "c" & Trim(Str(x))
pmhc(x).manager = Worksheets("teams").Range(tmp_range).Value
Next
For x = 1 To 12
' Populate the email fields;
tmp_range = "e" & Trim(Str(x))
pmhc(x).m_email = Worksheets("teams").Range(tmp_range).Value
Next

End Sub

c) The click event code from the user form:

Sub cmd_Admin_Click()
frm_main.Hide
do_admin (pmhc())
End Sub
 
R

Rick Rothstein \(MVP - VB\)

I see two things that I think are causing you problems. One, if pmhc is
declared in the module, it should use Public, not Dim to declare it. Two,
take the parentheses off of the do_admin call. The correct syntax is either
this...

do_admin pmhc()

or this...

Call do_admin(pmhc())

You should **only** use parentheses where **required** by syntax or in
forcing precedence in an expression evaluation. The problem is that when you
use non-syntax parentheses, VB assumes an expression is being evaluated. You
can get away with this for single, simple variable arguments in
function/subroutine calls because VB's evaluation of them as an expression
is simply themselves. But complex things, like Type arrays, multiple
arguments, and such are another story and VB doesn't know what to do with
them. You might be able to get away with complex things like an Object, such
as a control name, but that is because the Object has a default property and
VB will use that as the evaluation of the Object and, if the data type
matches what the function or subroutine is expecting, will pass it with out
raising an objection. The best rule to follow, though, is to use parentheses
only when required and not to "pretty things up".

Rick
 

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