Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Getting user information from Active Directory with vba

Hello everyone.

I recently came across this script http://www.freevbcode.com/ShowCode.Asp?ID=710 which returns user information from the AD.  The problem I am getting is I am trying to access a certain OU.  

I am new to LDAP with AD so I have no idea how to alter the code.  
On the line
Set oDomain = GetObject("LDAP://" & sDomain)

I tried changing it to
Set oDomain = GetObject("LDAP://OU=MyUsers," & sDomain)
but it wouldnt work.

If i run the script without altering it, i can access all the users in the "Users" folder in AD.  Whoever created the AD has placed the OU outside of the "users" folder....so if somedomain.com is the domain, the OU "MyUsers" and the "Users" folder will be subdirectories withthin that domain.

Can you assist me in to having this script run for the OU "MyUsers"?

I hope this is clear.

Thanks.
0
carpetflyer
Asked:
carpetflyer
  • 2
  • 2
1 Solution
 
mdiglioCommented:
Hello,
Make sure you have a reference to:

ActiveDS type library
Microsoft ActiveX Data Objects 2.7 library

Change the sfilter to query for the samaccountname instead of just name
GOOD:
sFilter = "(&(objectCategory=person)(objectClass=user)(samaccountname=" _
  & LoginName & "))"

Not so GOOD
sFilter = "(&(objectCategory=person)(objectClass=user)(name=" _
  & LoginName & "))"

It was probably throwing an error because it couldn't match the name attribute.
'Name' is different than the LoginName that they are using in the code.
samaccountname will always be the LoginName, but the 'name' attribute does not always have to be the loginname

Also the way the code was made you do not need to narrow the search by supplying the ou=myUsers, .
But feel free to do so as it will be a less intensive search

0
 
DarkoLordCommented:
Hi

Have you tried:  GetObject("LDAP://OU=MyUsers,dc=" & sDomain) ?
0
 
carpetflyerAuthor Commented:
mdiglio nice job!

The samaccountname helped out....i was actually entering the username and i guess it was looking for the full name instead. that "samaccountname" made it work.

One more question, how would i alter the code for it to look under a certain OU? Is it the line i used in my question above?

sirbounty: I was trying to get the user's initials for our Access DB.  So i found the code above to help me out.

Thanks again everyone.
0
 
mdiglioCommented:
Glad it worked for you.

Yes what you have should work fine
Set oDomain = GetObject("LDAP://OU=MyUsers," & sDomain)
it can be as deep as you need it to go....
Set oDomain = GetObject("LDAP://OU=IT,OU=Managers,OU=MyUsers," & sDomain)
0
 
carpetflyerAuthor Commented:
Alright sounds good. Thanks for the help again.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now