如果在代碼中使用了數(shù)據(jù)控件如 dao, rdo, 或 ado, 在退出時(shí)應(yīng)該關(guān)閉所有打開(kāi)的 recordset, database,和 workspace 。 雖然對(duì)象能自動(dòng)注銷(xiāo), 但是數(shù)據(jù)連接不會(huì)馬上斷開(kāi), 可能會(huì)導(dǎo)致一些內(nèi)存不能被系統(tǒng)重新分配。
下面的代碼可以關(guān)閉所有打開(kāi)的 dao workspace, 并釋放所占的內(nèi)存。
private sub form_unload(cancel as integer)
on error resume next
’
dim ws as workspace
dim db as database
dim rs as recordset
’
for each ws in workspaces
for each db in ws.databases
for each rs in db.recordsets
rs.close
set rs = nothing
next
db.close
set db = nothing
next
ws.close
set ws = nothing
next
’
end sub
下面的代碼可以關(guān)閉所有打開(kāi)的 dao workspace, 并釋放所占的內(nèi)存。
private sub form_unload(cancel as integer)
on error resume next
’
dim ws as workspace
dim db as database
dim rs as recordset
’
for each ws in workspaces
for each db in ws.databases
for each rs in db.recordsets
rs.close
set rs = nothing
next
db.close
set db = nothing
next
ws.close
set ws = nothing
next
’
end sub