Link to home
Start Free TrialLog in
Avatar of drtopserv
drtopservFlag for Israel

asked on

LDAP

Hi,
I have a code attached that is not working in my access vba .
may plz modify the code to get it work.
i need to do this:

I need to provide a ID in txtbox in a form and then perform a check in LDAP .
if this ID = SerialNumber" property in LDAP then msgbox "Found"
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

It would help if you tell us what you mean by "not working". Does it seem to run, but produces no result, or do you get an error, etc etc.

I would also caution you against dropping code here and instructing the VOLUNTEER Experts to "fix this". We're glad to help, but we're not your code monkeys.

Avatar of drtopserv

ASKER

LSMConsulting, first i`m glad that u helped me out many times:} around.

well, i have wrote in bold what i`m trying to do ,but still no luck;}
i need away to connect to ldap and retrieve "SerialName" property from it.
i search the expert-exchange and found the code that i attached ,but i can`t figure out how to let it work, at this time the gives me error  GetProperty variable not define.
should i add specfic reference to let it work?
or how can i perform the task?
sorry, not "Serialname" BUT "SerialNumber" property
You didn't attach your code to the question. Could you upload it?
Private Sub txtConsultantName_BeforeUpdate(Cancel As Integer)
Stop
Dim TempYoetzFirstName As String
Dim YoetzFirstName As String

Dim TempYoetzLastName As String
Dim YoetzLastName As String



TempYoetzFirstName = txtConsultantName.Value
If InStr(1, TempYoetzFirstName, " ") = 0 Then
MsgBox "! çåáä ìäæéï ùí îùôçä + ùí ôøèé", vbCritical + vbMsgBoxRight, "ùâéàä"
Cancel = True

Exit Sub
End If
YoetzFirstName = Left(TempYoetzFirstName, (InStr(TempYoetzFirstName, " ") - 1))
'MsgBox YoetzFirstName
TempYoetzLastName = txtConsultantName.Value
YoetzLastName = Right(TempYoetzLastName, (Len(TempYoetzLastName) - (InStr(TempYoetzLastName, " "))))
'MsgBox YoetzLastName
Dim strFilter: strFilter = "(&(givenname=" & YoetzFirstName & ") (sn=" & YoetzLastName & "))"
'Dim strFilter: strFilter = "(&(givenname=øåðéú) (sn=ãéðø))"
'MsgBox strFilter
Dim wscript As AccessObject
Dim objconnection: Set objconnection = CreateObject("ADODB.Connection")
objconnection.Provider = "ADsDSOObject"
'objconnection.Open "Active Directory Users and Computers"
objconnection.Open "ADs Provider"
Dim YoezCodeNumber As String
Dim ss As String
Dim SnifNumber As Long
Dim n As Boolean
Dim YoezCount As Long
'WaitWhileRunning (lngHWnd)
Dim objRootDSE: Set objRootDSE = GetObject("LDAP://mydomain/RootDSE")
Dim objRecordSet: Set objRecordSet = objconnection.Execute( _
  "<LDAP://" & objRootDSE.Get("dnsHostName") & ">;" & _
  strFilter & ";distinguishedName,name,sAMAccountName,SerialNumber,givenName,sn;subtree")

Set objRootDSE = Nothing

While Not objRecordSet.EOF
  ' Echoing the distinguished name, just in case
  'WScript.Echo objRecordSet.Fields("distinguishedName").Value
 ' ss = objRecordSet.Fields("distinguishedName").Value
  'MsgBox ss
  ' And the sAMAccountName
 
 
 
' YoezCodeNumber = objRecordSet.Fields("sAMAccountName").Value

 YoezCodeNumber = objRecordSet.Fields("SerialNumber").Value

Debug.Print YoezCodeNumber
here is a code that works for me for a user property like sAMAccountName for example.
but not for a user attribute SerialNumber.
YoezCodeNumber = objRecordSet.Fields("SerialNumber").Value

here it stop with error (run time error 13 - type mismatch) :
Debug.Print YoezCodeNumber
You could try to convert the number to a string using the cStr() function or the Format(xyz, "0") function


Debug.Print cStr(YoezCodeNumber )
sorry it stop in this line :
YoezCodeNumber = objRecordSet.Fields("sAMAccountName").Value
run-time error 13-type mismatch
Either cSTR or Format will solve the problem which is cause because you have declared YoezCodeNumber as a string. You will always get an error when you confuse a number with a string. Cstr will convert the number into a string.


YoezCodeNumber = cStr(objRecordSet.Fields("sAMAccountName").Value)
bro ,
this :  YoezCodeNumber = objRecordSet.Fields("sAMAccountName").Value  works
but
this :  YoezCodeNumber = objRecordSet.Fields("SerialNumber ").Value  doesn`t
 and i need the serialnumber attribute not the sAMAccountName property


The following will work:

YoezCodeNumber = cStr(objRecordSet.Fields("SerialNumber").Value)
nope this still  not working also:}
I tried your code and the error was "invalid use of null", not "type mismatch". Can be fixed by either dim YoezCodeNumber as Variant, or by YoezCodeNumber = nz(objRecordSet.Fields("SerialNumber").Value)  
I hope so,well i can`t check it rght now i should check at work to tomorrow, i`ll post the result until tomorrow .thnx
Yes vadimrapp1 found the correct message "invalid use of null", which is a real pain when handling recordsets.

You may not have a copy of the nz function. I have included some functions that can be useful when handling recordsets.

YoezCodeNumber = Cstr(GitNum(objRecordSet.Fields("SerialNumber").Value) )

Git() will allow you to transfer a recordset field that has a null value a null value will be converted to a blank string.
GitNum() will do the same but a null value will be converted to a zero.


Hope this helps:~)


Public Function Git(RecordsetField)

' Use this function when accessing ALL DAO/ADO Recordset fields
' To avoide ilegal use of Null error
' Example: MsgBox gf.git(RS("MyField"))
' this function was named after the programmer that thought it would be a good idea to make a program crash because of nothing.

If IsNull(RecordsetField) Then
    Git = ""
ElseIf IsEmpty(RecordsetField) Then
    Git = ""
Else
    Git = RecordsetField
End If

End Function

Public Function GitNum(RecordsetField)

' Force a null field to be numeric

If IsNull(RecordsetField) Or IsEmpty(RecordsetField) Then
    GitNum = 0
Else
    GitNum = RecordsetField
End If

End Function
Not good , still getting type mismatch :{ 13
Just before this line:

YoezCodeNumber = Cstr(GitNum(objRecordSet.Fields("SerialNumber").Value) )

Can you insert the following:

MsgBox "VarType=" + Cstr(VarType(objRecordSet.Fields("SerialNumber").Value))

When you run the program it will come up with a message box say what the VarType of the recordset element.

If you post back the answer we can have a think about the result.

Alter today I will be at a site with an AD server so I will be able to run the code myself.
var type = 8204
The variable type indicates a variant array which is fairly rare to find in a recordset. It could perhaps indicate a byte array perhaps.


I will take a look later when I am at a site with AD server.
I suppose we could see what is in the array:


MsgBox "ArraySize=" + Cstr(Ubound(objRecordSet.Fields("SerialNumber").Value))

and then

MsgBox "Element 0 VarType=" + Cstr(VarType(objRecordSet.Fields("SerialNumber").Value(0)))


MsgBox "ArraySize=" + Cstr(Ubound(objRecordSet.Fields("SerialNumber").Value))
got this:
arraysize=0
MsgBox "Element 0 VarType=" + Cstr(VarType(objRecordSet.Fields("SerialNumber").Value(0)))
got this : error 450
you can do better than debug.print: break the code, then in VBA window, open View/Locals, and examine the object of interest, like at the picture. If field.value has its own structure (which is why it can't be printed), you will be able to expand it further. User generated image
wow, there is some good now, i can see in the local window when i step the code until i reach the line :
YoezCodeNumber = objRecordSet.Fields("SerialNumber ").Value  
it give me in local window:
YoezCodeNumber(0)   and value = "123456789"
when i step after to the line :
msgbox YoezCodeNumber
it gives me the error "type mismatch"-13
how comes?? when the dim YoezCodeNumber  as variant ????
ASKER CERTIFIED SOLUTION
Avatar of Vadim Rapp
Vadim Rapp
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perfecto!!!
vadimrapp1, thnx u ALOT for  helping me out solving this problem :} here.

vadimrapp1, me u plz guide me to link/stuff that can help me out as bigenner in advaced debugging topics and the arrays issues.
i`ll be very glad.
u got my 500 points:}
> advanced debugging topics

There's not much more; the only remaining thing related to debugging is what's called a "watch", under menu "debug". You specify the variable of interest, and then the code will break whenever the variable's value is changed.

Array issues - if you want to move in that direction, look at Visual Studio.Net, it's fully object-oriented, and there are tons of stuff to learn.

...also, grab a book in your local library about the tool you are using. Many developers are self-learners, learning as they go and encounter problems, and it's really eye-opening experience when at some point you open a book and suddenly realize how many things were just around the corner, while you were wasting time on useless struggles and investigations for years.
thnx alot for ur advice:}