Hi Bruce, it's not working.... Still the same....Okay.. this is my module...
I am using class for database, with Connection object shared for the whole class. The connection is made with clsDB.Open <Database Name> not at initialize. There are 2 main command at this class, clsDB.Execute and clsDB.Retrieve.
Private connADO as ADODB.Connection
Private Connected as Boolean
Public Sub Open(DBName)
cnString = "Provider=SQLOLEDB.1;Password=password;" & _
"Persist Security Info=True;User ID=user;Initial Catalog=" & _
"DBName & ";Data Source=SERVER"
If Not Connected Then
Set connADO = New ADODB.Connection
connADO.Open cnString
Connected = connADO.State = adStateOpen
Else
cnADO.Execute "USE " & DBName
End If
End sub
Public Function Execute (SQLCmd) as integer
Dim RecAffected As Integer, cmdADO As ADODB.Command
Set cmdADO = New ADODB.Command
With cmdADO
.ActiveConnection = connADO
.CommandText = SQLCmd
.Execute RecAffected
End With
SQLExecute = RecAffected
end sub
Public Function Retrieve(SQLCmd) As ADODB.Recordset
Dim rsADO As ADODB.Recordset
Set rsADO = New ADODB.Recordset
rsADO.Open SQLCmd, connADO
Set SQLRetrieve = rsADO
End Function
Private Sub Class_Terminate()
connADO.Close
end Sub
The sample Procedure to call them is like this
Public sub TestSub
dim rsADO as ADODB.RecordSet, cDB as clsDB
Set cDB = new clsDB
set rsADO = cDB.Retrieve("SELECT ID, Name FROM TYPE")
cDB.Open "Data2002"
for x = 0 to 1
do While not rsADO.EOF
cDB.Execute "INSERT INTO Temp (Fld1, Fld2, Fld3) VALUES (a1, " & _
rsADO(0) & ", " & rsADO(1) & ")"
rsADO.MoveNext
loop
cDB.Execute "DELETE FROM Sales WHERE ID in (SELECT Fld1 FROM TEMP)"
cDB.Execute "INSERT INTO Sales SELECT * FROM TEMP"
cDB.Execute "EXEC RecalcAll"
set cDB = nothing
Next
end sub
Now.. the problem lies at the last 3 command. Like this....
1. Connect because of Open command
2. Retrieve
3. Doing Execute loop
4. disconnect, reconnect, doing DELETE
5. disconnect, reconnect, doing INSERT
6. disconnect, reconnect, doing EXEC
7. disconnect, reconnect, doing Retrieve
8. Doing Execute loop
9. disconnect, reconnect, doing DELETE
10. disconnect, reconnect, doing INSERT
11. disconnect, reconnect, doing EXEC
At step 2,3, and 8 it is not disconnected.