INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Get record number of longest record

Get record number of longest record

(OP)
How can I retrieve the record number of the longest record in a DBF file?

Ex. -

Joe Doe
Long Name Doe
Jane Doe

I would like to find Long Name Doe and that it is record 2 in the DBF file.  Any ideas?

Swi

RE: Get record number of longest record

In languages that support indexes of dbfs
you could

CODE

index on len(trim(fieldname)) tag indextagname
goto bottom
?recno()

Unless there is a tie.
wjwjr

This old world keeps spinning round - It's a wonder tall trees ain't layin' down

RE: Get record number of longest record



ORDER BY len([TheField) Desc"

Then, if you want the current absolute position of the record, get the value of the ID field (or use the value of the same field used in the ORDER BY) and do another SELECT using that value as criteria on that same field, but un-sorted.

RE: Get record number of longest record

(OP)
How would I integrate your above suggestion into the following query?  I am used ADO.

CODE

rs.Open "SELECT [NAME] FROM [TESTIT.DBF] WHERE LEN([NAME]) = (SELECT MAXLEN([NAME])) FROM [TESTIT])", conn, adOpenForwardOnly, adLockReadOnly, adCmdText

Swi

RE: Get record number of longest record

I haven't worked with dbf's in about 10 years, so I'm not confident this will work, but it is worth a try.


rs.Open "SELECT Top 1 [NAME] FROM [TESTIT.DBF] Order By LEN([NAME]) DESC", conn, adOpenForwardOnly, adLockReadOnly, adCmdText

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: Get record number of longest record

(OP)
Thanks.  How then would I retrieve the record number from the query results?

Swi

RE: Get record number of longest record

Since I applied the Top 1 operator, there will only be 1 record.  So... the record number is 1.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: Get record number of longest record

(OP)
Yes, but I would like to know where that record falls in the DBF I am selecting it from.

Swi

RE: Get record number of longest record

>How would I integrate your above suggestion into the following query?  I am used ADO.

Take the ORDER BY I mentioned onto the first SELECT. Remove the criteria

Dim lPos ás long
Dim sKey  as string
 
rs.Open "SELECT [NAME] FROM [TESTIT.DBF] ORDER BY LEN([NAME]) Desc", conn, adOpenForwardOnly, adLockReadOnly, adCmdText

If (Not (rs.Eof And rs.BOF)) then
   sKey = rs.Collect(0)
end if

rs.Close

rs.Open "SELECT [NAME] FROM [TESTIT.DBF]",conn, adOpenForwardOnly, adLockReadOnly, adCmdText

If (Not (rs.EOF And rs.BOF)) Then
   Call rs.find("NAME='" & sKey & "'", , adSearchForward, adBookmarkFirst)
   If Not rs.EOF Then lPos = rs.AbsolutePosition
End If

Debug.? lPos

AbsolutePosition is not really of any good use in this form.
What is the "record number"? A Field name, the AbsolutePosition with out indexes considered, or with, or is it based on some order?

RE: Get record number of longest record

(OP)
It is an AbsolutePosition with out indexes considered.  The above code works but I am worried about the performance hit due to the fact that I could be returning up to a million records.

Swi

RE: Get record number of longest record

Then use in the first SELECT additionally

 SELECT TOP 1

RE: Get record number of longest record

Also, use a server side cursor (rs.CursorLocation) if there are many records

RE: Get record number of longest record

(OP)
Thank you for the tip.  Your help is much appreciated.

Swi

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close