473,408 Members | 2,477 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,408 software developers and data experts.

DAO Recordset problem

I am using a DAO recordset based on a table on the back end, and the
table has almost 11,000 rows. Is there a way to 'filter' the table to
use only the rows that meet my criteria ? I know I could base the
recordset on a query, but I need the criteria to be based on 3 text
boxes on my main form. A command button on the main form, opens the
recordset and performs the actions on that recordset, but I think it
would be much more efficient to just loop through the records that
already meet the criteria. If I base the query on, for example, the
field strLocCode, where strLocCode = forms!frmMain!text1 (which is
the form containing the command button) and 2 other criteria, I get a
run-time error, and the message 'Parameter expected. 3 missing' (not
verbatim, but something close to that). Any help would be greatly
appreciated.
Jun 21 '06 #1
10 2015
Build the query statement dynamically, using just the criteria the user
entered. Your OpenRecordset() will then give you just the records you want.

Here's an example of how to build the SQL string:

Dim strSql As String
Dim strWhere As String
Dim lngLen As Long
Const strcStub = "SELECT * FROM Table1 "
Const strcTail = " ORDER BY Field1;"

If Not IsNull(Me.txtSurname) Then
strWhere = strWhere & "(Surname = """ & Me.txtSurname & """) AND "
End If
If Not IsNull(Me.txtBirthDate) Then
strWhere = strWhere & "(BirthDate = #" & Me.txtBirthDate & "#) AND "
End If
'etc for other boxes.

lngLen Len(strWhere) - 5 'Without trailing " AND ".
If lngLen <= 0 Then
strSql = strcStub & strcTail
Else
strSql = strcStub & "WHERE " & Left(strWhere, lngLen) & strcTail
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"LucaBrasi" <Lu*******@FishNap.com> wrote in message
news:449aeec6.3116484@news-server...
I am using a DAO recordset based on a table on the back end, and the
table has almost 11,000 rows. Is there a way to 'filter' the table to
use only the rows that meet my criteria ? I know I could base the
recordset on a query, but I need the criteria to be based on 3 text
boxes on my main form. A command button on the main form, opens the
recordset and performs the actions on that recordset, but I think it
would be much more efficient to just loop through the records that
already meet the criteria. If I base the query on, for example, the
field strLocCode, where strLocCode = forms!frmMain!text1 (which is
the form containing the command button) and 2 other criteria, I get a
run-time error, and the message 'Parameter expected. 3 missing' (not
verbatim, but something close to that). Any help would be greatly
appreciated.

Jun 21 '06 #2
Thank you very much..but I'm confused. What would the syntax for the
OpenRecordset be ? The query doesn't have a name, so how do I use
OpenRecordset ? I'm sorry if this is a stupid question, but I'm kind
of new to this, and it is 3:45 am here. Thanks again.

On Wed, 21 Jun 2006 15:35:59 +0800, "Allen Browne"
<Al*********@SeeSig.Invalid> wrote:
Build the query statement dynamically, using just the criteria the user
entered. Your OpenRecordset() will then give you just the records you want.

Here's an example of how to build the SQL string:

Dim strSql As String
Dim strWhere As String
Dim lngLen As Long
Const strcStub = "SELECT * FROM Table1 "
Const strcTail = " ORDER BY Field1;"

If Not IsNull(Me.txtSurname) Then
strWhere = strWhere & "(Surname = """ & Me.txtSurname & """) AND "
End If
If Not IsNull(Me.txtBirthDate) Then
strWhere = strWhere & "(BirthDate = #" & Me.txtBirthDate & "#) AND "
End If
'etc for other boxes.

lngLen Len(strWhere) - 5 'Without trailing " AND ".
If lngLen <= 0 Then
strSql = strcStub & strcTail
Else
strSql = strcStub & "WHERE " & Left(strWhere, lngLen) & strcTail
End If


Jun 21 '06 #3
"LucaBrasi" <Lu*******@FishNap.com> wrote in message
news:449aeec6.3116484@news-server...
A command button on the main form, opens the
recordset and performs the actions on that recordset, but I think it
would be much more efficient to just loop through the records that
already meet the criteria.


Couldn't you just open the filtered recordset in code, "perform the actions"
then requery your form?

Regards,
Keith.
www.keithwilby.com
Jun 21 '06 #4
Well, that was my question, 'How to open the filtered recordset'

On Wed, 21 Jun 2006 08:47:10 +0100, "Keith Wilby" <he**@there.com>
wrote:
"LucaBrasi" <Lu*******@FishNap.com> wrote in message
news:449aeec6.3116484@news-server...
A command button on the main form, opens the
recordset and performs the actions on that recordset, but I think it
would be much more efficient to just loop through the records that
already meet the criteria.


Couldn't you just open the filtered recordset in code, "perform the actions"
then requery your form?

Regards,
Keith.
www.keithwilby.com


Jun 21 '06 #5
You can OpenRecordset directly on a SQL string:
Dim rs As DAO.Recordset
Set rs = dbEngine(0)(0).OpenRecordset(strSql)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"LucaBrasi" <Lu*******@FishNap.com> wrote in message
news:4498f8e6.5708546@news-server...
Thank you very much..but I'm confused. What would the syntax for the
OpenRecordset be ? The query doesn't have a name, so how do I use
OpenRecordset ? I'm sorry if this is a stupid question, but I'm kind
of new to this, and it is 3:45 am here. Thanks again.

On Wed, 21 Jun 2006 15:35:59 +0800, "Allen Browne"
<Al*********@SeeSig.Invalid> wrote:
Build the query statement dynamically, using just the criteria the user
entered. Your OpenRecordset() will then give you just the records you
want.

Here's an example of how to build the SQL string:

Dim strSql As String
Dim strWhere As String
Dim lngLen As Long
Const strcStub = "SELECT * FROM Table1 "
Const strcTail = " ORDER BY Field1;"

If Not IsNull(Me.txtSurname) Then
strWhere = strWhere & "(Surname = """ & Me.txtSurname & """) AND "
End If
If Not IsNull(Me.txtBirthDate) Then
strWhere = strWhere & "(BirthDate = #" & Me.txtBirthDate & "#) AND "
End If
'etc for other boxes.

lngLen Len(strWhere) - 5 'Without trailing " AND ".
If lngLen <= 0 Then
strSql = strcStub & strcTail
Else
strSql = strcStub & "WHERE " & Left(strWhere, lngLen) & strcTail
End If

Jun 21 '06 #6
Last question..promise ! Even though I am not directly using the
table, can I still do edits and updates ? such as

rst1.edit
rst1!strField1 = "Shoelace"
rst1.update

etc..

On Wed, 21 Jun 2006 15:54:44 +0800, "Allen Browne"
<Al*********@SeeSig.Invalid> wrote:
You can OpenRecordset directly on a SQL string:
Dim rs As DAO.Recordset
Set rs = dbEngine(0)(0).OpenRecordset(strSql)


Jun 21 '06 #7
Sure. If the query statement represents an editable query, you can edit it.

This technique (dynamic SQL strings, and WHERE clauses) underlies most of
the code I write in Access databases.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"LucaBrasi" <Lu*******@FishNap.com> wrote in message
news:449afb9a.6399937@news-server...
Last question..promise ! Even though I am not directly using the
table, can I still do edits and updates ? such as

rst1.edit
rst1!strField1 = "Shoelace"
rst1.update

etc..

On Wed, 21 Jun 2006 15:54:44 +0800, "Allen Browne"
<Al*********@SeeSig.Invalid> wrote:
You can OpenRecordset directly on a SQL string:
Dim rs As DAO.Recordset
Set rs = dbEngine(0)(0).OpenRecordset(strSql)

Jun 21 '06 #8
Thank you, very helpful. And I appreciate your patience.
On Wed, 21 Jun 2006 16:15:43 +0800, "Allen Browne"
<Al*********@SeeSig.Invalid> wrote:
Sure. If the query statement represents an editable query, you can edit it.

This technique (dynamic SQL strings, and WHERE clauses) underlies most of
the code I write in Access databases.


Jun 21 '06 #9
"LucaBrasi" <Lu*******@FishNap.com> wrote in message
news:4499f9dd.5955609@news-server...
Well, that was my question, 'How to open the filtered recordset'


Something like (off the top of my head and untested):

Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
Set db = CurrentDb
strSQL = "Select * from tblMyTable where [MyField] = "whatever" and ...etc"

Set rs = db.OpenRecordset(strSQL)

With rs
.MoveFirst
Do Until .EOF
.Edit
![MyField] = "whatever"
.Update
.MoveNext
Loop
End With

rs.Close
Set rs = Nothing

Me.Refresh
Jun 21 '06 #10
LucaBrasi wrote:
Well, that was my question, 'How to open the filtered recordset'


I think your question was, "How to open and edit a filtered recordset?"

The answer is, "Don't!"

Update the Table by executing a dynamic SQL statement:

DbEngine(0)(0).Execute "UPDATE GoodPractice SET WhentoUseRecordsets =
'Never' WHERE Descriptor = 'KISS'"

Jun 21 '06 #11

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

Similar topics

0
by: gary artim | last post by:
Hi All, I have a problem using DBIx::RecordSet. I get correct results but continue to get these messages on stderr. I looked at Compat.pm and it seems to be pointing out a problem with my call...
0
by: belacyrf | last post by:
Here's the code: ------------------------------------------------------------------- accessID = request("accessID") strSQL = "SELECT * From PendingAccRequests Where AccessID = "&accessID ...
4
by: Gerry Abbott | last post by:
Hi all. I wish to call a recordset from a function. Ive tried the following approach, -------------------------------------------------------- Function PassRS() As Recordset Dim db As...
6
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query...
36
by: kjvt | last post by:
Based on a prior posting, I've written a function to convert a recordset to a dataview. The first call to the function for a given recordset works perfectly, but the second call always returns a...
5
by: zMisc | last post by:
Are there any tricks in updaitng a JOINed recordset? I joned to tables and when I try to change a field on the recordset and update it, I get this error: "Unknown column 'CCDE' in 'where...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
0
by: PCroser | last post by:
I have encountered a problem when querying a table. The query passed data into a recordset which should have resulted in many records but has returned EOF. After debugging the code the only...
0
by: Yarik | last post by:
Hello, Here is a sample (and very simple) code that binds an Access 2003 form to a fabricated ADO recordset: ' Create recordset... Dim rs As ADODB.Recordset: Set rs = New ADODB.Recordset '...
2
by: wallconor | last post by:
Hi, I am having a problem using Dreamweaver CS3 standard recordset paging behavior. It doesn’t seem to work when I pass parameter values from a FORM on my search page, to the recordset on my...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.