Solved

Passing an ADO Connection to an AdoQuery in a DLL

Posted on 2004-05-02
5
1,754 Views
Last Modified: 2007-12-19
I'm trying to pass an ADOConnection to an ADO query in a DLL.
When I try to activate the query I get an EOleException error.
I'm using Delphi 6 and am calling CoInitialize.
What am I missing or am doing wrong.
Any help would be appreciated.
TIA
Jeff
0
Comment
Question by:medusaresearch2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Expert Comment

by:Bracco
ID: 10975133
Hi medusaresearch2,
this code works properly. Note that if you're passing strings, your first uses clause must be ShareMem in both DLL and project:

uses
  ShareMem,
  ...
  ADODb,
  ActiveX,
  ...;

...
...    
    try
{ CoInitialize  }
        CoInitialize(nil);
{ Connection createing }
        adoConn                  := TAdoConnection.Create(myComp);
        adoConn.ConnectionString := sConn;
        adoConn.LoginPrompt      := False;
        adoConn.Connected        := True;
    except
        on e: exception do
        begin
            WriteLog('   ===> Connection Error <===');
            WriteLog(e.Message);
            CoUnInitialize;
            raise;
        end;
    end;
...
{ Query }
    try
        qryIndex            := TAdoQuery.Create(myComp);
        qryIndex.Connection := adoConn;
        qryIndex.Close;
        qryIndex.SQL.Add('Select * From ' + sTableName);
        qryIndex.SQL.Add(' Where ' + sFilter + ' <> ' + #39 + #39);

        qryIndex.Prepared;
        qryIndex.Open;
        ...
        ...
0
 

Author Comment

by:medusaresearch2
ID: 10978704
Bracco,

I'm passing an existing ado connection to a routine in a DLL from the main program:

Main program:
   EditData( Ado );

DLL:
   procedure EditData( ADOConn : TAdoConnection ); //DLL routine def

In the DLL, the routine creates a form which has a query component.

  frmEditData := TfrmEditData.Create(Application);

The connection is then set for the query:

  adoQuery.Connection := ADOConn;

Then the query is executed and I get the EOleException error.

What am I missing?

TIA Jeff
0
 

Expert Comment

by:Bracco
ID: 10984009
Hi medusaresearch,
i'm trying to do the same, but i think the problem isn't in the adoConn or in the adoQuery. If the query is created properly i think there's no problem there.

Bracco
0
 

Accepted Solution

by:
Bracco earned 125 total points
ID: 10984256
Hi Medusaresearch2,
i tried and the code works properly. In a main program i create the connection.

DLL
------
procedure editData(adoConn: TAdoConnection);
var frmEdit: TfrmEditFields;
begin
    frmEdit                  := TfrmEditFields.Create(adoConn);
    frmEdit.ShowModal;
    frmEdit.freeMemory;
    frmEdit.Free;
end;

frmEdit
---------
constructor TfrmEditFields.create(adoConn: TAdoConnection);
begin
    inherited Create(nil);
    myConn := TAdoConnection.Create(self);
    myConn := adoConn;
end;

procedure TfrmEditFields.FormShow(Sender: TObject);
begin
    AdoQry.Connection := myConn;
    AdoQry.Close;
    AdoQry.SQL.Clear;
    AdoQry.SQL.Add('Select * from TableName');
    AdoQry.Prepared := True;
    AdoQry.Open;

    if not ( AdoQry.Eof ) then
    begin
        edtField1.Text := AdoQry.FieldByName('Field1').AsString;
        edtField2.Text := AdoQry.FieldByName('Field2').AsString;
        edtField3.Text := AdoQry.FieldByName('Field3').AsString;
    end;
end;

Hope it helps you


0
 

Author Comment

by:medusaresearch2
ID: 10988146
Thanks, that fixed it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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