VB6 reading Firstname, Lastname and email from Active Directory


I want to read Firstname, Lastname and email Address from ADS.

I tried this:

    Dim RS As ADODB.Recordset
    Dim cnn As ADODB.Connection
    Dim cmd As ADODB.Command
    Set cnn = CreateObject("ADODB.Connection")
    Set cmd = CreateObject("ADODB.Command")

    cnn.Provider = "ADsDSOObject"  ' This is the ADSI OLE-DB provider name
    cnn.Open ("Active Directory Provider")

    cmd.ActiveConnection = cnn
    cmd.CommandText = "select name, distinguishedname, memberOf from 'LDAP://" & DOMAINCONTROLLER & "/OU=TKN-Kr,OU=User,OU=Administration,DC=kt,DC=tkw,DC=com' WHERE objectCategory='user' ORDER By Name"
    cmd.Properties("Page Size") = 1000
    cmd.Properties("Timeout") = 30
    cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE 'search all containers
    cmd.Properties("Cache Results") = False

I can get RS.Fields("name") containing the shown name. But sometimes it includes a comma, sometimes not. So I want to read firstname and lastname seperately.

But I cannot add lastname or any other field to my SQL string, always an error comes up then. Why not???

Thank you.
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.

Instead of using the "name" property, use .sn  for surname (last name) and .givenname for first name.
What is the error you get when you add lastname to the SQL string? Is lastname a field in the table you are reading from? If not, you will have to perform a string function on the name field. Please give more info.
He was probably using the property name "lastname".  MPKR, is this what you wanted to add?:

"select name, distinguishedname, memberOf, sn, givenname from...."
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Also give examples of the data in the name field

If it is displayed as "Smith, John", create this variable to return "John":

dispname=RIGHT(RS.Fields("name") , (LEN(RS.Fields("name") )-INSTR(",", RS.Fields("name") , 1)))

It finds the location of the comma in the string, then returns everything to the right of the comma. However, string functions will be complicated if not all of the names are formatted in the same way.

If this is what you want to do, more info would help me solve your problem

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
MPKRAuthor Commented:
@paix20: I don't have no table to look up what kind of fields are in cause I want to read the ADS.
@Dudge: Yes, thats it, I tried those Attributes, I found them in ms.com, but they don't work!

If I incluce filds like LASTNAME or FIRSTNAME or SURNAME (..), the application does not work at all and brings this err.desc-err.number:

"unknown error - 2147467259"

The used SQL is for example:
select name, lastname, distinguishedname, memberOf from 'LDAP://ktkrs2000/OU=TKN-Di,OU=Benutzer,OU=Administration,DC=ktn,DC=tk,DC=com' WHERE objectCategory='user' ORDER By name
Set RS = cmd.Execute

Whenever I try to concat my names from the displayed name (field "name") I also get errors, cause sometimes the ADS (including more then 2000 entries) is not administrated correctly, so first/last are turned rund, comma is missing, fields are empty ... I can not go tis way.

So I tried the following:

select name, distinguishedname, memberOf from 'LDAP://ktkrs2000/OU=TKN-Di,OU=Benutzer,OU=Administration,DC=ktn,DC=tk,DC=com' WHERE objectCategory='user' ORDER By name
Dim objUser As Object
Set objUser = GetObject("LDAP:// " & strDistName & "")
u_Lastname = objUser.Lastname
u_Firstname = objUser.Firstname

But this looks very funny and complicated and tricky to me. Is this the right way to go??

If it works, why not? It could be that someone else in your company changed around your AD schema.  Whatever works! :)
MPKRAuthor Commented:
Sometimes I wonder if anyone ever understood the dos and do nots of contacting ADS with different languages/technologies.

I never found any example in the web that works. None can tell me how you should start off, nobody can give adivce where the mistakes are... :)

Does anyone know a good book I could buy to get to get an introduction into programming ADS with ASP/ASP .NET / VB ??
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
Visual Basic Classic

From novice to tech pro — start learning today.