VB Searching Random Access Data Files

Posted on 2005-04-22
Last Modified: 2010-05-02

I have a type - what_done, that i have declared as cinfo.
I want to have a user type into text1, click Search (sv1) and then searches through a random access data file (data.vcr) and tries to find a record in the database so that cinfo.verb = text1.text.

I have heard about indexing and binary searching but I cant understand them very well...

Could someone help me with one of these?
PS: There will only be one result of the search.
Question by:qcl0wn
    LVL 85

    Accepted Solution

    (1) Indexing - Basically you have a secondary file which is the index file.  Each index file represents a different field from your record, which in your case is "verb".  In the file, you store two pieces of information per record: the value of verb and the byte location (or record number) of the location in the main database file where that value can be found.  The advantage of the index file is it takes less time to search thru and/or move/update records in the index file involving only those two pieces of information as opposed to reading a much larger database record potentially involving many more fields per record.

    (2) Binary Searching - This technique can be used in an index file or directly in the databse itself.  The key though is that the records must be sorted first.  Basically you start in the middle of the file and check the value there.  It if matches what you are looking for then you are done.  If it doesn't, then you only have two choices, left or right.  Since the file is sorted, if the value you are looking for is less than the value in the middle then the value you want is somewhere in the left half of the file...otherwise it must be in the right half.  So then you take the appropriate half and look at the middle element of that half.  You repeat the process until you have found the item you are looking for or you only have one element left in your "half" that you are currently which case the value you desire is not in the file.  So it is called a "Binary Search" because each time you narrow the available records to search by half.
    LVL 1

    Author Comment

    Could you give me an example of the binary searching/indexing?

    I have:

    Private Type what_done

    verb as string * 100
    sub_act as string * 100
    min as integer

    End Type

    and the database file is data.dat.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Approximately how many records are in your database?

    Rather than reinventing the wheel, have you considered using a database system that already has this capability?  They are optimized for this kind of searching and sorting.

    I can certainly give you code on how to do this but I can guarantee that it won't be very optimized.  Database systems are complex beasts...
    LVL 1

    Author Comment

    Actually, I found the example code in another place (your explanations helped so much!)...

    I just need to know how I can load all records into a listbox (list1).  Could you help me with this (get me a code)?



    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Suggested Solutions

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now