Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 ADO connection + recordset problem

Author  Topic 

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2005-08-04 : 05:29:28
Hi all,

Im struggling a little with this. Can anyone help me?

When I run the code I get "Run-time error '3709': The connection cannot be used to perform this operation. It is either closed or invalid in this context."

Private Sub btnAdd_Click()
Dim UserName As String
Dim Initials As String
Dim Password As String
Dim OutlookName As String
Dim cnCurrent As New ADODB.Connection
Dim rsUsers As New ADODB.Recordset

'Check each control, is their a value? if not, set focus to control
If IsNull(txtUserName) Then
MsgBox "You did not enter a new UserName nobby!"
Me!txtUserName.SetFocus
Exit Sub

ElseIf IsNull(txtInitials) Then ' return value of UserName variable;
MsgBox "You have not entered any initials for user: '" & Me!txtUserName & "'"
Me!txtInitials.SetFocus
Exit Sub

ElseIf IsNull(txtPassword) Then
MsgBox "You must create a password for user: '" & Me!txtUserName & "'"
Me!txtPassword.SetFocus
Exit Sub

ElseIf IsNull(txtOutlookName) Then
MsgBox "You must enter a Outlook name for: '" & Me!txtUserName & "'"
Me!txtOutlookName.SetFocus
Exit Sub

End If
' Pass the variables to the table.

cnCurrent.ConnectionString = "Provider=sqloledb;Data Source=PE750-D;Initial Catalog=HSS;Trusted_Connection=yes;"

MsgBox "Connection OK"

Set rsUsers = New ADODB.Recordset

rsUsers.Open "Users", cnCurrent, adOpenDynamic, adLockPessimistic, adCmdTable

With rsUsers
.AddNew
![User] = Me!txtUserName
![Password] = Me!txtPassword
![Initials] = Me!txtInitials
![OutlookName] = Me!txtOutlookName
![Level] = 1
![Select] = 0
![dummy] = Null
.Update
.Close
End With

Set rsUsers = Nothing

lblAdded.Visible = True

If MsgBox("The user: '" & Me!txtUserName & "' was successfully added. Do you wish to add another?", _
vbYesNo, "Information") = vbYes Then
DoCmd.Close
DoCmd.OpenForm "Add_User", , , , , acDialog
Else
DoCmd.Close
End If
End Sub


when life hands you lemons, ask for tequila and salt

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-04 : 05:35:34
After cnCurrent.ConnectionString statement write

cnCurrent.open

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2005-08-04 : 11:22:18
WOOT WOOT

Thanks a million.

Doh! what a noobish mistake... I had a feeling it was around their, thats why I was adding the message box, ie if it struggles with my code it wouldnt get as far as the box...

Oh well you live an learn, thanks for your help once again

when life hands you lemons, ask for tequila and salt
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-05 : 01:28:13
Well. It is usual thing that seems to occur everywhere

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -