Hello, I’m writing some code that reads some user attributes out of Active Directory and populates a user form with the attribute values. Previously I have always done this by populating the user form with values from the Registry, so this is new to me. I have the code working to read all the attributes and just a particular attribute, and it runs just fine as long as it is not called from within the user form code. For example, the following sub works:
Public Sub test()
‘gets the login ID of the logged in user, passes it to a public variable – works fine
basUserADInfo.GetLoginUserName
‘Parameters are the public var for the login ID, and the AD attr name; it returns public var sAttr which contains the value of the AD attr
basUserADInfo.GetUserAttr LoginUserName, “firstname”
MsgBox sAttr ‘displays the value just fine
End Sub
The code which gets the attribute – basUserADInfo.GetUserAttr – follows below:
Public Function GetUserAttr(LoginName As String, sAttrib As String) As String
‘PURPOSE: Display information that is available in the Active Directory about a given user
Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim oRoot As IADs
Dim oDomain As IADs
Dim sBase As String
Dim sFilter As String
Dim sDomain As String
Dim sAttribs As String
Dim sDepth As String
Dim sQuery As String
Dim user As IADsUser
On Error GoTo ErrHandler:
‘Get user Using LDAP/ADO
Set oRoot = GetObject(“LDAP://rootDSE”)
‘Work in the default domain
sDomain = oRoot.Get(“defaultNamingContext”)
Set oDomain = GetObject(“LDAP://” & sDomain)
sBase = “”
‘Only get user name requested
sFilter = “(&(objectCategory=person)(objectClass=user)(SAMaccountName=” & LoginName & “))”
sAttribs = “ADsPath”
sDepth = “SubTree”
sQuery = sBase & “;” & sFilter & “;” & sAttribs & “;” & sDepth
conn.Open “Data Source=Active Directory Provider;Provider=ADsDSOObject”
Set rs = conn.Execute(sQuery)
If Not rs.EOF Then
Set user = GetObject(rs(“ADsPath”))
With user
sAttr = user.Get(sAttrib)
End With
End If
ErrHandler:
On Error Resume Next
If Not rs Is Nothing Then
If rs.State 0 Then rs.Close
Set rs = Nothing
End If
If Not conn Is Nothing Then
If conn.State 0 Then conn.Close
Set conn = Nothing
End If
Set oRoot = Nothing
Set oDomain = Nothing
End Function
Now, the code I am using to populate the user form is:
Private Sub UserForm_Initialize()
Dim ctlText As Control
basUserADInfo.GetLoginUserName
basUserADInfo.GetUserAttr LoginUserName, “firstname”
txtFirstName.Value = sAttr
End Sub
I actually had this setup so the basUserADInfo.GetUserAttr procedure was a function that returned the attribute value, so I could run the following code, which cycles through all my tagged controls (which are named the same as the AD attributes) and populates them:
For Each ctlText In Me.Controls
If ctlText.Tag “” Then
ctlText.Value = basUserADInfo.GetUserAttr(LoginUserName, ctlText.Tag)
End If
Next
Set ctlText = Nothing
But, when that didn’t work I started simplifying the code (to what I have at the top of this post) in an effort to figure out what wasn’t working. Finally, I got the code so darn simplified, as you can see, and it works like a charm unless I try to call it from the user form…so I am at a loss.
I hope someone out there can point out what I am doing wrong. I just hate ending the day stuck!
Thanks to all you gurus,
Karina