Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10

    Unanswered: Querying Active Directory

    So I finally had a chance to play with reading data via ADO in Access and I've hit a stumbling block...
    I'm receiving "type mismatch" errors when trying to read from the otherTelephone field. This field can hold multiple values, and when exported as an delimited text file, the multiple values are separated by semi-colons and I think this is the reason I'm getting stuck here!

    FYI here's my code (utilising late binding) to read from AD
    Code:
    Dim rs As Object
    Dim strSQL As String
    Const adOpenStatic As Integer = 3
    
    strSQL = "SELECT sAMAccountName, telephoneNumber, otherTelephone " & _
                 "FROM 'LDAP://DC=<...>,DC=<...>'" & _
                 "WHERE objectClass='user' AND objectCategory='Person' AND sAMAccountName='smithj'"
    
    Set rs = New ADODB.Recordset
    
    rs.Open strSQL, "Provider=ADSDSOObject;", adOpenStatic
    
        If rs.EOF And rs.BOF Then
            Me.txtNTUsername.Value = "Unable to find a match in AD"
        Else
            Me.txtNTUsername.Value = rs.Fields("sAMAccountName")
            Me.txtTelephoneNumber.Value = rs.Fields("telephoneNumber")
            Me.txtOtherTelephone.Value = rs.Fields("otherTelephone")
        End If
        
        rs2.Close
        Set rs2 = Nothing
    With the code as above, no value is entered into txtOtherTelephone even though there definately is something in there (in this example there is also only a single value in otherTelephone).

    When I change the line to read
    Code:
            Me.txtOtherTelephone.Value = rs.Fields("otherTelephone") & "x"
    Or
            Me.txtOtherTelephone.Value = Cstr(rs.Fields("otherTelephone")) & "x"
    I get
    Error 13 Type Mismatch

    Any ideas what's wrong here?
    If I've not explained clearly enough or you have any questions please let me know!

    Cheers,
    George
    George
    Home | Blog

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    What does:
    Code:
    rs.Fields("otherTelephone").type
    return?

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Ok, found the solution.
    The value returned is of type Variant (it's an array of values)!

    So what I had to do was the following
    Code:
    Dim i As Integer
    Dim telExtension() As Variant
    
    If IsNull(rs.Fields("otherTelephone").Value) Then
        Me.txtTelExtension.Value = ""
    Else
        telExtension() = rs.Fields("otherTelephone").Value
        If UBound(telExtension) > 0 Then
            For i = 0 To UBound(telExtension)
                Me.txtTelExtension.Value = Me.txtTelExtension.Value & telExtension(0) & "; "
            Next i
        Else
            Me.txtTelExtension.Value = telExtension(0)
        End If
    End If
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •