Best way to pass an array from one form to another

D

default105

Access 2k, WinXP

What is the best way to pass an array from one form to another? I have been
told numerous time to avoid at all cost public variables. I have one form
that when a Work Order is completed it has to send out an email to the
appropriate people. Through code it opens another form with a multiselect
listbox and captures the people in an array to then send an email with lotus.
The problem I am having is the information on form1 contains the subject,
body text and attachment code, form2 is the multiselect listbox with the
array of names to pass the sendto part of the lotus automation. How can I
get the array or the other variables to one form only without declaring a
public variable? Sorry so lengthy and thank you in advance.

Pete
 
R

Rod Plastow

Pete,

So what's wrong with public variables? I agree it becomes a maintenance
nightmare if you scatter them all over the place and have a few in each and
every module. I tend to collect all (as far as possible) my public variables,
enumerations, user types and class declarations in one module, suitably named
as CommonVariables (or similar - the shorter the name the better).

I began this response with a long discussion of techniques in communicating
between forms but then thought that the best and simplest way is to include
the list box as a control on the first form. The next best way is to include
the second form as a subform on the first form making all the second form's
controls and values readily available from the first form.

If you really have to have two separate and independent forms then let me
know and I'll finish my discussion and post it.

Rod
 
B

BruceM

I agree that keeping it all on one form, or on a subform, is probably the
simplest choice. One problem with public variables, as I understand, is
that they can lose their value if there is an unhandled error. I use
MZ-Tools to add error handling at the click of a button, so I literally add
it to all procedures one way or the other. It seems to me that passing a
public variable immediately to another form is one thing, while maintaining
it for an entire Access session is another. That being said, I usually look
for a way to pass variables as arguments in function calls rather than using
public variables. I really couldn't say if that method has anything to
recommend it.

As another option to using an array, there are a couple of concatenation
functions that can be used to create a string, which you may be able to pass
to the e-mail function call, depending on the details of how you are doing
that. Duane Hookum's version is here:
http://www.rogersaccesslibrary.com/forum/forum_posts.asp?TID=16

Allen Browne has one here:
http://allenbrowne.com/func-concat.html

I don't know if this approach will work in your situation, but I have used
it to put together a listing of e-mail addressees, so I am passing it along
as something of interest if nothing else.
 
K

Ken Sheridan

You don't have to declare a public variable at standard module level. it can
be declared at the form's class module level, which exposes it as a property
of the class. You can't declare an array in this way, but you could declare
a string variable in Form1's class module's declarations area with:

Public strNameList As String

then assign the selections from the list box on Form2 to the variable as a
tokenised with something like this in Form2's module:

Dim varItem As Variant
Dim strNameList As String
Dim ctrl As Control

Set ctrl = Me.lstNames

' loop through list box's ItemsSelected collection
' and build comma separated list of selected items
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strNameList = strNameList & "~""" & ctrl.ItemData(varItem) & """"
Next varItem

' remove leading tilde
strNameList = Mid(strNameList, 2)

' assign list to Form1's variable
Form_Form1.strNameList = strNameList
Else
MsgBox "No names selected", vbInformation, "Warning"
End If

You can then, by means of the tilde tokens, parse the value of the
strNameList variable in Form1's module to fill an array to pass to lotus
along with the other values.

Ken Sheridan
Stafford, England
 
D

default105

Thank you all for your suggestions, I can see that there are many ways to do
what I asked, unfortunately I can not list all of you for answering my
question but I will pick the suggestion that I decided to go with this time.
 

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