代码消除需要输入密码

时间:2014-03-14 14:25:31

标签: vba ms-access

我有一个访问表单,可以汇总来自不同表的数据。其中一些需要输入用户名和密码。这是向其他用户发出的,我宁愿他们不必一遍又一遍地输入信息。每当提示输入密码以使其自动登录时,是否有我可以使用的代码?

目前,我已经建立了一个数据库连接的连接,无论何时打开数据库,它都会运行。它看起来像:

Public Function StartUp()
Dim cnn As ADODB.Connection
Dim localrst As New ADODB.Recordset
Dim remoterst As New ADODB.Recordset

On Error Resume Next
Set cnn = New ADODB.Connection


cnn.Open "Provider=SQLOLEDB; Data Source=SOURCE; Initial Catalog=NAME;" _
& "User Id=ID; Password=PW;"

If cnn.State = adStateOpen Then
MsgBox ("You have an established connection with the L&TD SQL Server Database and the CDData     table has been uploaded to the server.")
Else
MsgBox ("Cannot connect to SQL Server. Data will be stored locally to CDData Table until application is opened again with an established connection.")
End If
On Error GoTo 0


End Function

有没有办法为此添加更多连接,以便连接到所有3?

1 个答案:

答案 0 :(得分:1)

字面答案是;没有。您无法通过自动填充进行密码设置。但是,您可以设置连接字符串,以便没有人必须填写它。

'Set up the connection string
        strConn = "PROVIDER=SQLOLEDB;DATA SOURCE=MyServerName;INITIAL CATALOG=MyDatabaseName;UID=MyStandardUserID;PWD=MyStandardPassword;"
        cnComments.Open strConn

您也可以在没有用户名和密码的情况下进行设置:

'Set up the connection string
        strConn = "PROVIDER=SQLOLEDB;DATA SOURCE=MyServerName;INITIAL CATALOG=MyDatabaseName;TRUSTED_CONNECTION=yes;"
        cnComments.Open strConn