tags:

views:

66

answers:

4

I have a very simple query that only returns one record. When I try to get the value out of the only column in the only record, I get "Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record." What's going on here? The code that is causing the error doesn't even execute if RecordCount is 0 and I have verified that the recordset does in fact contain a record.

Code is below. Error is thrown when trying to set strDN. It's so dead simple but I can't figure out where I'm going wrong.

EDITED TO INCLUDE COMMAND

<LDAP://DC=something,DC=com>;(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(employeeID=01234567));distinguishedName;subtree

Set adoRecordset = adoCommand.Execute


    If adoRecordset.RecordCount > 0 Then


        strDN = adoRecordset.Fields("distinguishedName").Value

        Set objUser = GetObject("LDAP://" & strDN)

        objGroup.add(objUser.ADsPath)

    End if
A: 

-EDIT- Have a look at the following link. There are some causes listed, and solutions for most of them:

http://classicasp.aspfaq.com/general/why-do-i-get-bof-or-eof-errors.html

[I was wrong about this - thanks, Dave] I believe you need to call adoRecordset.MoveNext (or whatever the call is) before attempting to get the value of a field in the recordset.

Jason
If I do that I get the same error, it's just thrown on the adoRecordset.MoveNext instead of when I set strDN
res
Dave
+1  A: 

I use

If Not adoRecordset.EOF And Not adoRecordset.BOF Then
...
End If

For This Scenario

Dave
This is probably the best way to check if at least one record is found. The RecordCount property behaves differently depending on the cursor type.
Cheran S
Well, I have absolutely no idea why it works, but if I use BOF and EOF instead of RecordCount to check for the presence of a record, everything works fine. Thanks for your help!
res
see remou's answer as to why this fixes the issue.
Jason
A: 

Try

Set adoRecordset = adoCommand.Execute    

    If adoRecordset.RecordCount > 0 Then

        adoRecordset.MoveFirst 'Move to the first record

        strDN = adoRecordset.Fields("distinguishedName").Value

        Set objUser = GetObject("LDAP://" & strDN)

        objGroup.add(objUser.ADsPath)

    End if
Tester101
I have tried this but I get the same error, it's just thrown on adoRecordset.MoveFirst
res
@res: What type of cursor are you using? See Remou's answer. You have to be using a forward and backward moving cursor.
Tester101
+3  A: 

The recordcount property leaves the cursor at the end of the recordset, so you cannot then obtain the record (eof=true), you must movefirst. Use a different cursor type, because the default cursor type is forward only:

'' Assign cursorType that allows forward and backward movement.
adoRecordset.cursorType = 3 ''adOpenStatic

-- http://www.w3schools.com/ado/prop_rs_cursortype.asp

Remou