'定义一个全局变量
Public gConn as ADODB.Connection
Const SQL="你的连接字符串"
'定义一个函数获取全局变量
Public Function GetConnection () As ADODB.Connection
On Error Goto OpenErr
If gConn is Nothing then
Set gConn=New ADODB.Connection
gConn.Open SQL
End If
GetConnection=gConn
Exit Function
OpenErr:
Set gConn=Nothing
GetConnection=Nothing
End Function
'关闭连接对象
Public Sub CloseConnection()
On Error Resume Next
If Not gConn is Nothing Then
If (gConn.State And adStateOpen)=adStateOpen Then
gConn.Close
End If
End If
添加一个模块
global conn as adodb.connection
global rec as adodb.recordset
在需要打开连接的事件
set conn=new adodb.connection
conn.connectionString="....."
conn.open
set rec=new adodb.recordset
rec.open "查询",ActiveConnection,CursorType,LockType
do while ....
......
可以写一个全局过程 CloseServer()
public function CloseServer() as boolean
on error resume next
if not conn is nothing then
conn.close
set conn=nothing
set rec=nothing
end if
if err.number>0 then
MsgBox err.description
end if