打开ADODB MS Access Recordset时Excel VBA宏崩溃

时间:2017-09-26 16:27:30

标签: excel vba ms-access ado adodb

我有一个问题,即我有一个excel vba宏,它可以在我的本地机器上完美运行,但是当存储在服务器上崩溃时。

我已经确定了宏崩溃的点,这是excel宏尝试打开ADODB Recordset时(Db是MS Access)。

代码中的文件夹结构与我的桌面设置相同,所以我不认为这是问题所在。就好像是excel文件在打开ADODB记录集时尝试查询数据库的时候。

任何帮助非常感谢!如果您需要进一步的信息,请告诉我。

提前致谢!

编辑:

经过进一步调查,我已经验证打开和关闭连接和记录集是否正常工作。

当我通过此查询作为导致excel崩溃的源时。这是我的代码:

Dim DirectoryLocation As String
DirectoryLocation = Application.ActiveWorkbook.Path
Dim ConnString As String
ConnString = _
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DirectoryLocation & "\Data\UK Sales.accdb;Persist Security Info=False;"

'Declare new database connection object
Dim DBConn As ADODB.Connection
'Set new database connection object
Set DBConn = New ADODB.Connection

'Declare new recordset object
Dim SalesData As ADODB.Recordset
'Set new recordset object
Set SalesData = New ADODB.Recordset


'Define connection string property of database connection object
DBConn.ConnectionString = ConnString

'Open database connection
DBConn.Open
'On Error GoTo CloseConnection

'Set parameter values for reporting
Dim Rep_Param As String
Rep_Param = Application.WorksheetFunction.VLookup(Worksheets("Dashboard").Range("C5"), Worksheets("Lookups").Range("H:I"), 2, 0)
Dim Rep_Option As String
Rep_Option = Worksheets("Dashboard").Range("C6").Value

'Get customer names for report and populate report
'------------------------Query---------------------'
Dim Cust_Billing_Qry As String
Cust_Billing_Qry = "SELECT DISTINCT Customer_Billing FROM Sales WHERE " & Rep_Param & "='" & Rep_Option & "'"
SalesData.Open Cust_Billing_Qry, DBConn
'On Error GoTo CloseRecordSet
Sheets("Revenue Analysis (Detail)").Range("C:C").ClearContents
Sheets("Revenue Analysis (Detail)").Range("C6").CopyFromRecordset SalesData
SalesData.Close

非常感谢任何帮助!

0 个答案:

没有答案