Solved

Passing an ADO Connection to an AdoQuery in a DLL

Posted on 2004-05-02
5
1,747 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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