Be careful using CurrentDB
As we all know CurrentDB returns a reference to currently opened database, so we can open recordsets just using CurrentDB.OpenRecordset(). In most cases this works fine, but in complex and recursive procedures you can easy reach a limit of open references (or instances) CurrentDB can hold. Code below shows this:
Code will stop at i=250 with error message 3048:
“Can’t open any more databases”
The workaround – is to declare a public variable dbs as dao.database, set it to CurrentDB at program startup and then use it instead of CurrentDB.
Sub TestCurrentDB()
Dim i As Long, rst(1000) As DAO.Recordset
For i = 1 To 1000
Set rst(i) = CurrentDb.OpenRecordset( _
"Select * from Table1", dbOpenDynaset)
rst(i).MoveFirst
Next i
End Sub
Code will stop at i=250 with error message 3048:
“Can’t open any more databases”
The workaround – is to declare a public variable dbs as dao.database, set it to CurrentDB at program startup and then use it instead of CurrentDB.





0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home