Solved

Passing an ADO connection to an ActiveX DLL

Posted on 2000-03-10
6
473 Views
Last Modified: 2013-11-23
I'm trying to pass an open ADO connection object to a method in an ActiveX DLL.

As soon as I try to open a recordset using this connection in the ActiveX DLL it gives me an error 3001.

I know the connection is good as im using it in the calling procedure and when I check the status of the connection in the ActiveX DLL everything looks fine.

I could just open another connection but I would rather use the open connection if possible.

I also tried passing an ADO recordset and this works fine!!!

Ive tried setting the connection as a property, as a parameter, creating a local connection and setting it to the passed connection all to no avail.

Anyone got any ideas?

Cheers
Kev
0
Comment
Question by:kevrob
6 Comments
 
LVL 6

Expert Comment

by:Tmess
ID: 2606759
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 200 total points
ID: 2606767
What does your code look like?

This is the code I have in a Standard EXE:

Private Sub Command1_Click()
Dim cnLocal As ADODB.Connection
Dim rstTemp As ADODB.Recordset
Dim x As New Class1

    Set cnLocal = New ADODB.Connection
    cnLocal.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Program Files\Microsoft Visual Studio\VB98\Nwind2000.mdb;Persist Security Info=False"
    Set x.gConnection = cnLocal
    Set rstTemp = x.OpenRecordset
    Do Until rstTemp.EOF
        MsgBox rstTemp!CompanyName
        rstTemp.MoveNext
    Loop
End Sub

Here is the code that I have in the DLL:
Option Explicit

Private mConnection As ADODB.Connection

Public Property Get gConnection() As Variant
    Set gConnection = mConnection
End Property

Public Property Set gConnection(ByVal vNewValue As ADODB.Connection)
    Set mConnection = vNewValue
End Property

Public Function OpenRecordset() As ADODB.Recordset
Dim rstX As New ADODB.Recordset

    rstX.CursorType = adOpenStatic
    Set rstX = mConnection.Execute("SELECT * FROM Shippers")
    Set OpenRecordset = rstX
End Function
0
 

Author Comment

by:kevrob
ID: 2607429
Your code does work but I need to know why mine dont as at work we usually use the open method of the recordset not use a connection or command .execute method. We only use these if its an sql update etc.

This is my code in the EXE

Private Sub Command1_Click()
Dim cnLocal As ADODB.Connection
Dim x As New clsTest

    Set cnLocal = New ADODB.Connection
    cnLocal.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb;Persist Security Info=False"
    Call x.ProcessRecords(cnLocal)
   
    cnLocal.Close
    Set cnLocal = Nothing
End Sub


This is my code in the DLL

Public Sub ProcessRecords(ByVal adoConn As ADODB.Connection)
Dim rs As ADODB.Recordset

    Set rs = New ADODB.Recordset
    rs.Open "SELECT * FROM Shippers", adoConn, adOpenForwardOnly, adLockReadOnly
   
    With rs
        Do While Not .EOF
            Debug.Print !CompanyName
            .MoveNext
        Loop
        .Close
    End With
   
    Set rs = Nothing
End Sub

0
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.

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2607645
Hi kevrob,

Your code works perfectly on my PC!

Which version of ADO are you referencing? I doubt you use 2.0 (or less). Upgrade to at least 2.1 (from http://www.microsoft.com/data/).

Another thing you can try is to send the connection ByRef instead of ByVal like this:
Public Sub ProcessRecords(ByRef adoConn As ADODB.Connection)
0
 

Author Comment

by:kevrob
ID: 2608271
Ive tried both verions 2.0 & 2.1 of the ActiveX data libraries but both give me the same error message. I've also tried by ref and byval and still no good. Its got to be down to something on my machine.

Ive accepted your answer anyway and i'll try the same code on another machne, or as a drastic messure  rebuild my machine and see if that fixes the problem.

At worst I'll just use the execute method of the connection object passed

Cheers

Kevrob
0
 

Expert Comment

by:davelowndes
ID: 5704226
The comments here didn't answer the question which is why you cannot pass the reference to the connection object. Here's the answer from another PAQ:

From: afpcos
 Date: Tuesday, May 23 2000 - 10:06PM NZST  
You can not pass a connection out of the process in which it was created.

I am using connection pooling in my project.  I tried the same thing that you are talking about when I was first designed my project, about 9 months ago.  I was unsuccessfull in passing a connection between the exe and activex exe.  After calling Microsoft,  they advised that it is not possible to pass a connection out of process, such as from an exe to an active x exe or the other way around.

I am now managing the connection and all sql work in the active x exe passing back to the exe disconnected recordsets.  
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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 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

18 Experts available now in Live!

Get 1:1 Help Now