0 Replies - 1084 Views - Last Post: 13 June 2014 - 01:18 PM Rate Topic: -----

#1 w33ds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-June 14

Connecting to LDAP works in VBS but not in VB6

Posted 13 June 2014 - 01:18 PM

I'm having trouble connecting to LDAP via ADO in VB6. I have these references added to VB6: Visual Basic For Applications, Visual Basic runtime objects and procedures, Visual Basic objects and procedures, OLE Automation, Active DS Type Library, Microsoft ActiveX Data Objects 6.1 Library. I thought at first that maybe I had the connection string wrong or didn't have the proper access, however I am able to get it to work in a VBS script. I'm not sure what the difference is. I've even tried executing the VBS script from within VB6, but it doesn't like that either so I'm starting to think it's a problem with my IDE setup? When I try to run it in VB6 I get the error "Run-time error '-2147217865(80040e37)': Table does not exist." When I run it in VBS it works fine and returns the email address of whatever username I enter. If anyone has any insight to this, I would greatly appreciate any suggestions. Here is my code:

VBS (works):
' Get Domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")

userID = InputBox("Enter user ID:")
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Properties("User ID") = "corporate\anonymous"
objConnection.Properties("Password") = "amfam01"
objConnection.Properties("Encrypt Password") = True
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select mail from 'LDAP://" & strDomain & "' where objectClass='user' AND objectcategory='person' AND sAMAccountName='" & userID & "'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30 
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
objCommand.Properties("Cache Results") = False
'WScript.Echo objCommand.CommandText
Set objRecordSet = objCommand.Execute
If objRecordSet.EOF Or objRecordSet.BOF Then
	Wscript.Echo "No record found for " & userID & "!"
Else
	Wscript.Echo "Email: " & objRecordSet.Fields("mail").Value
	objRecordSet.MoveNext
End If


VB6 (gets error message):
Private Sub Form_Load()

   Dim userID As String
   Dim email As String
   
   userID = InputBox("Enter user ID:")
   email = getEmail(userID)

End Sub
Public Function getEmail(username As String) As String
   
   Dim objRootDSE
   Dim objConnection
   Dim objCommand
   Dim objRecordSet
   Dim strDomain As String
   
   ' Get Domain name from RootDSE object.
   Set objRootDSE = GetObject("LDAP://RootDSE")
   strDomain = objRootDSE.Get("DefaultNamingContext")
   
   Const ADS_SCOPE_SUBTREE = 2
   Set objConnection = CreateObject("ADODB.Connection")
   Set objCommand = CreateObject("ADODB.Command")
   objConnection.Provider = "ADsDSOObject"
   objConnection.Properties("User ID") = "corporate\anonymous"
   objConnection.Properties("Password") = "amfam01"
   objConnection.Properties("Encrypt Password") = True
   objConnection.Open "Active Directory Provider"
   Set objCommand.ActiveConnection = objConnection
   objCommand.CommandText = "Select mail from 'LDAP://" & strDomain & "' where objectClass='user' AND objectcategory='person' AND sAMAccountName='" & username & "'"
   objCommand.Properties("Page Size") = 1000
   objCommand.Properties("Timeout") = 30
   objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
   objCommand.Properties("Cache Results") = False
   Debug.Print objCommand.CommandText
   Set objRecordSet = objCommand.Execute
   If objRecordSet.EOF Or objRecordSet.BOF Then
      MsgBox "No record found for " & userID & "!"
   Else
      Debug.Print "Email: " & objRecordSet.Fields("mail").Value
      objRecordSet.MoveNext
   End If
   getEmail = objRecordSet.Fields("mail").Value

End Function


Is This A Good Question/Topic? 0
  • +

Page 1 of 1