【发布时间】:2018-09-06 13:11:16
【问题描述】:
我正在使用 DAO 在 Excel 中使用 vba 对受密码保护的 Access 数据库运行查询,有时在运行子程序时会打开一个 Access 实例以及一个询问数据库密码的窗口,而不是输入密码并按取消没有区别,查询仍然运行并显示输出,有没有办法停止访问打开并要求输入密码?
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim DB_Name As String
Dim cond As String
Dim pWord As String
Dim wb As Workbook: Set wb = ThisWorkbook
With wb
On Error GoTo ErrHandler:
clearRange.Value = ""
DB_Name = DataBname()
pWord = pwd()
Set MyDatabase = DBEngine.Workspaces(0).OpenDatabase(DB_Name, False, True, pWord)
Set MyQueryDef = MyDatabase.QueryDefs(queryName) 'Query Name
Set MyRecordset = MyQueryDef.OpenRecordset 'Open the query
pasteRange.CopyFromRecordset MyRecordset
failRange.Value = False
My_Exit:
If MyRecordset Is Nothing Then
'Do Nothing
Else
MyRecordset.Close
Set MyRecordset = Nothing
End If
If MyDatabase Is Nothing Then
'Do Nothing
Else
MyDatabase.Close
Set MyDatabase = Nothing
End If
End With
Exit Sub
ErrHandler:
MsgBox Err.Description
failRange.Value = True
Resume My_Exit
End Sub
Function pwd() As String
pwd = "MS Access;PWD=password"
End Function
【问题讨论】: