从adp导出到accdb

时间:2015-11-17 14:21:03

标签: sql-server ms-access access-vba ms-access-2007

所以我有一个* .adp文件连接到sql数据库并调用存储过程,更改/检索数据等。在ms office 2013安装后它停止工作。 我已将我的所有代码(表单,模块,宏)从* .adp文件导出到新创建的空* .accdb并且它已停止工作。我正在获得"方法' CloseConnection'对象CurrentProject失败"错误。

Application.CurrentProject.CloseConnection

strConnect = "Provider=SQLOLEDB.1" & ";Data Source=" & strServerName & _
  ";Initial Catalog=" & strDBName
If strUN <> "" Then
    strConnect = strConnect & ";user id=" & strUN
    If strPW <> "" Then
        strConnect = strConnect & ";password=" & strPW
    End If
Else 'Try to use integrated security if no username is supplied.
    strConnect = strConnect & ";integrated security=SSPI"
End If
Application.CurrentProject.OpenConnection strConnect

ChangeADPConnection = True

如果我对该行发表评论,我会收到另一个错误&#34; Application.CurrentProject.OpenConnection&#34;其中说&#34; Application.CurrentProject.OpenConnection是指一个已关闭或不存在的对象&#34;。所有这些代码都在.adp中运行良好。 我假设引用存在一些问题,我从旧文件中复制它们,但我猜测我需要使用其他文件来获取新的访问文件,参考文献是:

OLE Automation
Microsoft ActiveX Data Objects 2.1 Library
Microsoft Windows Common Controls 6.0 (SP6)
Microsoft Excel 12.0 Object Library
Microsoft Scripting Runtime
Microsoft Internet transfer control 6.0 (SP6)
Microsoft Visual Basic for Applications Extensibility 5.3

1 个答案:

答案 0 :(得分:0)

感谢Wayne G. Dunn以及我自己对此事的研究,我的代码工作正常。 首先,这些事情不适用于新的Access:

Application.CurrentProject.OpenConnection
Application.CurrentProject.CloseConnection

我必须为连接创建一个新的全局变量:

Global dbConn As ADODB.Connection

然后在我的方法中使用它。功能打开连接现在看起来像这样:

Function ChangeADPConnection(strServerName As String, strDBName As _
   String, Optional strUN As String, Optional strPW As String) As Boolean
Dim strConnect As String
On Error GoTo EH:

If Not dbConn Is Nothing Then
  If (dbConn.State And adStateOpen) = adStateOpen Then dbConn.Close
  Set dbConn = Nothing
End If

Set dbConn = New ADODB.Connection


strConnect = "Provider=SQLOLEDB.1" & ";Data Source=" & strServerName & _
  ";Initial Catalog=" & strDBName
If strUN <> "" Then
    strConnect = strConnect & ";user id=" & strUN
    If strPW <> "" Then
        strConnect = strConnect & ";password=" & strPW
    End If
Else 'Try to use integrated security if no username is supplied.
    strConnect = strConnect & ";integrated security=SSPI"
End If

dbConn.Open (strConnect)
ChangeADPConnection = True
dbConn.CursorLocation = adUseClient

Exit Function
EH:
 MsgBox Err.Number & ": " & Err.Description, vbCritical, "Connection"
 ChangeADPConnection = False
End Function

我还添加了一个不可见的表单,它会在关闭时触发关闭连接的操作。 &#34; dbConn.CursorLocation = adUseClient&#34;填写记录集也需要某种方式。