VB6 reading Firstname, Lastname and email from Active Directory

Posted on 2004-11-10
Last Modified: 2008-01-09

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.
Question by:MPKR
    LVL 2

    Expert Comment

    Instead of using the "name" property, use .sn  for surname (last name) and .givenname for first name.
    LVL 3

    Expert Comment

    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.
    LVL 2

    Expert Comment

    He was probably using the property name "lastname".  MPKR, is this what you wanted to add?:

    "select name, distinguishedname, memberOf, sn, givenname from...."
    LVL 3

    Accepted Solution

    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
    LVL 4

    Author Comment

    @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, 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??

    LVL 2

    Assisted Solution

    If it works, why not? It could be that someone else in your company changed around your AD schema.  Whatever works! :)
    LVL 4

    Author Comment

    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 ??

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now