Workaround to reference 'me' in a function module

gojets1721

Registered User.
Local time
Today, 15:40
Joined
Jun 11, 2019
Messages
430
I created a function in a module shown below:

Code:
Function GetEmail()
    'GetEmail= DLookup("ManagerEmail", "tblLocations", "Location='" & Me.Location & "'")
End Function

I wanted to use this in multiple forms/reports. No matter where it's is used, the form/report will always has a 'Location' field. That's why I tried using 'Me.Location' but obviously that doesn't work since 'me' is referring to the function itself.

Any suggestions on how to get around this issue without completely scrapping function?
 
Me is referring to the instance of the class the code resides in. As this does not apply to "normal" modules, Me is simply invalid there.

No matter where it's is used, the form/report will always has a 'Location' field.
Until it hasn't...

I would pass in the location as an argument to the function. Then you are not depending on assumptions of the calling context.
Code:
Function GetEmail(ByVal Location As String)
    GetEmail= DLookup("ManagerEmail", "tblLocations", "Location='" & Location & "'")
End Function
 
Instead of

Code:
Function GetEmail()
    'GetEmail= DLookup("ManagerEmail", "tblLocations", "Location='" & Me.Location & "'")

Use

Code:
Function GetEmail(frm as Access.Form)
    'GetEmail= DLookup("ManagerEmail", "tblLocations", "Location='" & Frm.Location & "'")

I.e. make the form an object parameter and pass it in. Then invoke it with

Code:
strX = GetMail(me)
[CODE]
 
Instead of

Code:
Function GetEmail()
    'GetEmail= DLookup("ManagerEmail", "tblLocations", "Location='" & Me.Location & "'")

Use

Code:
Function GetEmail(frm as Access.Form)
    'GetEmail= DLookup("ManagerEmail", "tblLocations", "Location='" & Frm.Location & "'")

I.e. make the form an object parameter and pass it in. Then invoke it with

Code:
strX = GetMail(me)
[CODE]

Thanks you! This worked!
 

Users who are viewing this thread

Back
Top Bottom