如何从access 2013中的链接表中获取vba中的连接字符串?

时间:2014-03-04 02:09:02

标签: vba ms-access ms-access-2007 access-vba ms-access-2010

我在访问时有这个链接表,我希望得到它的连接

这是截图

enter image description here

在我的vba中,我尝试了这段代码但由于我的存储过程没有执行而无法正常工作

Dim adocmd As New ADODB.Command

DoCmd.Maximize
adocmd.ActiveConnection = Application.CurrentProject.Connection.ConnectionString
  adocmd.CommandType = adCmdStoredProc
adocmd.CommandText = "spr_DECSBillingSchedule"
adocmd.CommandTimeout = 0
On Error Resume Next
adocmd.Execute , , adExecuteNoRecords
Set adocmd = Nothing
On Error GoTo 0

我怎样才能解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:2)

表的定义存储在名为MSysObjects的系统表中,连接字符串位于字段Connect中。您可以访问此表以获取连接字符串以及何时运行sproc(您需要引用您知道的表位于同一数据库中,我的前端链接到多个数据库),但作为连接字符串不会改变你可能更好地将它设置为全局变量或只是硬编码(我已经养成了做的习惯)。

注意:这是根据MS Access 2007,它是我安装的唯一一个

下面是我用来执行sproc的函数示例,sproc返回一个值以确认它已成功完成,并在@Ret中返回。希望这会有所帮助。

Function LogImportFile(strFile As String) As Long
On Error GoTo Err_Handle_LogImportFile


Set cnn = CreateObject("ADODB.Connection")
cnn.ConnectionString = "DRIVER={SQL Server};SERVER=[Server];DATABASE= _
    [DatabaseName];Trusted_Connection=Yes"
    ' The above is for linking to a SQL Server table using a DSN less connection
    ' which I would highly recommend (DSN less) if you plan to distribute your
    ' database

cnn.Open cnn.ConnectionString

Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "cmsUser.usp_LogImportFile"
cmd.CommandTimeout = 0

Set param = cmd.CreateParameter("@FileName", adVarChar, adParamInput, _
    200, strFile)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("@Ret", adInteger, adParamOutput)
cmd.Parameters.Append param


cmd.Execute

LogImportFile = cmd.Parameters("@Ret")


Exit_Proc:
Set cnn = Nothing
Set cmd = Nothing
Set param = Nothing

Exit Function

Err_Handle_LogImportFile:
    Msgbox "LogImportFile - " & Err.Number & " - " & Err.Description
    LogImportFile = -1
    GoTo Exit_Proc
End Function