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

.



Relevant Pages

  • Whats wrong with my code ?
    ... I'm trying this code but I have a code error 9 ... Private Sub Cmd_Valid_Click ... Dim c As Integer ... MsgBox "the first cell for this month is" & LaPremiereDispo ...
    (microsoft.public.excel.worksheet.functions)
  • RE: COPYING ONE ROW TO ANOTHER SHEET AFTER CELL INPUT
    ... Private Sub Worksheet_Change ... Dim lr As Long, lr2 As Long ... I have sheet named ERRORS and on this sheet I have Column A:G ... I have sheet name CODE ERROR and on this sheet I have Column A:G just like ...
    (microsoft.public.excel.programming)
  • Re: Modules not Known
    ... Dim pCode$ ... Dim pCodeModule As CodeModule ... Private Sub VBIDEReference() ... Option Compare Database ...
    (comp.databases.ms-access)
  • A VB6 program to generate CONSTants for a user defined gradient
    ... EndProperty ... It may be used for amy moral purpose. ... Dim mGreen1 As Long ... Private Sub ColorDisplay_MouseDown(Button As Integer, Shift As Integer, _ ...
    (comp.lang.basic.visual.misc)
  • Re: access 2003
    ... Dim ctl As Control ... Dim rs As Recordset ... This sets the query definitions for choosing data to create an invoice using ... Event on combo box: Private Sub ChooseCust_AfterUpdate ...
    (microsoft.public.access.conversion)