Re: fiter records in class module

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



You can run a temporary parameter query from a module. A parameter query
allows you to keep changing criteria in a loop.

The following code illustrates how to do this.

For help with creating an SQL string for your database, see the end of this
post.

Regards
Geoff

Public Sub FilterMyTable()

Dim objDB As DAO.Database
Dim objQDF As DAO.QueryDef
Dim objRS As DAO.Recordset

Dim strSQL As String
Dim intDateNo As Integer
Dim intIDNo As Integer
Dim datNextDate As Date
Dim lngNextID As Long

' Point to the current database:
Set objDB = CurrentDb()

' Create a temporary query
' by not giving it a name:
Set objQDF = objDB.CreateQueryDef("")

' Create an SQL statement for a
' parameter query. At this stage the
' SQL does not contain criterion values;
' only parameters, which we give values
' later:
strSQL = "PARAMETERS [Date] DateTime, " _
& "[ID] Long;" & vbNewLine _
& "SELECT Table1.*" & vbNewLine _
& "FROM Table1" & vbNewLine _
& "WHERE (((Table1.Date)=[Date]) " _
& "AND ((Table1.ID)=[ID]));"

' Set the query's SQL property:
objQDF.SQL = strSQL

' Loop through dates:
For intDateNo = 1 To 2
GoSub DoNextDate
Next

Bye:

GoSub CleanUp
Exit Sub

DoNextDate:

' We use the Choose function here simply
' as a device to process a different date
' for each iteration of the Date FOR-NEXT
' loop; we use the loop counter in Choose.
' Get the next date:
datNextDate = Choose(intDateNo, "1/1/06", "2/1/06")

' Put the next date into the Date
' parameter of the query:
objQDF.Parameters("Date").Value = datNextDate

' Loop through ID numbers:
For intIDNo = 1 To 4
GoSub DoNextIDNumber
Next

Return

DoNextIDNumber:

' We use the Choose function here simply
' as a device to process a different ID
' number for each iteration of the ID
' FOR-NEXT loop; we use the loop counter
' in Choose.
' Get the next ID number:
lngNextID = Choose(intIDNo, 100, 200, 300, 400)

' Put the next ID into the ID Parameter
' of the query:
objQDF.Parameters("ID").Value = lngNextID

' We've given the date and ID parameters a
' value, so now let's open a recordset
' using the query:
Set objRS = objQDF.OpenRecordset()

' See if recordset contains records:
If objRS.BOF And objRS.EOF Then
' No records. Simply close recordset
' and return to next iteration of loop.
objRS.Close
Return
End If

' Recordset contains records for the current date
' and ID. Do something with the records:
Do Until objRS.EOF
Debug.Print objRS.Fields![Date], objRS.Fields![ID]
objRS.MoveNext
Loop

' Close the recordset:
objRS.Close

Return

CleanUp:

Set objRS = Nothing
Set objQDF = Nothing
Set objDB = Nothing
Return

End Sub


HOW TO CREATE SQL STRING:

For your application, you'll need to replace the strSQL string variable.
To do this, follow these steps:

1. Create a temporary query in design view.

2. Add your Date and ID fields to the grid.

3. Enter the criterion for the Date field as [Date]

4. Enter the criterion for the ID field as [ID]

5. Open the Query menu and select Parameters.

6. In the first row, in the Parameter column, enter [Date]

7. Click in the Data Type column to the right of [Date] and use the
drop-down arrow to select Date/Time.

8. In the second row, in the Parameter column, enter [ID]

9. Click in the Data Type column to the right of [ID] and use the
drop-down arrow to select Long Integer (assuming that's the right data type
for your ID field).

10. Click OK to close the Parameter Queries dialog.

11. Open the View menu and select SQL view.

12. Highlight and copy all the SQL text.

13. Close the query and return to your code module.

14. Paste the SQL text into the strSQL variable. To do this, you may
find it easier to follow these steps:

(a) Click the mouse just after strSQL =
(b) Open the Add-Ins menu.
(c) Click VBA String Editor.
(d) Paste the SQL text into the Editor.
(e) Click Update.




"j" <j@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0C5BD6B8-3955-48CF-AF02-2AD242D67B30@xxxxxxxxxxxxxxxx
I am trying to filter an unindexed (database is too large for indexing)
database that matches certain fields. At first, I want to filter it based
on
date, then a series of identification numbers. How do I do this filtering
from inside a module? I do not want to use a query because I want to be
able
to loop the filter to search through many dates and then manipulate the
entries. Thank you.



.



Relevant Pages

  • Re: Help please on Record sets
    ... I tried running it from the query builder after deleting all the ... Just because your "original SQL string was generated in a working ... I had to add a quote to your first Formated Date and Change the quotes to ... recordset or would that lead to more problems? ...
    (microsoft.public.access.modulesdaovba)
  • Re: Need help with Code Please!!!
    ... posted in response to my last post was some SQL, but this is not the SQL that ... the actual string that gets built at the end of the SQ1 build process. ... The message says Syntax error in query expression ... Dim rs As Recordset 'object ref to qryCompany\USFNumber ...
    (microsoft.public.access.formscoding)
  • Re: ASP - FROM statement slows down connection to database
    ... Open your database in Access, switch to the Queries tab, create a new query ... in Design View without choosing a table, swtich to SQL View, paste the sql ... connection string rather than using an ... recordset open statements. ...
    (microsoft.public.inetserver.asp.db)
  • Re: Loading recordset at runtime
    ... difference in loading time for the entire recordset and the default 10 ... query or build the SQL string in VBA. ... Ah, ok, then use the forms "filter" option. ...
    (microsoft.public.access.forms)
  • Re: QueryDef
    ... >actual query because I need to pass a variable to it. ... >Dim dbs As Database, rst As Recordset, qdf As QueryDef ... A QueryDef is an object with an SQL property. ...
    (microsoft.public.access.modulesdaovba)