Solved

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

Posted on 2001-07-20
9
250 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now