Use custom functions in the Expression Builder

1.7k Views Asked by At

I added a function to get the Active Directory user login, for an Access DB using VBA, but I'm not sure why I don't see my function listed in the Expression Builder

I defined the functions like in this question but I can not see the function in the Expression Builder. I plan to use this function to fill an invisible txtBox on my form and log it into db.

    Public Function GetUser(Optional whatpart = "username")
        Dim returnthis As String
        If whatpart = "username" Then GetUser = Environ("USERNAME"): Exit Function
        Set objSysInfo = CreateObject("ADSystemInfo")
        Set objUser = GetObject("LDAP://" & objSysInfo.USERNAME)
        Select Case whatpart
            Case "fullname": returnthis = objUser.FullName
            Case "firstname", "givenname": returnthis = objUser.givenName
            Case "lastname": returnthis = objUser.LastName
            Case Else: returnthis = Environ("USERNAME")
        End Select
        GetUser = returnthis
    End Function

enter image description here

1

There are 1 best solutions below

1
On

I think you're asking how to have custom functions show up in the Expression Builder? (and therefore unrelated to Active Directory or logins).

It's most likely a simple case of either:
- you haven't saved the module since pasting the functions in, or,
- the functions are not located in a public module.

To make sure they're saved:
1. Close Access completely (all open databases), and then,
2. Re-open the database, and,
3. Open your module (from the "Modules" section of the Navigation Bar:

img

...and confirm your code is there.

Then try the expression builder again.


If the module is not showing in the Modules section, then your code is not in a public module.

Note that you don't have to use the Expression Builder. I've actually never used it, but I just tried it and once the module with my functions was saved, they showed up under:

FunctionsMy database nameMy module name.


This screenshot shows how my function wasn't listed i the Expression Builder until I saved it, and then how I found it in the Builder: