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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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