Public functions within a form module

JoshuaAnthony

Registered User.
Local time
Today, 21:38
Joined
Dec 18, 2003
Messages
68
Hi all,

I am now in the practise of placing all of my public functions where possible in a separate module but if you wanted, could you declare public functions within a particular form (I realise that the form would probably have to be open when calling the function)? It doesn't seem to work for me. I get an error message saying that the function I am calling is not declared anywhere... I would prefer not to place this particular function in a public module because then I don't get the benefit of using the "Me" keyword (for readability) for this rather large function!

So does anybody know if public declarations (variables too) work from within form modules, and if so how is it different to public declarations within a separate module? :)

Thanks,

Joshua
 
Last edited:
No. The reason is that the code behind a form is a Class module and, like any Class module, you would have to declare it in order to use it. Always put Public Functions in a standalone module.
 
G’day Joshua

Hmmm………

Code:
Public Sub TestIt()

    [color=green]'   Call a Public Function XYZ on the Form and pass argument.
    '   Display XYZ return value.[/color]
    MsgBox Forms!frmMain.XYZ("Fred")
    
    [color=green]'   Display Public variable ABC on Form.[/color]
    MsgBox Forms!frmMain.ABC

End Sub
As you say, the Form must be Open.

Hope that helps.

Regards,
Chris.
 
Actually, now that I think about it...

I thought it was working but just realised that Access doesn't seem to like what I am doing.

When I try:

Code:
Call [Forms]![Edit NCR].NCR_Selection_Click()

I get a syntax error on the '!'. Says: expecting a '.' or a '('.

So then (just for the craic) tried:

Code:
Call [Forms].[Edit NCR].NCR_Selection_Click()

This returns an error also: "Object doesn't support this property or method"

So I thought that maybe it just assumes the forms part:

Code:
Call [Edit NCR].NCR_Selection_Click()

That returns the error: "Can't find the field '|' referred to in your expression"

Finally I tried:

Code:
[Forms]![Edit NCR].NCR_Selection_Click()

"Application-defined or object-defined error"

Am I doing something wrong Chris??
 
Or...

Code:
    Dim strCName As String
    Dim myFrm As Form

    Set myFrm = ClosedFrmWithFunction

'Function name = CName
    strCName = myFrm.CName

    MsgBox strCName

    Set myFrm = Nothing

Regards,
Tim
 
Or…

Code:
Forms![Edit NCR].NCR_Selection_Click
Also it might be a good idea to remove spaces from names.

Regards,
Chris.
 
Hey Tim,

I tried the following:

Code:
Dim testForm As Form
Set testForm = [Forms]![Edit NCR]
Call testForm.NCR_Selection_Click

and it returned runtime error 2465:

"Application-defined or object-defined error"

I'm lost!
 
Sorry!

Actually guys. All of those suggestions work...

I've been changing stuff around so much (and putting things in and out of separate modules) that I forgot to return the function to public... :rolleyes: You'd think the errors would be more descriptive! ;)
 
JoshuaAnthony said:
I'm lost!

As Chris suggested, eliminate the space in your form name -- this may or may not be significant but why introduce an extra variable in the equation when testing?

Create a public sub in the form you want to access from another form.

Code:
Public Sub BeepBeep()
      Beep
      Beep
End Sub

Then try this code in the other form.

Code:
    Dim testForm As Form

'Add the "Form_" prefix even though you didn't name the form with prefix.
'Press Ctrl-R in the VBA Window to see the prefix Access attached.
    Set testForm = Form_EditNCR 

'Let it beep.
    testForm.BeepBeep

Regards,
Tim
 
G’day Joshua.

Now you’re asking too much. :)

Regards,
Chris.
 

Users who are viewing this thread

Back
Top Bottom