473,386 Members | 1,775 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,386 software developers and data experts.

How to narrow a recordset by date range

I have been trying to select a recordset that filters on a text field for all records in the source table that are <= a specific date that I tried to specify earlier in the code. The select works fine without the AND date condition, but fails every time whenever I try to add a date.

My code is below - any help you can offer would be great. I am new to this. Thanks Ozy

Expand|Select|Wrap|Line Numbers
  1. Dim db As Database
  2. Dim recTest As DAO.Recordset
  3. Dim recBuys As DAO.Recordset
  4. Dim ttl As Double
  5. Dim pay As Double
  6. Dim tradedate As Date
  7. Set db = CurrentDb
  8.  
  9. 'specify tradedate for later use
  10.  
  11. Set recTest = db.OpenRecordset("SELECT * FROM RefRecords WHERE RefRecords.TranType = 'Pay' ORDER By RefRecords.TranAsset_TradeDate", dbOpenDynaset)
  12. recTest.MoveFirst
  13. Do While Not recTest.EOF
  14. With recTest
  15. pay = !TranAsset_ParAmount
  16. tradedate = !TranAsset_TradeDate
  17. End With
  18.  
  19. 'opens a dataset using SQL to select all preceding buys and stores CalcPAR total in ttl
  20.  
  21. Dim mySQL As String
  22. mySQL = "SELECT * FROM RefRecords WHERE (RefRecords.TranType)='Buy' AND"
  23. mySQL = mySQL + "" (RefRecords.TranAsset_TradeDate)<=tradedate"
  24.  
  25. Set recBuys = db.OpenRecordset(mySQL, dbOpenDynaset)
  26. recBuys.MoveFirst
  27. Do While Not recBuys.EOF
  28. With recBuys
  29. .FindNext !TranType = "Buy"
  30. ttl = ttl + !CalcPAR
  31. End With
  32. recBuys.MoveNext
  33. Loop
  34.  
Jun 18 '10 #1
5 5963
colintis
255 100+
in specifying the dates in the SQL, try putting a # before and after the dates. Such as

Expand|Select|Wrap|Line Numbers
  1. "WHERE #" & (RefRecords.TranAsset_TradeDate) & "# <= #" & tradedate & "#"
  2.  
If (RefRecords.TranAsset_TradeDate) & tradedate are table elements (not variables defined in vba), remove the [#" &] and [& "#] beside those variables.
Jun 18 '10 #2
Thanks colintis,

tradedate is a variable I define in VBA to store the tradedate of the first sell records. My goal is then to use tradedate as a condition to select all preceding buy records.

I don't quite understand the use of the [#"&] - can you explain further?
Jun 18 '10 #3
colintis
255 100+
@acoppini
In SQL any date format values except the table elements, must be quote with # such as:

Expand|Select|Wrap|Line Numbers
  1. Table.Date = #06/18/2010#
  2.  
Where 06/18/2010 is in the format of MM/DD/YYYY as SQL standard parameter. So any non table elements will need to be quote by # for identification as date.

The & is just a string combining function in VBA, usually when you try to declare the string in a mixture with some words and a variable such as:

Expand|Select|Wrap|Line Numbers
  1. Dim todayDate as String
  2.  
  3. todayDate = "Today is " & Date()
  4.  
As using + is incorrect as well. So in your second line of the mySQL string would be like this, with a ";" as the ending of SQL statement:

Expand|Select|Wrap|Line Numbers
  1. mySQL = mySQL & " RefRecords.TranAsset_TradeDate <= #" & tradedate & "#;"
  2.  
Hope my explanation won't make you more confused :P
Jun 18 '10 #4
@colintis
haha - you are awesome - i will try this out when I am back in the office on Monday - thanks so much!
Jun 18 '10 #5
NeoPa
32,556 Expert Mod 16PB
That's mainly right Colintis. You did a pretty good job of explaning :)

However, the '+' can be used as a concatenation character in Jet SQL (Accesses version of SQL), but it has a special meaning when dealing with Null values. See Using "&" and "+" in WHERE Clause.

Using date literals is described more fully in Literal DateTimes and Their Delimiters (#), though I can't think of anything wrong in what you said.
Jun 18 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Sunny K | last post by:
Hi guys, whilst working on a project which I thought was nearly complete I have come across a problem which was some how over seen, which I am hoping one of you guys know how to resovle. ...
1
by: John Rappold | last post by:
I've Googled this, but can't find an answer to my problem. When a user selects a page with the recordset I want it to list dates that fall into a certain range, related to CURDATE(). The range...
4
by: Mark | last post by:
Hi I have been trying to convert the week number to a range of dates that I can use. It should be fairly simple for you guru's out there but for us mere mortals it is beyond our grasp. I know...
1
by: isetea | last post by:
Hi, I want to create a from where user can select from a date range / type in a date range to get only data from an underlying query within this range. This should overwrite the existing criteria...
19
by: ali3n8 | last post by:
Hello I have attempted to create a date range report from a query called qrycustomerinformation. The field that contains the value of my date is called Followup. When i run a report on this it is...
12
smithj14
by: smithj14 | last post by:
I have a form to enter start and end dates then select a worker name to filter a report. This all works fine and when the report is open in preview mode it shows the date range in the txtboxes on the...
1
by: dlouche | last post by:
I want to get all the records from a table (no grouping) and order them first by a date range and then within that range order them by another column. For example, I have a table called Events: ID...
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
3
by: Vinda | last post by:
Hi Bytes, Using a previous question as a base Access 2000 Inserting multiple rows based on a date range. I also wanted to insert multiple rows into a table according to a date range supplied by a...
3
by: anuragrathor | last post by:
I have a "Date Range" Section, with these three items:- Last full week; Last full month; Last full quarter. I want to get the data depending upon the selection of the Date Range. If today is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
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...

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.