【发布时间】:2016-11-04 11:00:15
【问题描述】:
我可以通过 excel 访问我的数据库,当我再次尝试访问它时出现问题。似乎我在我身后留下了一些开放的东西,但我不明白是什么。
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim strInput As String
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
strInput = Sheets("base_pivot").Range("I4").Value
'I get the input for the parameter from my sheet base_pivot on the cell I4
With con
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open "C:\cmo_accounting\CMO_accounting.mdb"
End With
With cmd
.ActiveConnection = con
.CommandText = "mk_edu_ana_invoices"
.CommandType = adCmdStoredProc
.Parameters.Append cmd.CreateParameter("[input_code_ana]", adChar, adParamInput, 5)
.Parameters("[input_code_ana]") = strInput
End With
cmd.Execute
con.Close
Set cmd = Nothing
Set prm = Nothing
Set con = Nothing
这在我第一次使用时有效,并且在我重新打开 excel 文件时再次有效。关闭excel时似乎连接已关闭。
【问题讨论】:
-
我发现了几件事 - 但不确定是否会有所作为。你在哪里定义了
strPath? (使用option explicit可以避免这些问题)。Range("I4:I4")也可以是.Range("I4") -
嘿,我对 strPath 进行了更改,我将字符串放在那里,这样每个人都更清楚。我将对范围 I4 进行更改。谢谢!
-
这个宏(子或函数)在哪里,它是如何触发的?工作簿的其他地方是否有另一个数据库连接?