• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Passing an ADO connection to an ActiveX DLL

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
kevrob
Asked:
kevrob
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
kevrobAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Éric MoreauSenior .Net ConsultantCommented:
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
 
kevrobAuthor Commented:
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
 
davelowndesCommented:
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

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now