Solved

Search in a DB HELP!!!

Posted on 1997-04-23
4
145 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
ID: 1335642
Better removing this question if you already have one.
0
 

Expert Comment

by:wizzard
ID: 1335643
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
ID: 1335644
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
ID: 1335645
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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