Access form for login using active directory

Sivasan used Ask the Experts™
Hi all,
I have an access application. I have a login form, in MS Access. Currently, I'm saving the user name, password, security level(User,Admin) and their email , in a table 'Table1' in access table. So when the user opens the login form, he/she enters the user name and password and from that form on clicking the submit button, I authenticate against 'Table1' and redirect to other form, by carrying their name and displaying it on other forms.
Now, I like to get the user name from the domain login ( active directory) on their computer so they don't have to have a separate password for this app and keep entering everytime.

Ideally, when they click Login form I automatically capture their login name from their domain login into the computer and using the 'table1' in access, I can find the security level and direct to other forms, I also like to capture their email from active directory if possible.
I will appreciate if somebody can tell me how I can do that.
Thank you in advance.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can get login name with environ("username")
For email you need AD query:
Public Function get_mail() As String
Dim cmd As Object
Dim rootDSE As Object
Dim conn As Object
Dim rs As Object
Dim attr As String
Dim base As String
Dim UserName As String
Dim filter As String
Dim scope As String
UserName = Environ("username")
On Error Resume Next
Set rootDSE = GetObject("LDAP://RootDSE")
base = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"
'get current user objects
filter = "(&(objectClass=user)(objectCategory=Person)" & _
        "(sAMAccountName=" & UserName & "))"
'add mail field to query
attr = "mail"
scope = "subtree"

Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = base & ";" & filter & ";" & attr & ";" & scope

Set rs = cmd.Execute
If Not rs.EOF Then
    get_mail = rs.Fields("mail").Value
End If

End Function

Open in new window


Hi Als315,
Thanks you.Does this go on on click event of a button?
I tried to put the code you have provided on a on click event of a button, to see if I can get the user name.It says expected End of Sub.When I add End Sub, it still says End Sub expected.
At least to test, I'm trying to pass the user name from your code to a text box Tbox in my form on a click of event of a button. I would appreciate if you can tell me what I'm missing.
It is function, place it to module. In onclick event you can use it as follows:
Dim mail as string
mail = get_mail
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


Hi Als315,
Thanks a millione. Yes, when I did mail = get_mail, now, I can get the email. I'm trying to get the user name as well.Looking at your code, I thought UserName will contain the user name but doesn't display the name.I tried the following.

Private Sub Command0_Click()
Dim mail As String
Dim uname As String
mail = get_mail
uname = UserName
Me.T1 = mail
Me.T2 = uname

End Sub

Wondering if I'm doing it right.
uname =  environ("username")


Thanks a million for your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial