尝试使用AttachDSNLessTable函数时出错

时间:2014-05-08 04:43:13

标签: sql sql-server-2008 access-vba

我正在尝试使用无dsn连接连接到SQL Server 2008。我尝试使用AttachDSNLessTable例程但未能使其正常工作。我收到了消息

  

运行时错误' 2425'您输入的表达式具有Microsoft无法找到的功能名称

我有一个名为AutoExec的宏,它运行代码AttachDSNLessTable()。

我在AutoExec宏中写道:

AttachDSNLessTable ("Land", "dbo.Land", "ABZAL", "Pdata", "sa", "294285")

AttachDSNLessTable代码:

选项比较数据库

'//Name     :   AttachDSNLessTable
'//Purpose  :   Create a linked table to SQL Server without using a DSN
'//Parameters
'//     stLocalTableName: Name of the table that you are creating in the current database
'//     stRemoteTableName: Name of the table that you are linking to on the SQL Server database
'//     stServer: Name of the SQL Server that you are linking to
'//     stDatabase: Name of the SQL Server database that you are linking to
'//     stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
'//     stPassword: SQL Server user password
Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
    On Error GoTo AttachDSNLessTable_Err
    Dim td As TableDef
    Dim stConnect As String

For Each td In CurrentDb.TableDefs
    If td.Name = stLocalTableName Then
        CurrentDb.TableDefs.Delete stLocalTableName
    End If
Next

If Len(stUsername) = 0 Then
    '//Use trusted authentication if stUsername is not supplied.
    stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
Else
    '//WARNING: This will save the username and the password with the linked table information.
    stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
End If
Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
CurrentDb.TableDefs.Append td
AttachDSNLessTable = True
Exit Function

AttachDSNLessTable_Err:

AttachDSNLessTable = False
MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description

结束功能

以下是我使用方法1的源http://support.microsoft.com/kb/892490的链接。

请帮帮我。

0 个答案:

没有答案