Sylvain;
I have a module which creates the ADO connection object to validates the
login:
Option Compare Database
'declare ADO objects
Public adoConn As New ADODB.Connection
Function CreateADOObjects()
End Function
Function dbLogin(txtUser As String, txtPword As String, txtServer As String,
txtDatabase As String)
On Error GoTo login_err
Dim strCnn As String
'Connect to SQL Server with the native provider
With adoConn
.Provider = "SQLOLEDB.1"
On Error Resume Next
'Try SQL Server security with credentials
strCnn = "Data Source=" & txtServer & ";User ID=" & txtUser & _
";Password=" & txtPword & ";Initial Catalog=" & txtDatabase
.Open strCnn
End With
dbLogin = True
Exit Function
login_err:
dbLogin = False
End Function
I have a specific login form that calls the dbLogin function as shown below:
Option Compare Database
Option Explicit
Dim strCriteria As String
Dim intTry As Integer
Dim strInputFileName As String
Dim strFilter As String
Dim strServer As String
Dim stLocalTableName As String
Dim stRemoteTableName As String
Dim strDatabase As String
Dim strUID As String
Dim strPwd As String
Dim ALLOWCLOSE As Variant
Dim adoCmd As New ADODB.Command
Dim adoRS As New ADODB.Recordset
Dim adoParam As New ADODB.Parameter
Private Sub Form_Unload(Cancel As Integer)
If Not ALLOWCLOSE Then Cancel = True
End Sub
Private Sub cmdCancel_Click()
DoCmd.Quit
End Sub
Private Sub cmdOK_Click()
If IsNull(Me.ServerName) = True Then
MsgBox "The Server is required.", vbCritical
Me.ServerName.SetFocus
Exit Sub
End If
If IsNull(Me.UserName) = True Then
MsgBox "The User Name is required.", vbCritical
Me.UserName.SetFocus
Exit Sub
End If
If IsNull(Me.Password) = True Then
MsgBox "The Password is required.", vbCritical
Me.Password.SetFocus
Exit Sub
End If
Me.LoginMessage = "Logging in to server..."
Me.Repaint
strDatabase = "PBICdb_v10"
strServer = Me.ServerName
strUID = Me.UserName
strPwd = Me.Password
If dbLogin(strUID, strPwd, strServer, strDatabase) Then
'// All is okay.
Me.Visible = False
Me.Password.StatusBarText = ""
ALLOWCLOSE = False
DoCmd.OpenForm "frmMenu"
Else
'// Not okay.
MsgBox "Login failed. Try again.", vbCritical
Me.UserName.SetFocus
Exit Sub
End If
End Sub
Private Sub Form_Open(Cancel As Integer)
'disable default security login form
CurrentProject.OpenConnection ""
DoCmd.SetWarnings False
Application.SetOption "Confirm Record Changes", False
Application.SetOption "Confirm Document Deletions", False
Application.SetOption "Confirm Action Queries", False
strUID = GetUserName()
Me.UserName = GetUserName()
Me.ServerName = "PBICdb_v10"
End Sub
Private Sub Password_AfterUpdate()
On Error Resume Next
Call cmdOK_Click
End Sub
Up to this point I am ok. In the next form, when I try to use the adoConn
object I get an error trying to set the ActiveConnection attribute for the
command object. The connection object does exist at that point:
Private Sub Form_Open(Cancel As Integer)
' close connection
CurrentProject.OpenConnection ""
'Turn off Access messages
DoCmd.SetWarnings False
Application.SetOption "Confirm Record Changes", False
Application.SetOption "Confirm Document Deletions", False
Application.SetOption "Confirm Action Queries", False
'Get the user name entered
Dim strUserName As String
Dim strPwd As String
Dim strDatabase As String
Dim strServer As String
strUserName = Forms!frmlogin.UserName
strPwd = Forms!frmlogin.Password
strDatabase = "PBICdb_v10"
strServer = Forms!frmlogin.ServerName
'create and append parameters and execute the store procedure
With adoCmd
'create and append the parameter
Set adoParam = .CreateParameter("UserName", adVarChar, adParamInput,
Len(strUserName), strUserName)
.Parameters.Append adoParam
Set .ActiveConnection = adoConn
'specify a stored prcoedure
.CommandType = adCmdStoredProc
'Brackets must surround stored procedure names with spaces
.CommandText = "sp_Get_User_Access"
'receive the recordset
Set adoRS = .Execute
End With
I have tried several iterations where I rebuild the adoConn object for each
form but it seems like that should not be necessary. Perhaps I have a basic
misunderstandng of how the object is handled?
David