Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class - Initialize & Terminate

Status
Not open for further replies.

swapnil

Programmer
Oct 13, 2000
3
0
0
IN
Sir,

I am facing following problem while working on class.

In class module, after executing statements in the 'Initialize' event , 'Terminate' event is executed immediatly.
I want to execute user defined procedure after class_initialize event in executed. Could you help me in solving this problem.


Thanks in advance

Swapnil
 
Hi James,

Thanks for ur reply.
I had confirmed that the procedure names are correct.
I will listout some more information regarding this.

I am using 2 different COM.(say com1 and com2)
In the 'class_initilize' procedure of 'com1', the procedure from 'com2' is executed. After executing this procedure(of com2), the 'class_terminate' event of 'com1' is executed. Due to this I am getting error 'Type mismatch'.

Herewith I am listing code.
------------------------------------------
Private Sub Class_Initialize() 'of com1
Dim Check As Boolean
On Error GoTo X:
Set Obj = CreateObject("com2.test")
Check = Obj.Connect
If Check = True Then
Exit Sub
End If
X:
Err.Clear
Check = False
End Sub
------------------------------------------
Private Sub Class_Terminate() 'of com1
Set Obj = Nothing
End Sub
------------------------------------------
Public Function Connect() As Boolean 'of com2.test
On Error GoTo X:
Set Obj = CreateObject("Adodb.connection")
Obj.Open "connectionstring"
Connect = True
Exit Function
X:
Err.Clear
Connect = False
End Function
------------------------------------------

Thanks in advance.

Swapnil
 
I actually get the code you've posted to work?

I create a form with a Command control (Command1) and a TextBox control (Text1) and inserted the code as below...
Code:
Dim Cclass1 As Class1

Private Sub Command1_Click()
    Text1.Text = Cclass1.Connect
End Sub

Private Sub Form_Load()
    Set Cclass1 = New Class1
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set Cclass1 = Nothing
End Sub

I inserted your code indicated above in a class module (named Class1 - original I know ;-)) and follow it through with f8, when the form appears click the command button and you can see the class doesn't terminate but the connect function returns a [tt]False[/tt].

A point to note might be that Class_Initialize() does indicate an error (429. ActiveX component cant create object) but the error is ignored through X: and the class is not terminated (put in a [tt]msgBox "ClassTerminate"[tt] to confirm) Mark Saunders :)
 
Swapnil:

Before your line X: to run your error catching, do you need an exit sub? It looks as though your error code is always run.

Parke
 
I've noticed when placing code in the initialize and terminate events, these should be used for creating and/or destroying objects, setting default values. A seperate sub or function should be used to complete initialization of objects. If not, you run the risk of an automation time out. Just a suggestion to help root out unknown errors :)
 
Based on what you wrote. Firstly, if you have need for the usage of ADODB Connection, there is no need for the use of the CreateObject() method. Second, based on what Mark Saunders said about a the connection method error, you might want to check if you have added the (Microsoft ActiveX Data Objects x.x Library) reference in your Project Reference.

In COM2, I would code :

Dim cn as Adodb.Connection

Set cn = New Adodb.Connection
cn.Open <Connection String>
 
Are you sure that the scope of the variable holding the instance of your class extends beyond the Calling procedure.

If it does not then the terminate event of the class will be called at the end of the calling procedure.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top