Calling A Module Via Button In Form

roystreet

Registered User.
Local time
Today, 03:15
Joined
Aug 16, 2004
Messages
47
Hello,
I am trying to put functions into modules now. I have little experiece with this since I have always placed them directly in the forms. But I'm trying to get more organized, plus (and maybe more importantly) learn VBA more. I have this code in a module called: Number_System

Code:

Me.Unique_Number = Format(Nz(DMax("[DAA-MasterTable].Unique_Number", "DAA-MasterTable", "Standard_Number = '" & Me.Standard_Number & "' AND FY = '" & Me.FY & "'"), 0) + 1, "0000")

DoCmd.GoToControl "Employee_Name"


I call it via a button in the form by:
Call Number_System.GenerateNumber(Me)

Everytime I press the button it states; "Invalid Use Of ME Keyword"
Not 100% sure why it's doing this. I took a stab in the dark and removed all of the "Me." from the module and that did not work.

Thanks,
---roystreet

Also, I don't know if this makes a difference in this case, but would I use public function or public sub in the module?? Currently, I'm using public function.
 
Last edited:
"Me" is a context-sensitive shortcut that has meaning only in the context of a class module (form/report). Inside a GENERAL module, "Me" has meaning only if it was passed into the module as an Object.

I can't tell you WHY it is this way other than that the compiler looks at "Me" at different times depending on whether you are in a general or a class module. It is quite probably that for general modules, there is no context for the compiler to use to establish what "Me" means. Which means that "Me" is not truly dynamic. The COMPILER must bind "Me" rather than have it as a dynamic runtime call.
 
«Me.Unique_Number = Format...»
means: I want to put «Format...» in the field «Unique_Number» of the form «Me».

Since you are working in a module, «Me» is the name of the module, but you want to put «Format...» in the form, not in the module. So, you've got to replace «Me» by the name of the form.

Ex.: «frmMyForm.Unique_Number = Format...»

When you don't put «Me», and leave it blank, it uses «Me» as default. So, it is normal if it doesn't work in your case. «Me» is used in the code of the form only, not in the modules.

Oups! Doc has been faster than me. ;)
 
Last edited:
OK...What about this???

OK, I'm about to test the code...But I want to call this code for use in more than form. So, would I really want to place the "frm.EntryForm" in the module?? In this case "EntryForm" is the form name...But if I wanted to use the same code, I can't use the code anymore?

Also, in any case do I still use the "(Me)" portion of the buttons code?
Example: Call Number_System.GenerateNumber(Me)
 
Here's What I Tried....

OK, here is what I placed in the module:

Code:
[COLOR=Blue]Public Function GenerateNumber(frm As Form) As Integer

    frm.Unique_Number = Format(Nz(DMax("[DAA-MasterTable].Unique_Number", "DAA-MasterTable", "Standard_Number = '" & frm.Standard_Number & "' AND FY = '" & frm.FY & "'"), 0) + 1, "0000")

    DoCmd.GoToControl "Employee_Name"

End Function[/COLOR]
And this is how I call it with the button:

Code:
[COLOR=RoyalBlue]Private Sub Number_Generator_Click()
Call Number_System.GenerateNumber(Me)
End Sub[/COLOR]
It appears to be working now. I went ahead and tested it in another form and it's working. Any comments or suggesstions??

Thanks,
---roystreet
 
If you want to use it in more then one form then save the code in a module and reference the variables when you call the function from the different forms like :

(example in Module)
Public Sub EXAMPLE(strFormName as String)
etc...
.
.
.
End Sub

(example in Form)
Private Sub Button_On_Click
call EXAMPLE(Me.Name)
end sub

HTH,
Kev
 
In the module's formal parameters, you can define an object of type form. Then in the call to the module, "Me" can be one of the actual arguments. I've done that dozens of times.
 

Users who are viewing this thread

Back
Top Bottom