I am working now changing the old DAO to ADO in my M2000 environment. I see some reduce in speed and I found why...All of my commands use the old DBEngine and workspaces...and so I was to change that to a variant
mybase= CreateObject("ADODB.Connection")
But in DAO the opening and close of Workspace has no delays. So I thinking about and my solution is easy to follow... All we have is to save the object and reused it and at the end we can delete all together.
Why we have a solution here which uses a collection and not use one or more variants variables and do the same thing at the end of program?
For simple programs the easy way is to use simple variables. But in more complicated, when we can't figure how many open connection we have, this collection is useful. We can expand the use of it if we pass to the index of it a number indicating the number of open connection (not as ordinal but as an autoincrement number)
So, when we need to set a mybase with SET mybase= CreateObject("ADODB.Connection")
we can see if exist and if exist we can use that or if not we make one.
We need to set that in the same module as with the functions included in the thread
Dim conCollection As Collection
Dim init As Boolean
If Not getone(base, myBase) Then
Set myBase = CreateObject("ADODB.Connection")
' it is better to use the default CursorLocation. So do not change it
' With CursorLocation=3 I can't read an mdb file written with DAO..but with CursorLocation=2 I can..
Set.Open ....the known string here.., 3, 4
end if
now we can open and close recordsets easy and fast.
So before we close the program e say just CloseAllConnections
CloseAllConnections
mybase= CreateObject("ADODB.Connection")
But in DAO the opening and close of Workspace has no delays. So I thinking about and my solution is easy to follow... All we have is to save the object and reused it and at the end we can delete all together.
Why we have a solution here which uses a collection and not use one or more variants variables and do the same thing at the end of program?
For simple programs the easy way is to use simple variables. But in more complicated, when we can't figure how many open connection we have, this collection is useful. We can expand the use of it if we pass to the index of it a number indicating the number of open connection (not as ordinal but as an autoincrement number)
So, when we need to set a mybase with SET mybase= CreateObject("ADODB.Connection")
we can see if exist and if exist we can use that or if not we make one.
We need to set that in the same module as with the functions included in the thread
Dim conCollection As Collection
Dim init As Boolean
If Not getone(base, myBase) Then
Set myBase = CreateObject("ADODB.Connection")
' it is better to use the default CursorLocation. So do not change it
' With CursorLocation=3 I can't read an mdb file written with DAO..but with CursorLocation=2 I can..
Set.Open ....the known string here.., 3, 4
end if
now we can open and close recordsets easy and fast.
So before we close the program e say just CloseAllConnections
CloseAllConnections
Code:
Dim conCollection As Collection
Dim init As Boolean
Sub PushOne(conname As String, v As Variant)
On Error Resume Next
conCollection.Add v, conname
Set v = conCollection(conname)
End Sub
Sub CloseAllConnections()
Dim v As Variant, BB As Boolean
On Error Resume Next
If Not init Then Exit Sub
If conCollection.Count > 0 Then
Dim i As Long
Err.clear
For i = conCollection.Count To 1 Step -1
On Error Resume Next
BB = conCollection(i).connectionstring <> ""
If Err.Number = 0 Then
If conCollection(i).ActiveConnection <> "" Then conCollection(i).Close
End If
conCollection.Remove i
Err.clear
Next i
Set conCollection = New Collection
End If
Err.clear
End Sub
Function getone(conname As String, this As Variant) As Boolean
On Error Resume Next
Dim v As Variant
InitMe
Err.clear
Set v = conCollection(conname)
If Err.Number = 0 Then getone = True: Set this = v
Err.clear
End Function
Sub InitMe()
If init Then Exit Sub
Set conCollection = New Collection
init = True
End Sub