Solved

What is the best way to show records in dbgrid, do sql query and modify the records?

Posted on 2001-07-20
9
251 Views
Last Modified: 2010-04-06
Hi all,
I have an event database.
When user open the mainform, I want all the records show in a "dbgrid". Then the user can select some conditions to do a sql query, the query result show in the "dbgrid", and they can modify some fields.

What is the best way to do that? What is the best component to do that? Please give me some
advice.  Thanks.

(I found a component, it name is xRxDBGrid. xSortRxDBGrid is an RxDBGrid (from RxLib) with automatically data sorting when user clicks on field's title button. Support for TRxQuery and
TTable-based grids.   http://deks.webjump.com/)

Regards, supwang
0
Comment
Question by:SupWang
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6302908
hi supwang,

use the TUpdateSQL-Component as Update-Object of your TQuery (property UpdateObject)
fill out the the Update, Insert, Delete Statements in TUpdateSQL
(there is an expert, if you double click on it)
set the cachedupdates property to true of the TQuery
don't forget to call applyupdates, maybe in the afterpost and afterdelete-event of the TQuery

meikl ;-)
0
 
LVL 3

Expert Comment

by:ahll
ID: 6303860
Hi,

You can use a TTable component and the filter property, if you filter the table the grid relationed to its datasource will show only the records that applies this filter.

please excuse my english.

bye  
0
 
LVL 2

Author Comment

by:SupWang
ID: 6304036
Hi meikl,
I am not go to here for a long time. Your points stride over rwilson now. :-)
I will view the help of TUpdateSQL-Component. Please give me more advice.  

Hi ahll,
I want to use sql. Because the query will be very complicated, and may have many records.

Regards, supwang
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 9

Accepted Solution

by:
ITugay earned 100 total points
ID: 6304542
Hi SupWang,

meikl :)

easy way to modify SQL statement is:

procedure TForm1.Query1BeforeOpen(DataSet: TDataSet);
var
  S: string;
begin
  S := 'select * from groups where group_name like "E1%" and user_name like "E2%"';
  S :=  StringReplace(S,   'E1',  Edit1.Text, [rfReplaceAll]);
  S :=  StringReplace(S,   'E2',  Edit2.Text, [rfReplaceAll]);
  Query1.SQL.Text := S;
end;

if user type nothing in TEdit1 then all recods will be shown. If user type some text then after reopenning Query1, dbgrid will keep only records where field "group_name" starts from typed text.

------
Igor
0
 
LVL 3

Expert Comment

by:rondi
ID: 6304615
You can use a regular TQuery and TDBGrid/TRxDBGrid.
The TQuery's RequestLive property should be set to True to let
users modify the data.
Then use queries with parameters
 eg.
const
  AT_LEAST_QUERY = "SELECT * FROM MyTable WHERE MyAmount >= :pAmount"

Then when the user exits the TEdit or clicks a "Filter"
button:

with Query1 do    //where Grid's datasource is Query1
begin
  DisableControls;
  try
    if Active then Close;  
    SQL.Clear;
    SQL.Add(AT_LEAST_QUERY);
    ParamByName("pAmount").AsInteger := StrToInt(Edit1.Text);
    Open;
  finally
    EnableControls;
  end;
end;

//-------------------------------------------------

This approach works for me. I just declare all this query
constants and fill the parameters later with whatever I want. The queries can get as complex as you want. I've
had a few with multiple nested SELECT's
eg. SELECT ... WHERE ... IN (SELECT ... WHERE ... IN (SELECT ... WHERE ... = ...))

All you really need to be wary of is the parameter types
so you use the right As... property, esp. with dates.

HTH,
rondi
0
 
LVL 2

Author Comment

by:SupWang
ID: 6304804
Hi meikl,
About the TUpdateSQL, it seems that it just be use when cached updates are enabled. But I don?t need to set the cached updates to true. How about I just set the TQuery's RequestLive property to True?

Hi Igor,
I like your comment. :-)  


Please give more advice.

Thanks, supwang
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6304954
yup, TUpdateSQL do only work,
if the TQuery cached updates are set,
but this may not cause a problem

the requestlive Porperty may not work under some conditions,
which depends on the database, which is used

meikl ;-)
0
 
LVL 2

Author Comment

by:SupWang
ID: 6343284
Hi,
Sorry, very busy in this time.
Thanks all.
0
 
LVL 2

Author Comment

by:SupWang
ID: 6678550
Hi meikl,
What time will need the TUpdateSQL?
If I just use some simple Query and the requestlive property can be true then I don't need the TUpdateSQL. Is it?
And could you give me a very simple example about the TUpdateSQL? (Please don't let me to view the delphi demo)

Regards, supwang
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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