Can I set a variable reference to a form that is referenced by a variable?

J

Jon A

I have a Public function that is called from a number of
different forms. One of the things this function does is to
open a form whose FormName is passed as a parameter into the
function.

I can open the form that is referenced by that variable
easily enough by using the following DoCmd statement.
However, when I try to use SET as shown below I get runtime
errors saying that the form "strTheForm" cannot be found.

Public Function LoadQualityFactors(varTheReviewID As
Variant, strTheForm As String) As Boolean

DoCmd.OpenForm strTheForm

Set frmTheForm = Forms![strTheForm]

end Function

I find that I can open the referenced form only by including
the actual form name in the brackets such as

Set frmTheForm = Forms![frm_DisplayQualityFactors]

The problem I have is that this function is useful for a
number of different forms that I have built and I don't want
to have to code every form name into the function. I thought
it would be possible to use variable references to point to
the different forms.

Can anyone tell me what the correct syntax is? I have
searched MSDN and cannot find a thing.
 
A

Albert D. Kallal

You need to realize that virtually everything in ms-access is a collection.
Forms collection, even data in a reocrdset is a collection.

And, you can make your own custom collections (they are great tool). You can
find a great use of custom collections in my multi-select example where I
let you select roecrds..and send them to a reprot:

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

Anyway, I just wanted you to "think" about collections before I just blindly
give you the syntax:

Since the forms collection is a collection, then you can go:

Set frmTheForm = Forms(strTheForm)

Virtually all collections in VB6, (or ms-access) can be referenced via a
string, so you could also go:

Set frmTheForm = Forms("frm_DisplayQualityFactors")

This also means that you can reference a field name in a form, or a
reocrdset via a string (variable) also...
 
J

Jon A

Thanks.

I thought I had tried that syntax and got the same error
message. I don't exactly remember, so either I did not try
that syntax or else I did but maybe I had it typed in wrong.

In any case it works just like you said it would.

Also -
Thanks for the reference to your website. I found some
valuable information there.

I am just at the point in developing this application where
I needed to put together the reporting capability. Your
website had a lot of really good ideas about things I had
not at all considered. That will make things go better but
also that will result in a better application!

Again - thank you very much.

I'm pretty new to Access. this is my first application I am
putting together and I've only been working on it for a
month. So some things like this "Collections" stuff is still
pretty confusing to me - but I'll pick it up as I go along.
 

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