Binary Search Routine For RDO

Binary Search Routine For RDO

RDO does not have a FindFirst or Seek method, and as a programmer, you sometimes need to quickly move to a particular record. I had a user who wanted to be able to scroll through more than 3,000 records and also be able to search for a particular record. A linear search was too slow, so I decided to write a binary search routine. The routine is case-insensitive and finds the matching entry. It can be copied into the form module and used for searches on any sorted column for a given resultset. It takes three arguments: the rdoResultset being searched, the column name within the resultset to be searched, and the search string:

 Private Sub BinarySearch(ByRef rs _	As rdoResultset, ByVal strColName As _	String, ByVal varSearch As Variant)	Dim lngFirst&, lngLast&, varBookMark	varBookMark = rs.Bookmark 'set a bookmark	lngLast = rs.RowCount	If lngLast = 0 Then Exit Sub	lngFirst = 0	rs.AbsolutePosition = (lngLast - lngFirst) _		 2 'move to middle	varSearch = Trim(UCase(varSearch))	Do While ((lngLast - lngFirst)  2) > 0		Select Case StrComp(UCase(Left(Trim _			(rs.rdoColumns(strColName)), _			Len(varSearch))), varSearch, vbTextCompare)			Case 0 'found				Exit Sub			Case -1 'still ahead				lngFirst = rs.AbsolutePosition				rs.Move (lngLast - lngFirst)  2			Case 1 'left behind				lngLast = rs.AbsolutePosition				rs.Move (-1) * ((lngLast - lngFirst)  2)		End Select	Loop	rs.MoveLast 	If (StrComp(UCase(Left (Trim(rs.rdoColumns(strColName)), _		Len(varSearch))), varSearch, _		vbTextCompare)) <> 0 Then		' record not found. Return to bookmark and 		' display message.		rs.Bookmark = varBookMark 		MsgBox "Entry not found for " _			& varSearch, vbOKOnly Or _			vbInformation, "Binary Seacrh"	End IfEnd Sub

Because this search always misses the last item, the last record is checked specifically. Also, this routine finds the last matching record if the record is positioned before the middle of a resultset, or the first matching record if the record is positioned after the middle of a resultset. For example, say there are three Smith’s?A. Smith, B. Smith, and C. Smith?in a resultset and the user is searching by last name for Smith. Also assume there are 100 records in the resultset. If C. Smith is at absolute position of 25 (< 50 = 100/2), then this search routine finds C. Smith first. However, if A. Smith has an absolute position of 59 (>50 = 100/2), then this search routine finds A. Smith first. In case the Smiths happen to be somewhere in the middle, this search routine finds the first Smith encountered. This routine works best for searching on unique keys, such as Social Security numbers.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes