Solved

Can't open a Recordset?

Posted on 2006-11-17
1
229 Views
Last Modified: 2010-04-16
I am trying to open a recordset right after opening a connection. I keep getting the error message:

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

Here is my code.

Public Function OpenDBConnection() As Boolean
    Dim strConnectString As String
   
    On Error GoTo ErrProc
   
    strConnectString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" & App.Path & "\PT.mdb;Exclusive=1;Uid=admin;Pwd="
   
    If cn.State = 0 Then
        cn.Open strConnectString
        OpenDBConnection = True
    Else
        cn.Close
        cn.Open
        OpenDBConnection = True
    End If
   
    Exit Function
   
ErrProc:
    MsgBox Err & ": " & Error
End Function
'
'
'
Private Sub LoadEmployees()
    Dim rs As New ADODB.Recordset
   
    On Error GoTo ErrProc
   
    If OpenDBConnection Then
        rs.Open "tblEmployee", cn, adOpenKeyset, adLockReadOnly   <<<<<<<<<< ERROR MESSAGE >>>>>>>>>>
   
        If rs.RecordCount > 0 Then
            Do While Not rs.EOF
                cmbEmployee.AddItem rs!empLastName & ", " & rs!empFirstname
                rs.MoveNext
            Loop
        End If
    End If
   
    Exit Sub

ErrProc:
    MsgBox Err & ": " & Error
End Sub


I know the connection is open but I don't know why I can't open the recordset.

-Blake
0
Comment
Question by:BlakeMcKenna
1 Comment
 
LVL 3

Accepted Solution

by:
cdemir earned 500 total points
ID: 17971259
rs.Open "select * from tblEmployee", cn, adOpenKeyset, adLockReadOnly
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

832 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