Link to home
Start Free TrialLog in
Avatar of unholly_plugin
unholly_plugin

asked on

Event raised before object is set

Hello,

I wrote an activex DLL that holds a class which handles connecting to an Access or SQLServer DB.
the class has different properties like DB password, Save DB path to registry, etc.
the only public function is this:

   Public Function CreateConnection(Optional DBPath As String) As ADODB.Connection
   .
   .
   .
   end function

which handles the all connection operation and as you can see it returns a connection object to set the one in the application that uses this activex.

my problem is that im raising a Connection_Successful event in the CreateConnection function/
If i try writing a code that accesses the database in the sub that handles this event  using my connection object i get an error that my object is not set which is true since the CreateConnection function didnt return the object yet.

is there a way to make the Connection_Successful event raise after the function that raised it has returned the object?

Thank you.
Avatar of jkaios
jkaios
Flag of Marshall Islands image

In your CreateConnection() function, you first test if the connection was, in fact, successful by testing the "State" property and then pass the Connection Object to a parameter in your Connection_Successful event.

1) Your Connection_Successful event should be declared like this:

   Public Event Connection_Successful(ConnObject As ADODB.Connection)

2) In your CreateConnection function, your codes should be like this:

   Public Function CreateConnection(SQLServerName As String, DBName As String) As ADODB.Connection
      Dim oConn As New ADODB.Connection
      With oConn
         .Provider = "SQLOLEDB"
         .Properties("Persist Security Info").Value = False
         .Properties("Integrated Security") = "SSPI"
         .Properties("Data Source").Value = SQLServerName
         .Properties("Initial Catalog").Value = DBName
         .Open      
         If .State = adStateOpen Then
            RaiseEvent Connection_Successful(oConn)
         End If
      End With
   End Function
By the way, "underscores" are allowed in Event names so you would have to remove the "_" from your "Connection_Successful" event.
... or try this one:

   Public Function CreateConnection(SQLServerName As String, DBName As String) As ADODB.Connection
      Dim oConn As New ADODB.Connection
      With oConn
         .Provider = "SQLOLEDB"
         .Properties("Persist Security Info").Value = False
         .Properties("Integrated Security") = "SSPI"
         .Properties("Data Source").Value = SQLServerName
         .Properties("Initial Catalog").Value = DBName
         .Open      
         If .State = adStateOpen Then
            Set CreateConnection = oConn
         End If
      End With
   End Function
Avatar of unholly_plugin
unholly_plugin

ASKER

Hey jkaios, thanks for the quick answer.

I have already thought about sending the object with the Success event, but is it not considered redundant??? returning the object with the event and also with the function???

If its not then this is the obvious solution, i was just wondering if it was possible to have it like i did and make the event raise a bit later when the connection object is already set.

Thanks again
Yeah, that's kinda redundant, but that was just a quick example on how to resolve the issue.

In my first example, I was referring to a function that returns a Boolean value instead of an ADODB.Connection object because the object is already returned by the custom event, in which you're right.  We don't have to return the object twice via both the event and the function.

In the second example, the function returns an ADO Connection object which is only set after the connection was in fact successful. Otherwise, it would return an ADO Connection object which is set to Nothing if the connection was not successful.
consider redesign your object model.
i'd return a connection object, and it's up to the client to invoke the connection's "Open" method, and then listening to the connection's events is up to the client as well.
imho, raising an event and passing the connection object as an argument is not a good design.
hey ronklein,

id agree about not passing the connection object with the event
but id still want someone who uses my class to get a notification about the success or failure of the CreateConnection method.

what if i exposed a connection object as public from my class and change CreateConnection to a sub. then when the success event is raised someone who ueses the class will be able to use the connection object i exposed and connected to the db with the parameters given to the class object?

any thoughts about that?
ASKER CERTIFIED SOLUTION
Avatar of ronklein
ronklein

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial