Public dbConn As ADODB.Connection '定义一个ADODB连接
Public stuRs As ADODB.Recordset '定义Recordset对象
Public SqlFindStr As String
Public Function Conn2DB() As Boolean
'公共Access数据库连函数
Dim connStr As String
Dim dbName As String
dbName = App.Path & "\" & "StudentDB.mdb" '返回程序所在路径
' Access连接字符串
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbName
Set dbConn = New ADODB.Connection '定义一个ADODB连接对象
If dbConn.State <> adStateOpen Then '判断ADODB连接是否打开
dbConn.Open conStr '如果不是打开状态 就打开连接
End If
Conn2DB = True
End Function
Public Function ExcSQL(ByVal sql As String) As ADODB.Recordset
Dim rst As ADODB.Recordset
'执行一条SQL查询,得到一个记录集
Set rst = New ADODB.Recordset
rst.Open Trim$(sql), dbConn, adOpenKeyset, adLockOptimistic '得到记录
Set ExcSQL = rst
End Function
自己修改下