Solved

Search in a DB HELP!!!

Posted on 1997-04-23
4
148 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LAN or WAN ? 11 95
how to update exe applicatio from internet ? 6 86
PHP preg_replace code convert to Delphi 14 66
DBCtrlGrid, Delphi, Scroll 8 37
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

808 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