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

How do I Select Records from the Month 12 Months Hence

I am trying to create a query that pulls information that is 12 months out. I need this to change every month. The syntax I have now will only pull exactly 12 months out, i.e. today is 5 Dec 11 and it only shows the data from 05 Dec 12. I need the data for the entire month. Any help would be appreciated.
Dec 5 '11 #1
5 1761
NeoPa
32,556 Expert Mod 16PB
You give us very little to work with (Have you even read any of the notes indicating how to ask decent questions). I've changed the title to what I guess you mean to ask, as that also was invalid (Let me know if it's incorrect and what it should have been if so).

To answer I'll assume some typical and obvious names for your table layout and you will need to interpret them to suit your actual setup. I'll also assume you are looking for a filter (WHERE clause) to specify which records to process and which to ignore.

The following code is somewhat clumsy but is internationally independent (so won't get confused by Regional Settings - NB. These are not SQL Date literals so they will not adhere to the ANSI-92 SQL standard for those - See Literal DateTimes and Their Delimiters (#)) :

Expand|Select|Wrap|Line Numbers
  1. WHERE ([RecDate] Between CDate(Format(DateAdd('m',12,Date()),'1 mmm yyyy'))
  2.                  And     CDate(Formad(DateAdd('m',13,Date())-1,'d mmm yyyy'))
Dec 5 '11 #2
You have to build the WHERE clause in your SQL statement. To get this:

Expand|Select|Wrap|Line Numbers
  1. SELECT item, datefield FROM table WHERE ((datefield >= #12/1/2011#) AND (datefield <= #12/31/2011#));
I used the following:

Expand|Select|Wrap|Line Numbers
  1. Dim varMyDate As Variant
  2. Dim strSearch, strDate  As String
  3. varMyDate = Date
  4.  
  5. varMyDate = DateAdd("d", -(Day(varMyDate) - 1), varMyDate) 'Set the day at the first of the month
  6. Debug.Print varMyDate
  7. strDate = Format(varMyDate, "Short Date")
  8. Debug.Print strDate
  9. strSearch = "SELECT item, datefield FROM table WHERE ((datefield >= #" _
  10.             & strDate & "#) AND (datefield <= "
  11.  
  12. varMyDate = DateAdd("m", 1, varMyDate) ' move the date to the first of the next month
  13. varMyDate = DateAdd("d", -1, varMyDate)  ' move the day back one to the end of the current month
  14. Debug.Print varMyDate
  15. strDate = Format(varMyDate, "Short Date")
  16. strSearch = strSearch & "#" & strDate & "#));"
  17.  
  18. Debug.Print strSearch
Dec 5 '11 #3
NeoPa
32,556 Expert Mod 16PB
Scott, I appreciate the helpful impulse, but there are a number of problems with your suggestions :
  1. The question implies a query (QueryDef) is required rather than a SQL string, which is awkward (to say the least) to use if a display is required. Action queries and filters are generally more suited to creating SQL using VBA as you have here.
  2. Expand|Select|Wrap|Line Numbers
    1. Dim strSearch, strDate  As String
    You should understand that in VBA this code creates strSearch as a Variant type variable and not as a String type.
  3. Using the format (X <= Y) AND (X <= Z) is a clumsy way of saying (X Between Y And Z), and is particularly less desirable in situations where X is more than a simple field reference.
  4. Date literals in SQL, as you use in your illustration, should not be used in the way you have. They must be entirely unambiguous in Jet SQL, and that is not necessarily the case when you leave it to the Regional Settings to determine that format for you (You would have understood this already had you taken the time to follow the link attached in my earlier post (#2)).
  5. Nothing wrong with your logic though. The code would certainly produce the correct dates.

NB. I mention these, not in a spirit of criticism, but because people may read it without realising some of the drawbacks of following that guidance.
Dec 5 '11 #4
Alrighty. I'll take your word for it. I'm about a week or so into VBA for Access. I've been using '07, and DAO recordsets for just about everything. The search string builder I copied from a working sub and just changed around - mainly I replaced a chr$(34) with a hash inside the quotes (the chr$() was copied from where I had to insert quote marks inside of a search).

As for the format - true, but you can change it to Format(date, "mm/dd/yyyy") to get the date string in the desired order. Probably should have done that.

All that I know is that building a search string and throwing it inside of a OpenRecordset method seems to be working - both for strings and dates as search terms. When I get around to learning better, I'll use that.

Besides, the clever bit was the DateAdd part; everything else was just context.
Dec 6 '11 #5
NeoPa
32,556 Expert Mod 16PB
Scott W:
Besides, the clever bit was the DateAdd part; everything else was just context.
Ooops. Now you mention it, that only shows a span of a month, instead of the twelve months required. That said, the code does illustrate a good understanding of manipulating dates using DateAdd().

Hopefully, like the rest of us here, you were able to learn from simply attempting to help. Good for you :-)
Dec 7 '11 #6

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

Similar topics

3
by: William Gill | last post by:
I can't help but think I'm re-inventing the wheel if I have to code my own interface! Isn't there some script, php code, or something (modifiable / customizable) available that lets me select...
2
by: Danny | last post by:
How to allow users to select a set of records and then let them change a field for all these records at once? I would like to do this in code on a form. I will have a form with tabular view of...
1
by: arthur-e | last post by:
How can you select records based on more than one combo box - I have a combobox that selects records based on name (I'm sure this has been asked a thousand times - web site answer/link could be...
1
by: Dan Sikorsky | last post by:
How do you select records from one dataset and create a second dataset? I need to pull out the master records from a dataset and create a master datagrid in which to embed a details datagrid (for...
1
by: francophone77 | last post by:
What is the best way to setup a query to select records in specific months only. For instance if I want to compare sales in the month of May regardless of year. TIA
10
by: YvesDM | last post by:
Hi, How can I do this, I've got a little form with a dropdown. <select name="maand"> <option value='1'>januari</option> <option value='2'>februari</option> <option value='3'>maart</option>...
2
by: Hamayun Khan | last post by:
Hi all I am using the following query to select records for table Select JobTitle,JobDesc,Scraped,logoimage,JobPostID,SchoolID,web,MemType,InstitutionName,PayScale,LEA,Contract as...
2
by: rkferguson | last post by:
Hi Folks, I have a table that captures information about documents that leave the office. Some of those documents may get a signature and then return to the office. Need away to query the...
0
by: Gordon Padwick | last post by:
A form contains controls, one or more of which can be other forms. A form that contains another form is known as a main form. A form contained by a main form is known as a subform. A subform itself...
0
by: Elizabeth Mitte | last post by:
Hello, Thank you for the tutorial type article, is proving very useful. However, I have been following your instructions step by step and still get errors with the code? Trying to create the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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.