Workaround to reference 'me' in a function module (1 Viewer)

gojets1721

Registered User.
Local time
Yesterday, 16:20
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?
 

sonic8

AWF VIP
Local time
Today, 01:20
Joined
Oct 27, 2015
Messages
998
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
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:20
Joined
Feb 28, 2001
Messages
27,253
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]
 

gojets1721

Registered User.
Local time
Yesterday, 16:20
Joined
Jun 11, 2019
Messages
430
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

Top Bottom