VB runtime error 3709 when using 2 access database files

Viewed 47

Error 3709: The connection cannot be used to perform this operation. It is either closed or invalid in this content.

I am trying to add a record to 2 separate access database

the first database is:

Public Function Connection()

    Connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= \\amznfsxormvw5vq.prod-ca1.sterling.zone\share\CDC_Departments\ST Structure\Verifications Dept\Ver Resources\Sterling Tally App Shortcuts\Offshore Follow Up Error Tracker\bin\debug\sequence\dbFollowUpTracker.ACCDB;Persist Security Info=False"
      
End Function

and the second one is this:

Public Function ConnectionBCP()
 
    ConnectionBCP = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\bin\debug\sequence\dbBcpTracker.ACCDB;Persist Security Info=False"
    
End Function

And here are my Public connections:

Public DB           As New ADODB.Connection
Public DB1          As New ADODB.Connection
Public DB2        As New ADODB.Connection
Public RS           As New ADODB.Recordset
Public RS1          As New ADODB.Recordset
Public RS2          As New ADODB.Recordset

Now here's the main problem

Now on my "Save command button" this is the code, so basically what it's main function is to add a new user in the FIRST Database then after it adds the new user, it will also add a new record in the SECOND Database. Please see below:

    DB.Open Connection
    RS.Open "SELECT * FROM tblUsers WHERE Username ='" & txtUsername.Text & "'", DB, 3, 3
    If RS.RecordCount = 0 Then
        RS.AddNew
        RS(1) = txtUsername.Text
        RS(2) = txtPassword.Text
        RS(3) = cboUserType.Text
        RS(4) = txtEmployeeNumber.Text
        RS(5) = txtLastName.Text
        RS(6) = txtFirstName.Text
        RS(7) = txtEmailAddress.Text
        RS(8) = cboEmployeeStatus.Text
        RS(9) = cboIsTeamLead.Text
        
        
        If cboIsTeamLead.Text = "Yes" Then
                DB1.Open Connection
                RS1.Open "SELECT * FROM tblTeamLead WHERE TeamLeader ='" & txtUsername.Text & "'", DB1, 3, 3
                If RS1.RecordCount = 0 Then
                    RS1.AddNew
                    RS1(1) = txtUsername.Text
                    'RS1(2) = txtFirstName.Text
                    RS1(2) = txtFirstName.Text + " " + txtLastName.Text
                    RS1.Update
                    RS1.Close
                    DB1.Close
                Else
                    cboIsTeamLead.Text = ""
                    RS1.Close
                    DB1.Close
                    MsgBox "Record found: Existing team lead on database.", vbExclamation, "Failed"
                End If
        End If
        
        
        RS(10) = cboIsDepartmentHead.Text
        RS(11) = txtOffice.Text
        RS(12) = txtDepartment.Text
        RS(13) = cboTeam.Text
        RS(14) = cboSecretQuestion.Text
        RS(15) = txtSecretAnswer.Text
        RS(16) = txtFirstName.Text & " " & txtLastName.Text
        RS(17) = txtTeamLeader.Text
        RS(18) = txtTLemail.Text
        RS(19) = txtPLemail.Text
               
        
    RS.Update
        
        MsgBox "Successfully added a new user.", vbInformation, "Success"
        addUserActions "Added new user '" & txtUsername.Text & "'", "New User"
        'clearfields
        list_box_data
        
    Else
    txtUsername.Text = ""
    RS.Close
    DB.Close
        MsgBox "Record already exist.", vbExclamation, "Failed"
        Exit Sub
   End If

    RS.Close
    DB.Close

Now what after closing the DB, it goes to the next line which is this and I will encounter the runtime error 3709 at line that says RS2.Open "SELECT * FROM tblUserBcp", DB, 3, 3 :

DB2.Open ConnectionBCP
RS2.Open "SELECT * FROM tblUserBcp", DB, 3, 3
    RS2.AddNew
    RS2(1) = txtFirstName.Text & " " & txtLastName.Text
    RS2(2) = txtEmployeeNumber.Text
    RS2(3) = txtManagerName.Text
    RS2(4) = txtTeamLeader.Text
    RS2(5) = txtOffice.Text
    RS2(6) = ""
    RS2(7) = txtUsername.Text
    
    
RS2.Update
   
RS2.Close
DB2.Close

Thank You!

0 Answers
Related