4

Firstly, I am new to these and the question could be stupid. Anyway, I have a procedure like this:

procedure Tform1.QueryChange(sqltext : String; query : Integer);
begin
if query = 1 then begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(sqltext);
ADOQuery1.Open;
end;
if query = 2 then begin
ADOQuery2.Close;
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Add(sqltext);
ADOQuery2.Open;
end;

I would like to remove the if blocks and make one united code:

ADOQuery+query.Close; (know that looks very silly)
ADOQuery+query.SQL.Clear;
ADOQuery+query.SQL.Add(sqltext);
ADOQuery+query.Open;

My goal is when query=1 code will use ADOQuery1.Close; etc. when query=2 code will use ADOQuery2.Close;

1
  • 1
    only from curiosity , why not QueryChange(const sqltext : String; query : TAdoQuery); ? Commented May 23, 2013 at 11:10

3 Answers 3

8

You could create a local variable that referred to the TADOQuery object that you wish to operate on. Like this:

var
  ADOQuery: TADOQuery;
begin
  if query=1 then
    ADOQuery := ADOQuery1
  else if query=2 then
    ADOQuery := ADOQuery2;
  ADOQuery.Close;
  ADOQuery.SQL.Clear;
  ADOQuery.SQL.Add(sqltext);
  ADOQuery.Open;      
end;
Sign up to request clarification or add additional context in comments.

Comments

6

Instead of creating variables ADOQuery1, ADOQuery2, ADOQuery3 etc of type TADOQuery, create an array:

ADOQueries: array of TADOQuery;

Then set the number of elements in it, when you know how many they will be:

SetLength(ADOQueries, NumberOfQueries);

Alternatively, if you know from the beginning how many elements there will be, you can define ADOQueries to be a static array instead:

ADOQueries: array[0..7] of TADOQuery;

Now you can do

procedure TForm1.QueryChange(sqltext: String; query: Integer);
begin
  ADOQueries[Query].Close;
  ADOQueries[Query].SQL.Clear;
  ADOQueries[Query].SQL.Add(sqltext);
  ADOQueries[Query].Open;
end;

8 Comments

If you want to talk about elegance, I'd draw attention to the repeated lookup of the array. I'd declare a local variable Query: TADOQuery and at the start of the method write Query := ADOQueries[Query]. Use Query from then on. That has the benefit that you do not repeat yourself.
Thank you kind sir. This was what I want. Now, I would like to check that if a particular query is already created or not. something like if (adoqueries[query] is not created) then create...
@user1899291 what you want is if ADOQueries[query] = nil then ADOQueries[query] := TADOQuery.Create(nil);.
If you call SetLength on an array, don't forget to later call SetLength(ADOQueries, 0) to free the array memory.
@DavidM No need, that happens when the array leaves scope
|
3

Another way you can do this is with the FindComponent method, ie assuming the form owns the query components

procedure Tform1.QueryChange(sqltext : String; query : Integer);
var cmp: TComponent;
    Query: TADOQuery;
begin
  cmp := FindComponent('ADOQuery' + IntToStr(query));
  if cmp <> nil then begin
     Query := cmp as TADOQuery;
     Query.Close;
     ...
  end;  
end;

3 Comments

But this is much less elegant than David's and my solutions. However, since there might be some academical interest in listing different solutions, I will not downvote.
I can't imagine doing it this way, but I'll upvote all the same. It's a perfectly reasonable answer. Normally you'd not want TADOQuery objects as published components on .dfm files and instead create them programmatically. But for a component that did naturally live in the .dfm file this is a sane way to do it.
@DavidHeffernan: I agree that you don't want these components on a form, and would create them in code my self, but in the drag-n-drop way of developing their natural habitat would be a data module and data modules are streamed just like forms are. So, if you are already using data modules, ain's solution would be a good way of doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.