Access form for login using active directory

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SivasanAuthor Commented:
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
Get a highly available system for cyber protection

The Acronis SDI Appliance is a new plug-n-play solution with pre-configured Acronis Software-Defined Infrastructure software that gives service providers and enterprises ready access to a fault-tolerant system, which combines universal storage and high-performance virtualization.

SivasanAuthor Commented:
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")
SivasanAuthor Commented:
Thanks a million for your help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.