Learn how to a build a cloud-first strategyRegister Now


Simple ADO Recordset, Run Time Error: 3709

Posted on 2004-11-23
Medium Priority
Last Modified: 2009-07-29
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?

Question by:Jeffrey Coachman

Accepted Solution

Soluch earned 500 total points
ID: 12662651

Try removing the "" marks after the variable, i.e. Change this:

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

To this:

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

I think this is where your error is coming from... if not please let me know.

LVL 74

Author Comment

by:Jeffrey Coachman
ID: 12662728
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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

804 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