Jeffrey Coachman
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!
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,
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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