Use listbox as variable

J

jpowell

Hello,

I tried posting this earlier but unfortunately didn't get any help and
still haven't solved this. Hopefully someone can help me! I want to
use a listbox as a variable and I can't figure out how.

I have a form that can potentially get called from many different
forms. For instance the same for frmPhones might be called from
frmOrganizations or from frmIndividuals. After pressing "OK" in
frmPhones I want to requery the listbox from the original form
(frmIndividuals or frmOrganizations). So, from the original form I
would pass in the listbox that will be updated, right?

I tried setting a public property in frmPhones called ListBox as a
ListBox and tried this but it didn't work.

With frmPhones

Set .ListBox = frmIndividuals.lstPhones
(I've tried with and without the set)

End With

What am I missing?!?!? Please help!

-Joshua
 
A

Albert D. Kallal

You just need to pass the name as a string

But, even better would be to pick up the name of the calling form automatic,
and not actually pass the values.

At the forms level code, in the 2nd form (phones), you can go:


dim frmPrevious as form

Then in the he forms on-open event, you can go:


set frmPrevious = screen.ActiveForm

Note that you can EVEN pick up the previous ActiveForm as late as the forms
on-load event.


At this point, anywhere in the code, you can do all kinds of things to the
previous form

frmPreous.Requery

frmPrevous!MyListBox.Requery

msgbox "the value of the list box in the previous form is " &
frmPrevous!MyListBox

You could use the above form re, and still pass the listbox name in the open
arges like:

docmd.OpenForm "frmPhones",,,,,,"MyListBoxName"

So, we just pass the name of the listbox as a string. Then, in code you can
go:

frmPrevous(me.OpenArgs).Requery

So, in any code, you can always use a string var to ref a listbox


strMyControl = "lastName"

msgbox "the value of hte last name contorl is " & me(strMyContorl)
 
J

James Hahn

You can refer to a control using a combination of the form name and the
control name. The form reference is required if the control is on a
different form.
Eg, from frmPhones you would use:
Forms!frmIndividuals!lstPhones

If you need to select the form at run time then you could pass a coded value
in the argument list of the function call, or set a global variable to a
coded value that indicates the calling form.

If CallingForm = 1 then
Forms!frmIndividuals!lstPhones ...
else
Forms!frmOrganisation!lstPhones ...
end if

A neater solution would be to pass the calling form's forms() collection
index number, so you could use

Forms(CallingForm)!lstPhones ...
 
J

jpowell

Albert,

Thanks for the quick response and this makes great sense but I'm
running into an error 13 "Type mismatch." Any advice?

-Joshua
 
J

jpowell

Albert,

Thanks for the quick response and this makes great sense but I'm
running into an error 13 "Type mismatch." Any advice?

-Joshua
 
J

jpowell

Thanks for responding James, I was hoping to parameterize frmPhones a
little more to avoid a huge mess of logical arguments (there are many
more forms than just individuals and organizations!). Thanks again!

-Joshua
 
J

jpowell

Well, while I didn't solve my unlucky error 13 problem I came up with a
solution I am quite happy with. Maybe it will lead to some trouble in
the future but right now it works beautifully. I followed your advice,
but instead of dimming frmPrevious as a Form I made it a string so my
form Load code says

strPrevious = Screen.ActiveForm.Name

In my calling form I have

public sub RequeryLists(strListGroup as String)

Select Case strListGroup

Case "ContactInfo"

Me.lstPhones.Requery
Me.lstEmails.Requery

End Select

And then in each form that is called ( ie frmPhones) the last step in
my cmdOK_Click code is

Call Forms(strPrevious).RequeryLists("ContactInfo")

It works magically!

-Joshua
 
A

Albert D. Kallal

Albert,

Thanks for the quick response and this makes great sense but I'm
running into an error 13 "Type mismatch." Any advice?

-Joshua
It sounds like you found a solution. It was/is not clear where you are
getting the above error?

The assume here is that you are going to pass the text name of the listbox
via open args..(open args is only text values).

And, you are grabbing the text name of the previous form (which is not a bad
idea).

And, do note you could pass two vars in the open.args like:

docmd.OpenForm "frmName",,,,,,"abc,def"

Then, in the forms on-open event, you go:

strParm1 = split(me.OpenArgs,",")(0)
strParm2 = split(me.OpenArgs,",")(1)

And, to be safe, I often use ~ as delimiter...
 
Top