Solved

run time error 3709 database connection problem

Posted on 2004-04-26
8
1,485 Views
Last Modified: 2013-12-25
hi,
im recieving the run time error 3709: the connection cannot be used to perform this operation. It is either closed or invalid in this context.


My program consists of a MainMenu form (which is the startup form) and a Customer Form (which is linked to the database). When i first click on the main menu form to access the Customer Form everything runs fine with the database connection... though when i click to go back to the mainmenu frm and then try to go back into the Customer Form again... the database connection closes for some reason and the run time error comes up... the following code is highlighted when i debug.

mrsCustomers.Open pstCustomersSQL, gcnCoastandCountry, adOpenStatic, adLockOptimistic, adCmdText

here is the rest of my code

Module:
Option Explicit
'**Declare a New ADODB connection object variable
Public gcnCoastandCountry As New ADODB.Connection

Sub Main()
If Left(App.Path, 2) <> "\\" Then
    ChDrive App.Path
End If
ChDir App.Path
'**Open the connection to the database
gcnCoastandCountry.Open "Provider = Microsoft.jet.OLEDB.4.0; Data Source = CoastandCountry.mdb"
frmMainMenu.Show 1
End Sub

Sub CloseConnection()
gcnCoastandCountry.Close
Set gcnCoastandCountry = Nothing
End Sub

here is the Code on the MainMenu form:

Private Sub cmdCustomers_Click()
frmcustomers.Show 1
End Sub

here is the code on the Customer Form to go back the Main Menu form:

Private Sub cmdmainmenu_Click()
'**Close the recordset and database before exiting
mrsCustomers.Close
Set mrsCustomers = Nothing
Call CloseConnection
Unload frmcustomers
End Sub

Thanks,
Spiz
0
Comment
Question by:igotnosmoke
  • 5
  • 3
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 10916982
In what event is this ?
mrsCustomers.Open pstCustomersSQL, gcnCoastandCountry, adOpenStatic, adLockOptimistic, adCmdText
0
 

Author Comment

by:igotnosmoke
ID: 10917013
its in the Customer Form... Form Load
here the whole code

Private Sub Form_Load()
Dim pstCustomersSQL As String
frmcustomers.WindowState = 2
Call SetInactiveTextBoxes
pstCustomersSQL = "SELECT * FROM tblCustomers ORDER BY CustomerNumber"
mrsCustomers.Open pstCustomersSQL, gcnCoastandCountry, adOpenStatic, adLockOptimistic, adCmdText
If mrsCustomers.RecordCount > 0 Then
    Call DisplayData
Else
    'Call ClearData
End If
End Sub
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 10917037
Where did you declare mrsCustomers ?

Private Sub Form_Load()
on error goto LoadError
   Dim pstCustomersSQL As String
   dim mrsCustomers as ADODB.Recordset
   frmcustomers.WindowState = 2
   Call SetInactiveTextBoxes
   pstCustomersSQL = "SELECT * FROM tblCustomers ORDER BY CustomerNumber"
   mrsCustomers.Open pstCustomersSQL, gcnCoastandCountry, adOpenStatic, adLockOptimistic, adCmdText
   If mrsCustomers.RecordCount > 0 Then
       Call DisplayData
   Else
       'Call ClearData
   End If
   exit sub
LoadError:
   msgbox err.number & " " & err.description
End Sub

0
 

Author Comment

by:igotnosmoke
ID: 10917369
i declared mrsCustomers in the customers form

Option Explicit
'**Declare a modlule-level variable for the recordst which is to be used by more
'**than one procedure in the form module.
Private mrsCustomers As New ADODB.Recordset
Private mcocontrol As Control
Private mstmode As String
Private mstCustomerNumber As String
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 53

Expert Comment

by:Dhaest
ID: 10917405
You close your complete connection in your child-form

Private Sub cmdmainmenu_Click()
'**Close the recordset and database before exiting
mrsCustomers.Close
Set mrsCustomers = Nothing
'Call CloseConnection
Unload frmcustomers
End Sub

Remove the call CloseConnection !
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 10917686
0
 

Author Comment

by:igotnosmoke
ID: 10917936
Hey sorry about that i didnt know that you couldnt post more then 500... respond to this ill give u the 500
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 10918857
No problem at all. Glad I could help you out with your problems.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now