Public Function ExecuteSQL(ByVal SQL As String) As ADODB.Recordset
SQL = Trim(SQL)
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
Dim strAppPath As String
strAppPath = App.Path
If Right(strAppPath, 1) <> "\" Then
strAppPath = strAppPath & "\"
End If
strAppPath = strAppPath & "data\wx.mdb"
ConnectString = "DRIVER=Microsoft Access Driver (*.mdb);DSN=" & strAppPath & ";UID=;PWD="
rst.open
Set rst.activeconnection = cnn rst.LockType = adLockOptimistic
rst.CursorType = adOpenKeyset
rst.open SQL
Set exesql = rst
Set rst = Nothing
Set cnn = Nothing
end function
应该错在这里(红色标志), 提醒:1。 用DSN 做连接源,确定你有没有配置“数据源 (ODBC)”(控制面板中配置)?
2。 Acess建议用Microsoft.JET 做连接源方便点。
给你个例子看带加密的。
Public Function TransactSQL(ByVal sql As String) As adodb.Recordset
Dim con As adodb.Connection
Dim rs As adodb.Recordset
Dim strConnection As String
Dim strArray() As String
Set con = New adodb.Connection
Set rs = New adodb.Recordset
On Error GoTo TransactSQL_Error
strConnection = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path & "\data\EMSDB.mdb;Jet OLEDB:Database password=" & DBPWDstr
strArray = Split(sql)
con.Open strConnection
If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
rs.Open Trim$(sql), con, adOpenKeyset, adLockOptimistic
Set TransactSQL = rs
iflag = 1
Else
con.Execute sql
iflag = 1
End If
TransactSQL_Exit:
Set rs = Nothing
Set con = Nothing
Exit Function
TransactSQL_Error:
MsgBox "连接错误:" & err.Description
iflag = 2
Resume TransactSQL_Exit
End Function