tags:

views:

72

answers:

3

I am working on converting BDE to ADO for my company. I have run into an expression I can not figure out how to convert to ADO

Here is the BDE expression with all the ADO changes except for one....the part that is causing me issues is the

with Tquery.Create(nil) do at the beginning. Any ideas?

  with Tquery.Create(nil) do
        begin
            cmd := TStringList.Create;
            cmd.Add('select top 3 csnttext from casenotesint');
            cmd.Add('where csntcaseid = ''' + scasenum + ''' ');
            cmd.Add('and csntclmid = ''' + sclmnumber + ssplitcode + ''' ');
            cmd.Add('order by csntseqnum desc');
            rs := fConnection.Execute(cmd.Text);
            cmd.Free;

            while not Eof do
            begin
                SAPrintReport1.Tab(0.5);
                SAPrintReport1.Print(rs.Fields.Item('CsNtText').Value);
                SAPrintReport1.NewLine;
                rs.next;
            end;
            rs.Close;
        end;
        if cbxSpacer.checked then
        begin
            SAPrintReport1.NewLine;
            SAPrintReport1.NewLine;
            SAPrintReport1.NewLine;
        end;
+1  A: 

You tried changing the TQuery to a TADOQuery, and then what happens?

Warren P
I resolved the issue. The DB connection is opened on the form create, I was having an issue of redundancy
James
+1  A: 

IMO, you should use try..finally..free..end; Otherwise, you're creating a query and never freeing it. i.e. line 2 should be "try".

with Tquery.Create(nil) do 
try
  ..  
  ..  
finally
  free;
end; 
Chris Thornton
+2  A: 

A with statement brings the given object's members into scope, so you can mention its fields, methods, and properties without having to qualify them with the name of the object they belong to. Please see With Statements in the documentation.

One thing that's valid to put in a with statement is an object that was just newly created. That's the case here. The code creates a new TQuery object and immediately uses all its members implicitly. The reference to that object is not stored in any local variable, so there's no explicitly way to refer to it, but that doesn't bother the compiler. (It can bother humans, though, which is one of the reasons you'll see for people to discourage use of with in Delphi.)

Identifiers that don't belong to the given object are searched for in the next surrounding scope instead. It looks like the only identifier from TQuery being used in that with block is Eof, so the code you're converting is probably wrong anyway. It might be better to just figure out what the code was supposed to be doing, and then write new ADO code for that.

Rob Kennedy