Getting user information from Active Directory with vba

Posted on 2005-03-07
Medium Priority
Last Modified: 2010-05-02
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.

Question by:carpetflyer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 16

Accepted Solution

mdiglio earned 2000 total points
ID: 13481903
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
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

LVL 22

Expert Comment

ID: 13481913

Have you tried:  GetObject("LDAP://OU=MyUsers,dc=" & sDomain) ?

Author Comment

ID: 13482002
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.
LVL 16

Expert Comment

ID: 13482071
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)

Author Comment

ID: 13482087
Alright sounds good. Thanks for the help again.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question