• | 当您使用 Autoexec, 调用 AttachDSNLessTable 函数, 并然后传递参数, 如以下所示从 RunCode 操作。
|
• | 当您使用启动窗体, 将代码, 它类似于以下以 Form_Open 事件。 向 Access 数据库添加多个链接表时 注意 您必须调整编程逻辑。 |
'//Name : CreateDSNConnection
'//Purpose : Create a DSN to link tables to SQL Server
'//Parameters
'// stServer: Name of 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 CreateDSNConnection(stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String) As Boolean
On Error GoTo CreateDSNConnection_Err
Dim stConnect As String
If Len(stUsername) = 0 Then
'//Use trusted authentication if stUsername is not supplied.
stConnect = "Description=myDSN" vbCr "SERVER=" stServer vbCr "DATABASE=" stDatabase vbCr "Trusted_Connection=Yes"
Else
stConnect = "Description=myDSN" vbCr "SERVER=" stServer vbCr "DATABASE=" stDatabase vbCr
End If
DBEngine.RegisterDatabase "myDSN", "SQL Server", True, stConnect
'// Add error checking.
CreateDSNConnection = True
Exit Function
CreateDSNConnection_Err:
CreateDSNConnection = False
MsgBox "CreateDSNConnection encountered an unexpected error: " Err.Description
End Function
注意 如果再次, 调用 RegisterDatabase 方法 DSN 更新。• | 当您使用 Autoexec, 调用 CreateDSNConnection 函数, 并然后传递参数, 如以下所示从 RunCode 操作。
|
• | 当您使用启动窗体, 将代码, 它类似于以下以 Form_Open 事件。
|