Solved

Search in a DB HELP!!!

Posted on 1997-04-23
4
139 Views
Last Modified: 2010-04-04
I need to write a program to enroll students. I need to search a student on name, lastname and number. In my IndexFieldNames of my table stands the following:
number;name;lastname
My statements are
Setkey;
FieldByName('Name').AsString := Edit.Text;
Gotokey;
However, it doesn't seems to work. how can I solve this. An earlier answer I got was to put an index on name but I think I did. If i didn't How do I this.HELP!HELP!HELP!HELP!HELP!
IT'S URGENT. Thanx to everyone who can help me, please???
0
Comment
Question by:940216td
4 Comments
 
LVL 2

Expert Comment

by:javiertb
Comment Utility
Better removing this question if you already have one.
0
 

Expert Comment

by:wizzard
Comment Utility
Just one thing that I need to know before solving your problem: what type of DB file are you using? I can send you an example program that will search for asked things ina DB file!

      C ya,
         WizzarD.
0
 

Accepted Solution

by:
aceman earned 50 total points
Comment Utility
function Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions): Boolean;

This is a clip from the Delphi help screen on the LOCATE method of a TTable component. Although the definition of the function includes the constant KeyFields the actual string listed does NOT have to be a KEY field in a database (thereby necessitating a primary key index). I have used this function to locate field values in many un-indexed, un-keyed tables regardless of the data table type. I have not tried this with tables other than local tables (dBase and Paradox are my usual tables so if you are using Oracle or SQL tables, this function may not apply) but give it a try. Implementation can be made as such

{this assumes the value to search is a text value}
var
  srchTxt:string;
begin
  srchTxt:=Edit1.Text;{get name / number to search for}
  if not table1.locate('StudentName',srchTxt,[loCaseInsensitive]) then
     {Student not located in table}
     ShowMessage('Unable to locate this student')
  else
     {show edit form, do something here, etc}

Alternately, you can use [loPartitialKey] instead of [loCaseInsensitive] will allow you to display partial string matches (e.g. HAM will display HAMMOND, HAMMERMILL, HAMILTON, etc).

You could also perform a query on a partial match, display the results in a dbGrid component, allow the user to click on the correct search, capture the student number in a variable, close the query, move back to the actual data table and use the LOCATE method to move the table cursor to that correct record (which WILL be found since you got it from that source to begin with) I like to allow both in my applications. If the user KNOWS the EXACT identifying number use it, otherwise, show me all possibilities starting with ??? (use HAM example) query the table with this EDIT1.TEXT value and do the above suggested choose and find procedures.

I hope this gives you the direction you need to accomplish your tasks!

Bill
0
 

Expert Comment

by:jmstolting
Comment Utility
keep in mind that when you use gotokey, the search is performed on all the key fields. in your code you only assign the 'name' field, which will most likely generate no results because the index also contains the fields 'lastname' and 'number'.

if you want to search on only the 'name' field, use an index which only contains the 'name' field, or use KeyFieldCount
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

11 Experts available now in Live!

Get 1:1 Help Now