If No Record Found

jjpski

New Member
Joined
Jan 4, 2011
Messages
29
Hey,

I have a problem with the following code. It does almost everything I need except when a user account is not found it puts nothing in column A. I tried isEmpty IsNull but maybe I am not putting them in the right place. Any help is always appreciated.


With ActiveSheet
Lastrow = .Cells(.Rows.Count, "G").End(xlUp).Row
End With


Set rootDSE = GetObject("LDAP://RootDSE")
Base = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"

Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn

'add other attributes according to your requirements
attr = "distinguishedName,sAMAccountName"
scope = "subtree"

For R_counter = 2 To Lastrow
UserName = Trim(Range("G" & R_counter).Value) & Trim(Range("H" & R_counter).Value)

'filter on user objects with the given account name
fltr = "(&(objectClass=user)(objectCategory=Person)" & _
"(sAMAccountName=" & UserName & "*))"

cmd.CommandText = Base & ";" & fltr & ";" & attr & ";" & scope
Set rs = cmd.Execute
Do Until rs.EOF

Range("A" & R_counter).Value = rs.Fields("distinguishedName").Value

rs.MoveNext
Loop
rs.Close
Next R_counter
conn.Close


v/r
Jared
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Jared

I think you might need to check rs.EOF, if that is True then no records have been returned to the recordset and the code in this loop will never be executed.
Code:
Do Until rs.EOF
    Range("A" & R_counter).Value = rs.Fields("distinguishedName").Value

    rs.MoveNext
Loop
 
Upvote 0

Forum statistics

Threads
1,215,324
Messages
6,124,250
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top