Solved

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

Posted on 2001-07-20
9
248 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

27 Experts available now in Live!

Get 1:1 Help Now