Link to home
Start Free TrialLog in
Avatar of Jeffrey Coachman
Jeffrey CoachmanFlag for United States of America

asked on

Simple ADO Recordset, Run Time Error: 3709

Hello Experts,

I am having trouble with what I think is a simple ADO recordset. I am simply trying to count the number of records.

This code works fine:
Dim rstBooks As New ADODB.Recordset

    rstBooks.Open "SELECT * FROM [Books] WHERE [CategoryID]=2", CurrentProject.Connection, adOpenStatic, adLockReadOnly

    MsgBox rstBooks.RecordCount


HOWEVER, when I modify the code so that I can type in the CategoryID from a InputBox
the code fails on the "rstBooks.Open" line.  "RunTime Error: 3709 The connection can not be used to perfrom this operation. It is either closed or invalid in this context.".
Here is the code that fails:

Dim rstBooks As New ADODB.Recordset
Dim intWhatCategory As Integer
   
    intWhatCategory = InputBox("What Category: 1,2,3 or 4")
 
    rstBooks.Open "SELECT * FROM [Books] WHERE [CategoryID]= " & intWhatCategory & ", CurrentProject.Connection, adOpenStatic, adLockReadOnly"
   
    MsgBox rstBooks.RecordCount

I have used the syntax: " & VariableName & "  in other procedures before and it works fine. Why is it failing now?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Soluch
Soluch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeffrey Coachman

ASKER

Thanks for your speedy reply,

You were correct.

Funny thing though, when I left both quotes around the variable name, VBA put a closing " at the end of adLockReadOnly.

So when I deleted the closing quote at the end of the Variable it still gave me errors. I missed seeing the quotes at the end of the line!

Thanks again!

Works fine