Solved

Passing an ADO Connection to an AdoQuery in a DLL

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

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

24 Experts available now in Live!

Get 1:1 Help Now