类模块中数据库连接代码
Option Explicit
Public cn As ADODB.Connection
Public rs As ADODB.Recordset
Public Function conn() As Boolean
Set cn = New Connection
On Error GoTo cnerr
Dim connect As Boolean
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\dates.mdb;"
cn.ConnectionTimeout = 20
cn.Open
conn = True
Exit Function
cnerr:
conn = False
MsgBox Err.Description
End Function
Public Function openrs(ByVal sqlstr As String) As Boolean
Set rs = New Recordset
On Error GoTo rserr
rs.Open sqlstr, cn, adOpenDynamic, adLockOptimistic, -1
openrs = True
Exit Function
rserr:
openrs = False
MsgBox Err.Description
End Function
你把连接字符串写成SQL的连接就可以了,不会写去BAIDU一下有的是