We need to know which line raises the error. Again, I don't see a problem in
the code. One possibility is that the value for "Provider" passed to the
function is invalid. However, I don't understand the purpose of this
variable. I also am not sure of the purpose of the function.
If the purpose is to determine the Distinguished Name of a computer given
the NetBIOS name, there are easier ways to do this. I would recommend using
the NameTranslate object. I also would use the RootDSE object to determine
the name of the current domain (as I pointed out earlier).
I assume the value of sUserName is the NetBIOS name of a computer because
you append a trailing "$" to the value and search for a computer object
whose sAMAccountName matches. The code to retrieve the DN of a computer from
the NetBIOS name could be as follows:
================
Private Function FindHostPosition(ByVal strComputer As String) As String
' Function to determine Distinguished Name of computer from NetBIOS
name.
Dim objTrans As NameTranslate
Dim objRootDSE As Object
Dim strDNSDomain As String
Dim strNetBIOSDomain As String
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
' Determine DNS name of domain from RootDSE.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use the NameTranslate object to find the NetBIOS domain name from the
' DNS domain name.
Set objTrans = CreateObject("NameTranslate")
' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove trailing backslash.
strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)
' Use the Set method to specify the NT format of the object name.
' The sAMAccountName of computer objects is the NetBIOS name
' with a trailing "$" appended.
' Trap error if object does not exist.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strComputer &
"$"
If (Err.Number = 0) Then
On Error GoTo 0
' Use the Get method to retrieve the RPC 1779 Distinguished Name.
FindHostPosition = objTrans.Get(ADS_NAME_TYPE_1779)
Else
On Error GoTo 0
' Object not found.
FindHostPosition = "<Not found>"
End If
End Function
==========
The NameTranslate object requires a reference to "Active DS Type Library"
(activeds.tlb). Using NameTranslate is more efficient than using ADO to
search for the object. However, I believe your code will work if the proper
values are passed to it.

Signature
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
> We need to know which line raises the error.
I'll some more debug-lines and ask the customer to reproduce the issue.
> Again, I don't see a problem in the code.
Nor do I. The code runs fine on hundreds of PCs, but this customer has
already four machines that run into this issue.
> One possibility is that the value for "Provider" passed to the
> function is invalid.
No. "Provider" can only be "GC:" or "LDAP:", I tried both with the same
error.
> However, I don't understand the purpose of this variable.
The purpose of this variable was to be able to either use GC or LDAP
with the same function.
> I also am not sure of the purpose of the function.
I need to find the correct full LDAP path of the machine.
> If the purpose is to determine the Distinguished Name of a computer given
> the NetBIOS name, there are easier ways to do this.
To be honest: It's been a while since I last had my fingers in this area
of the code. A customer is complaining about issues and I found this in
the logs and tried to narrow it down a bit. What I want to say is: I
think you are right that I'm just trying to get the DN of a computer.
But I'm not sure if I haven't done it this way for a reason.
> I would recommend using the NameTranslate object.
I'll give that a try.
> I also would use the RootDSE object to determine
> the name of the current domain (as I pointed out earlier).
I read that but I can assure you that this was no issue yet. We
shouldn't forget that the code is working on a few hundred machines
without any trouble.
> The code to retrieve the DN of a computer from the NetBIOS name
> could be as follows:
[...]
No matter what the reason for the issues in my code might be, I'm going
to give this a shot anyway.
> However, I believe your code will work if the proper
> values are passed to it.
I had the values printed out in the debug log and can assure you they
are correct. It must some form of incompatibility with something on the
customers machines. But maybe that's not much of importance anymore is
your code suggestion does the job. I'll try tomorrow.
Thanks a lot for your help!
Regards,
T.
Richard,
thank you so much!
I added the function that you proposed as fallback in case my function
does not work properly. It worked perfectly in the environment that had
caused the issues.
Best regards,
T.