Passing a form name to a function

G

Guest

I have a large number of very similar forms. I need to do the same type of
dependency check and set a status field for each of these forms. I created a
sub to do the dependency checking. I pass the name of the calling form to the
function, but when the function attempts to use that name, I get the
following error:

Run-time error '2450':

Microsoft Office Access can't find the form 'frmName' referred to in a macro
expression or Visual Basic Code.

Here is the code for the sub - the error happens on the first statement
containing frmName:

Public Sub Aquanaut_Status(frmName As String)
....
If Forms!frmName!Aqua1 = True Then

Here is the code for calling the sub:
Aquanaut_Status "AquanautFrm"

It looks to me like Access thinks frmName is the name of the form instead of
a string containing the name. I know this must be a simple syntax thing, but
I can't seem to figure it out. Can anyone please help?

Thx,
Roxanne
 
D

Dirk Goldgar

Sapling said:
I have a large number of very similar forms. I need to do the same
type of dependency check and set a status field for each of these
forms. I created a sub to do the dependency checking. I pass the name
of the calling form to the function, but when the function attempts
to use that name, I get the following error:

Run-time error '2450':

Microsoft Office Access can't find the form 'frmName' referred to in
a macro expression or Visual Basic Code.

Here is the code for the sub - the error happens on the first
statement containing frmName:

Public Sub Aquanaut_Status(frmName As String)
...
If Forms!frmName!Aqua1 = True Then

Here is the code for calling the sub:
Aquanaut_Status "AquanautFrm"

It looks to me like Access thinks frmName is the name of the form
instead of a string containing the name. I know this must be a simple
syntax thing, but I can't seem to figure it out. Can anyone please
help?

Thx,
Roxanne

Jeff has given you an answer based on the idea of passing a reference to
the form itself, rather than just the name. It's a good answer, and
probably preferable for most purposes. Just for completeness, I'll post
the answer to your specific question. Here's how to use the name of the
form as a string index to the Forms collection:

Public Sub Aquanaut_Status(frmName As String)

If Forms(frmName)!Aqua1 = True Then
' ...
 
G

Guest

This also worked great. Thanks for taking the time to answer my specific
question. Your answer helped me to understand using the form name as an index
to the Forms array. It is also good to know that passing a reference to the
form itself is the prefered method. Thanks!
 
D

David C. Holley

I would actually pass the form as an object as in ...

...........
Function createOutlookAppointmentFromId(lngTransportID As Long, frm As
Variant)

If IsNull(frm) = False Then
frm.txtAdvisory = "Getting Outlook Entry Id"
frm.Repaint
End If
...........

I declare frm as a variant to allow for situations where I want the
procedure to run independent of any particular form, otherwise declaring
it as an OBJECT would be fine. By passing the form to the function (or
sub), the frm.txtAdvisory statement will work on any FORM that has a
control named txtAdvisory. A side benefit is that if I need to do
anything else with the form, I've already got the object there in the code.
 

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