Select Max Record with ADO

S

Scott

I'm trying to get the MAX() date in a field using an ADO call. I'm getting
the below ERROR. The field and table name are correct. I even tried other
fields from this and other tables. Am I using the wrong CursorType?


ERROR **********
Item cannot be found in the collection corresponding to the requested
name or ordinal

CODE ***********

Public Function GetMaxDataDate()

Dim objRS As ADODB.Recordset, sSQL As String

Set objRS = New ADODB.Recordset

objRS.CursorType = adOpenDynamic
objRS.LockType = adLockOptimistic
sSQL = "SELECT MAX(prod_date) FROM tblDate"
objRS.Open sSQL, CurrentProject.Connection

GetMaxDataDate = objRS!prod_date
Debug.Print objRS!prod_date
objRS.close
Set objRS = Nothing

End Function
 
R

RoyVidar

Scott said:
I'm trying to get the MAX() date in a field using an ADO call. I'm
getting the below ERROR. The field and table name are correct. I even
tried other fields from this and other tables. Am I using the wrong
CursorType?


ERROR **********
Item cannot be found in the collection corresponding to the
requested name or ordinal

CODE ***********

Public Function GetMaxDataDate()

Dim objRS As ADODB.Recordset, sSQL As String

Set objRS = New ADODB.Recordset

objRS.CursorType = adOpenDynamic
objRS.LockType = adLockOptimistic
sSQL = "SELECT MAX(prod_date) FROM tblDate"
objRS.Open sSQL, CurrentProject.Connection

GetMaxDataDate = objRS!prod_date
Debug.Print objRS!prod_date
objRS.close
Set objRS = Nothing

End Function

Either try aliasing

sSQL = "SELECT MAX(prod_date) as MyMaxDate FROM tblDate"
....
Debug.Print objRS!MyMaxDate

or just use the first field of the recordset

objRS.Fields(0).value
 
S

Scott

that did it, thanks.

RoyVidar said:
Either try aliasing

sSQL = "SELECT MAX(prod_date) as MyMaxDate FROM tblDate"
...
Debug.Print objRS!MyMaxDate

or just use the first field of the recordset

objRS.Fields(0).value
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top