none
Custom attribute, Active Directory and VBA RRS feed

  • General discussion

  • Hi guys

    I have the following function which attempts to add a value to a user's AD account. It works fine on all the standard attributes, but I want to update a user's "Clock" which is a custom attribute we have at my company. Whenever I do that, it fails as if the attribute does not exist. It does not have a different name in the AD schema... Any ideas?

    Public Function AddDataToADRecord(sAM As String, ADprop As String, ADval As String) As Boolean
    
        Dim objRootLDAP As Object
        Dim objUser     As Object
        Dim dn          As String
        
        Set objRootLDAP = GetObject("LDAP://rootDSE")
        
        dn = GetDNFrom(sAM)
        If dn <> "" Then 'To ensure a non-existent user does not cause a crash
            Set objUser = GetObject("LDAP://" & GetDNFrom(sAM))
            
            'Update the property requested with the value requested
            On Error Resume Next
            objUser.Put ADprop, ADval
            objUser.SetInfo
            On Error GoTo 0
            
            If GetAdsProp("samAccountName", sAM, ADprop) = ADprop Then
                AddDataToADRecord = True
            Else
                AddDataToADRecord = False
            End If
        Else
            AddDataToADRecord = False
        End If
        
        Set objUser = Nothing
        Set objrootdlap = Nothing
    
    End Function

                                
    • Changed type Bill_Stewart Wednesday, May 30, 2018 6:50 PM
    • Moved by Bill_Stewart Wednesday, May 30, 2018 6:51 PM Off-topic/unanswerable
    Tuesday, March 27, 2018 7:52 PM

All replies

  • The only general advice I could provide is to remove On Error Resume Next and see what the error is. (Do not use that line of code unless you understand exactly what it does and how it works.)

    Other than than, this is not a VBA forum.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 27, 2018 8:17 PM
  • Thanks, unfortunately, the error message is not enlightening, hence it's inclusion (automation error).

    I've now opened a copy of this in the Excel forum, so please don't worry about answering people.

    • Edited by Adeling Tuesday, March 27, 2018 8:54 PM Clarification
    Tuesday, March 27, 2018 8:47 PM
  • The error message may not be enlightening to you, but that doesn't mean it won't be obviously meaningful to someone else. (Remember, nobody can see your screen.)

    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 27, 2018 9:45 PM
  • To learn how to write code in VBA you need to post in the VBA developers forum.

    This is the correct way to manage errors in VB/VBA.

    Public Function AddDataToADRecord(sAM As String, ADprop As String, ADval As String) As Boolean
        On Error GoTo error_exit
    
        Dim objRootLDAP As Object
        Dim objUser     As Object
        Dim dn          As String
        
        Set objRootLDAP = GetObject("LDAP://rootDSE")
        
        dn = GetDNFrom(sAM)
        If dn <> "" Then 'To ensure a non-existent user does not cause a crash
            Set objUser = GetObject("LDAP://" & GetDNFrom(sAM))
            
            'Update the property requested with the value requested
            objUser.Put ADprop, ADval
            objUser.SetInfo
            
            If GetAdsProp("samAccountName", sAM, ADprop) = ADprop Then
                AddDataToADRecord = True
            Else
                AddDataToADRecord = False
            End If
        Else
            AddDataToADRecord = False
        End If
    Exit function error_exit: msgbox "an error has occurred" & Err.Description End Function

    VBA is NOT VBScript and cannot be managed like VBScript.


    \_(ツ)_/



    • Edited by jrv Tuesday, March 27, 2018 11:01 PM
    Tuesday, March 27, 2018 11:00 PM
  • The error message may not be enlightening to you, but that doesn't mean it won't be obviously meaningful to someone else. (Remember, nobody can see your screen.)

    -- Bill Stewart [Bill_Stewart]

    "Unknown error" - does that help?! :-)
    Friday, March 30, 2018 12:42 PM
  • "Unknown error" - does that help?! :-)

    That's the textual description of the error, not the error code.

    If you want to ask for troubleshooting help in a forum, the best way to do that is to post a short example script that contains only the absolute minimum amount of code needed to reproduce the problem. (This is sometimes called the SSCCE principle.) In your case, since you are using a custom attribute, what you would do is reproduce the error with a standard attribute, so that respondents can get your short code example to fail in exactly the same way.

    In any case, this is all an aside, as this isn't a VBA forum anyway.


    -- Bill Stewart [Bill_Stewart]

    Friday, March 30, 2018 2:28 PM