• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

Search in a DB HELP!!!

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
940216td
Asked:
940216td
1 Solution
 
javiertbCommented:
Better removing this question if you already have one.
0
 
wizzardCommented:
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
 
acemanCommented:
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
 
jmstoltingCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now