Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO Trouble...Connection Closed?

Status
Not open for further replies.

bluenoser337

Programmer
Jan 31, 2002
343
0
0
CA
I am opening a connection (CN.Open...) in VB on "MDIForm_Load". When I try to "CN.Execute..." my insert, I get error "3704" telling me that the "Operation is not allowed when the object is closed"...but it isn't closed? A timer is activating the writing to the DB and if I put the "open" statement inside the timer each time, then it works (but is slow). I should only have to open this once...right? What am I missing? Thanks!!
 
Hi:

It's an attempt to write a record to the SQLServer DB from VB...

-- VB Code

Dim CN As New ADODB.Connection
Private Sub Timer1_Timer()
Dim strSQL As String
strSQL = " Insert Into Tea (Tea) Values ('" & txtName.Text & "')"
CN.Execute strSQL
End Sub

Private Sub Form_Load()
CN.Open "Provider=SQEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog= Coffee ;Data Source = XYZ"
End Sub

When CN.Execute strSQL happens, I get the error. If I open every time just before the strSQL, it works but I know this isn't right.
Thanks!
 
Make sure the definition is in scope while in the Subs. It appears to be out of scope when referencing in the Subs. Define the variable at a higher level and as public - maybe a Module or Form.

Public CN As New ADODB.Connection
 
Maybe you should start your timer after you have opened the connection. Durkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top