Avatar of drtopserv
drtopserv
Flag 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"
Microsoft AccessMicrosoft OfficeVisual Basic Classic

Avatar of undefined
Last Comment
drtopserv

8/22/2022 - Mon
Scott McDaniel (EE MVE )

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.

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?
drtopserv

ASKER
sorry, not "Serialname" BUT "SerialNumber" property
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Scott McDaniel (EE MVE )

You didn't attach your code to the question. Could you upload it?
drtopserv

ASKER
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
drtopserv

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
inthedark

You could try to convert the number to a string using the cStr() function or the Format(xyz, "0") function


Debug.Print cStr(YoezCodeNumber )
drtopserv

ASKER
sorry it stop in this line :
YoezCodeNumber = objRecordSet.Fields("sAMAccountName").Value
run-time error 13-type mismatch
inthedark

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)
Your help has saved me hundreds of hours of internet surfing.
fblack61
drtopserv

ASKER
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

inthedark


The following will work:

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

ASKER
nope this still  not working also:}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Vadim Rapp

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)  
drtopserv

ASKER
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
inthedark

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
drtopserv

ASKER
Not good , still getting type mismatch :{ 13
inthedark

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.
drtopserv

ASKER
var type = 8204
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
inthedark

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.
inthedark

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)))


drtopserv

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Vadim Rapp

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. screenshot
drtopserv

ASKER
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
Vadim Rapp

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
drtopserv

ASKER
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:}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Vadim Rapp

> 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.

Vadim Rapp

...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.
drtopserv

ASKER
thnx alot for ur advice:}
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck