Board index » delphi » Problem #2: SQL question

Problem #2: SQL question

I have an SQL that I want to take a string that would be entered into an
editbox by the user and search the table to see if that string exists in
the table.  Question: Can a T String in the SQL component be written so
that it can use the string from the editbox component?

I've been trying to use the following string to no avail:

select * from ntrition where ntrition.'food name' like :vartext1

Thanks for your help.

John W. Gilmer

 

Re:Problem #2: SQL question


Quote
warptwe...@aol.com (WarpTwelve) wrote:
>I have an SQL that I want to take a string that would be entered into an
>editbox by the user and search the table to see if that string exists in
>the table.  Question: Can a T String in the SQL component be written so
>that it can use the string from the editbox component?
>I've been trying to use the following string to no avail:
>select * from ntrition where ntrition.'food name' like :vartext1
>Thanks for your help.
>John W. Gilmer

shouldn't be a problem.
Check how you're accessing :vartext1
TQuery.ParamByName('vartext1').AsString := myEdit.Text;
-----------------------------------------------------------------------------
heres a snippet from one of my prog.'s (works fine)
-----------------------------------------------------------------------------
  try
    {search the catalog for an item}
    if (rbnPartNo.Checked) then
      FmtStr(theSQL, LoadStr(515), [LoadStr(555), edtFind.Text])
[snipped]
    QSearch.SQl.Clear;
    QSearch.SQl.Add(theSQL);
    QSearch.Open;
    if (QSearch.RecordCount > 0) then
    begin
      if not (tblCatalog.FindKey([QSearch.Fields[0].AsInteger])) then

I have 3 radio buttons, 1 edit feild.
Users can enter text, and search a field depending on the radio button
choice.
Oops. nearly forgot the actual string resource thats loaded ..
This is the string resource #515
SELECT CATALOG.COUNTER FROM CATALOG WHERE CATALOG.%s = '%s'
resource #555 is the field name I want to search on
COUNTER is an autoincrement field, I'm checking that there's a record
with a corressponding field match, so there must be an entry in the
autoinc. field

Hope this helps ....

Re:Problem #2: SQL question


Quote
WarpTwelve wrote:

> I have an SQL that I want to take a string that would be entered into an
> editbox by the user and search the table to see if that string exists in
> the table.  Question: Can a T String in the SQL component be written so
> that it can use the string from the editbox component?

> I've been trying to use the following string to no avail:

> select * from ntrition where ntrition.'food name' like :vartext1

> Thanks for your help.

> John W. Gilmer

How are you assigning the Parameter??

Does the addition of "%" help with the search?

Does it work with literal text??

Matthew
--

==================================================================
Matthew McDermott
McDermott Enterprises
matt...@valleynet.com
My Web Page: http://www.valleynet.com/~matthew
------------------------------------------------------------------
Director of Information Services & Adjunct Professor - Mathematics
The American College of Prehospital Medicine: http://www.acpm.edu/
==================================================================

Re:Problem #2: SQL question


Quote
WarpTwelve wrote:

> I have an SQL that I want to take a string that would be entered into an
> editbox by the user and search the table to see if that string exists in
> the table.  Question: Can a T String in the SQL component be written so
> that it can use the string from the editbox component?

> I've been trying to use the following string to no avail:

> select * from ntrition where ntrition.'food name' like :vartext1

> Thanks for your help.

> John W. Gilmer

Just put in your source code:

exp1:=edit1.text;
query1.active:=false;
query1.parambyname('varedit').asstring:=exp1;
query1.active:=true;

Jack

BTW can you tel me how to print a dbgrid selected filed
Thanks

Re:Problem #2: SQL question


In <31949C07.4...@valleynet.com> Matthew McDermott

Quote
<matt...@valleynet.com> writes:

>WarpTwelve wrote:

>> I have an SQL that I want to take a string that would be entered
into an
>> editbox by the user and search the table to see if that string
exists in
>> the table.  Question: Can a T String in the SQL component be written
so
>> that it can use the string from the editbox component?

>> I've been trying to use the following string to no avail:

>> select * from ntrition where ntrition.'food name' like :vartext1

>> Thanks for your help.

>> John W. Gilmer

>How are you assigning the Parameter??

>Does the addition of "%" help with the search?

>Does it work with literal text??

>Matthew
>--

>==================================================================
>Matthew McDermott
>McDermott Enterprises
>matt...@valleynet.com
>My Web Page: http://www.valleynet.com/~matthew
>------------------------------------------------------------------
>Director of Information Services & Adjunct Professor - Mathematics
>The American College of Prehospital Medicine: http://www.acpm.edu/
>==================================================================

There are several ways to write the code - all with the same result.
You will attach any of the following code to - say a button.

1.  Query1.Params[0].asString := Edit1.Text;
    Query1.SQL.Clear;
    Query1.SQL.Add('select * from ntrition');
    Query1.SQL.Add('where foodname like %:vartext1%');
    Query1.Open;

2.  Query1.SQL.Clear;
    Query1.SQL.Add('select * from ntrition');
    Query1.SQL.Add('where foodname like %' + Edit1.Text + '%';
    Query1.Open;

3.  Query1.SQL.Clear;
    Query1.SQL.Add('select * from ntrition');
    Query1.SQL.Add(format('where foodname like %"%s"', [Edit1.Text]));
    Query1.Open;

You should consult LocalSQL.hlp in the Delphi\bin directory or
SQLRef.Hlp in iblocal\bin directory.  I hope this helps.

Ben
Arrow

Other Threads