473,421 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

OpenRecordset method

I am developing a program in Access 2000. I couldn't make OpenRecordset
method work right.

It's working when I opened a simple SQL query below in OpenRecordset.

.....
Dim dbs As Database, rst As Recordset

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset("Select * from ATest")
.....

But when I tried to use the value of Text Box object in a SQL query's
criteria, it warned me "Run-time error '3061', Too few parameters.
Expected 4". The SQL query is as below:

Select * from ATest
Where Date >[Forms]![myForm].txtDate

Thanks for your time and help.

Dec 11 '05 #1
5 19892
Came cross an article below that sorted out the problem.
URL:
http://msdn.microsoft.com/library/de...ml/sa00e19.asp

The answer is that you're invoking the Jet engine in a different
context here, and that makes all the difference. When you get data from
a parameter query that uses a form to supply the parameter via the
Access user interface, as in the earlier example, Access can evalute
the expression involved and supply a value to Jet. When you get data
from a parameter query that uses a form to supply the parameter via
VBA, instead of through a form, the bits of Access that manage user
interface matters aren't involved. Consequently, Jet is passed the
string "[Forms]![frmSelectCountry]![cboCountry]" instead of the value
in cboCountry. Because Jet doesn't know how to evaluate the
expression, it can't open the recordset.

Thanks

Dec 12 '05 #2
Database is a DAO object. By default, Access 2000 uses ADO. You've obviously
set a reference to Microsoft DAO 3.6 Object Library, or else the declaration
"Dim dbs As Database" would have raised an error, but when you did so, you
must not have unchecked the reference to Microsoft ActiveX Data Objects 2.1
Library (ADO)

When you have both references, you'll find that you'll need to
"disambiguate" certain declarations, because objects with the same names
exist in the 2 models. For example, to ensure that you get a DAO recordset,
you'll need to use Dim rst as DAO.Recordset (to guarantee an ADO recordset,
you'd use Dim rst As ADODB.Recordset)

The list of objects with the same names in the 2 models is Connection,
Error, Errors, Field, Fields, Parameter, Parameters, Property, Properties
and Recordset
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Sunnyrain" <so******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I am developing a program in Access 2000. I couldn't make OpenRecordset
method work right.

It's working when I opened a simple SQL query below in OpenRecordset.

....
Dim dbs As Database, rst As Recordset

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset("Select * from ATest")
....

But when I tried to use the value of Text Box object in a SQL query's
criteria, it warned me "Run-time error '3061', Too few parameters.
Expected 4". The SQL query is as below:

Select * from ATest
Where Date >[Forms]![myForm].txtDate

Thanks for your time and help.

Dec 12 '05 #3
On 11 Dec 2005 14:59:37 -0800, Sunnyrain wrote:
I am developing a program in Access 2000. I couldn't make OpenRecordset
method work right.

It's working when I opened a simple SQL query below in OpenRecordset.

....
Dim dbs As Database, rst As Recordset

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset("Select * from ATest")
....

But when I tried to use the value of Text Box object in a SQL query's
criteria, it warned me "Run-time error '3061', Too few parameters.
Expected 4". The SQL query is as below:

Select * from ATest
Where Date >[Forms]![myForm].txtDate

Thanks for your time and help.


You need to wrap the text control with the date delimiter #, and also
concatenate it into the where clause string, so that Access looks for
the Data within the control and not the literal text
"[Forms]![myForm].txtDate ".

(Select * from ATest Where DateField > #" & [Forms]![myForm].txtDate &
"#")

The form MyForm must be open.
If this code is placed in the form MyForm itself, you can use:
Where Date > #" & Me!txtDate & "#")

Note: Do you really have a field named date?
Date is a reserved Access/VBA/Jet word and should not be used as a
field name.
For additional reserved words, see the Microsoft KnowledgeBase article
for your version of Access:

109312 'Reserved Words in Microsoft Access' for Access 97
209187 'ACC2000: Reserved Words in Microsoft Access'
286335 'ACC2002: Reserved Words in Microsoft Access'
321266 'ACC2002: Microsoft Jet 4.0 Reserved Words'

--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
Dec 12 '05 #4
The openrecordset method expects a string as the SQL, so it interprets
[Forms]![myform ... as just part of that string and doesn't get the value of
the textbox. To get the value of the textbox in there, you have to do this:
sest rst= dbs.OpenRecordset("Select * from ATest Where Date > " &
[Forms]![myForm].txtDate)
hope that helps
-John

"Sunnyrain" <so******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I am developing a program in Access 2000. I couldn't make OpenRecordset
method work right.

It's working when I opened a simple SQL query below in OpenRecordset.

....
Dim dbs As Database, rst As Recordset

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset("Select * from ATest")
....

But when I tried to use the value of Text Box object in a SQL query's
criteria, it warned me "Run-time error '3061', Too few parameters.
Expected 4". The SQL query is as below:

Select * from ATest
Where Date >[Forms]![myForm].txtDate

Thanks for your time and help.

Dec 12 '05 #5
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in
news:fp********************@rogers.com:
Database is a DAO object. By default, Access 2000 uses ADO. You've
obviously set a reference to Microsoft DAO 3.6 Object Library, or
else the declaration "Dim dbs As Database" would have raised an
error, but when you did so, you must not have unchecked the
reference to Microsoft ActiveX Data Objects 2.1 Library (ADO)

When you have both references, you'll find that you'll need to
"disambiguate" certain declarations, because objects with the same
names exist in the 2 models. For example, to ensure that you get a
DAO recordset, you'll need to use Dim rst as DAO.Recordset (to
guarantee an ADO recordset, you'd use Dim rst As ADODB.Recordset)

The list of objects with the same names in the 2 models is
Connection, Error, Errors, Field, Fields, Parameter, Parameters,
Property, Properties and Recordset


It is best practice to disambiguate all such references:

Dim rs As DAO.Recordset

in all cases. That way, you never have to worry about the issue at
all.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Dec 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Philippa | last post by:
I'm trying to access data in vba using the openrecordset command. The data in on a SQL Server 2000 database, and I have linked tables to that data. the Table I'm trying to access is one of these...
6
by: Jonathan LaRosa | last post by:
I am trying to open a recordset and I am getting an error and I can't figure out why. See code below. sqlString2 does not work. sqlString does. Clearly the problem is with the nested SELECT...
1
by: jnikle | last post by:
I have a parameter query named "qry_employee_info_reports" that I need to run in the OnOpen event of a form. I'm after its total number of records. The query's got several joins in it, and one of...
2
by: MLH | last post by:
Set rstOtherTable = MyDB.OpenRecordset(xxx, dbOpenDynaset) I have a question regarding syntax of the above OpenRecordset method: Is it OK for xxx to reference both tables and queries when using...
1
by: RTKramer | last post by:
I am trying to pull data from a table in a Visual Basic program in MS Access using the OpenRecordSet method as follows: Dim dbs As Database Dim rst As Recordset Dim strSelect As...
7
by: waltvw | last post by:
I'm using FindFirst method in Access VBA to find a particular record in a recordset. I have 2 search criteria each of which works just fine if used separately as an argument in FindFirst, but NOT in...
3
by: Ashton12 | last post by:
Good day all. I am trying to use the OpenRecordSet method to update a field in a table upon the update of a separate table. My code looks like this: Private Sub NewLocation_AfterUpdate() Dim...
7
by: mirandacascade | last post by:
The questions are toward the bottom of this post. Situation is this: 1) Access 97 2) Multi-user appplication 3) SQL Server 2000 4) Sporadically (i.e. less than 1% of the time) encounter the...
2
by: modmans2ndcoming | last post by:
According to Access 2007, when I use the query builder, this sql code returns exactly what I want from my table Select reports. FROM reports WHERE (((reports.rptLable)=!!)); but when I am using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.