Solved

Passing an ADO Connection to an AdoQuery in a DLL

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

15 Experts available now in Live!

Get 1:1 Help Now