Re: The connection cannot be used to perform this operation. It is either closed or invalid in this context.



Several things:

1. This NG is about classical VB (i.e. VB6 or older). Not .NET stuff.
2. Using ADODB with .NET is very poor choice. You use either classical VB with ADODB, or .NET with ADO.NET.
3. Your code error: the Command object is not linked to the Connection object you just opened. You need:

cm=New ADODB.Command
cm.ActiveConnection=db
...

"mickey" <mickmarshall@xxxxxxxxxxxxx> wrote in message news:%23OP7yYI3IHA.3500@xxxxxxxxxxxxxxxxxxxxxxx
Hello All
I'm trying to read records from a .accdb file with vb.net (or vb6) but I get the above error when I try to to read the first record.
Can anyone tell me what I am am doing wrong?
Here is my code:

Imports Microsoft.VisualBasic
Imports System.io
Public Class Form1
Dim db As ADODB.Connection
Dim cm As ADODB.Command
Dim rs As ADODB.Recordset
Dim ConnectString As String
Dim sql As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try
ConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mickey Marshall\Documents\Database1.accdb;Persist Security Info=False;"
db = New ADODB.Connection
db.Open(ConnectString)
cm = New ADODB.Command
cm.CommandText = "SELECT Table1.[ID], Table1.[Picture], Table1.[Title] FROM Table1;"
sql = "SELECT Table1.[ID], Table1.[Picture], Table1.[Title] FROM Table1;"
Try
rs = cm.Execute(sql)
Catch ex As Exception
MsgBox(ex.Message)
Debug.Print(ex.Message)
End Try
Catch ex As Exception
MsgBox(ex.Message)

End Try


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cm = New ADODB.Command
cm.CommandText = "SELECT Table1.[ID], Table1.[Picture], Table1.[Title] FROM Table1;"
Try
rs = cm.Execute
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

End Class

Thanks
Mickey

.