Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Specify A Date Range

    I have weekly reports and in these weekly reports, there is a query that I have to specify a begin date (whatever Monday's date is) and an end date (whatever Sundays date is). I was going to try to instead of manually going into each database to open the query and run the query that way, build a form, and have an onclick event of the button that would execute this code:



    Code:
    'Opening the Database
    Set db = DBEngine.Workspaces(0).OpenDatabase("C:\Test\Test123\Test123db.mdb")
    'Running specified queries
    db.Execute "qry_check_weekly_statistics", dbFailOnError
    Set db = Nothing.
    And of course, I am opening about 15 different databases, and running the same query in each database. Is there a way to either 1) Only input the start/end date one time at the beginning of the run, and VB code can retain that start/end date and pull data from within that time frame for each subsequent query or 2) somehow programatically build a calander into VBA coding and when the query is run, in VBA have it only pull data for the previous 7 days (altho that sounds like it would be extremely difficult).

  2. #2
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    You have to 'open' each database - Why are they all closed?
    15 different databases? Are you talking tables or actual databases?
    Do you have the sql code for qry_check_weekly_statistics defined?
    Where are you collecting the query results? db.execute will simply run the query and close without placing them anywhere unless this is an Append/Update/Insert query.
    Why don't you create reports instead?
    That way, you can run reports from all 15 different databases in one procedure.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    I am actually having to open each database to run this query. It is an update query, so I don't need to see the results, I just need it run.

    I actually switched this to a Union Query and ran:
    Code:
    Select * from [Enter Query name] in '[Enter Query Location]'
    UNION ALL
    Select * from [Enter Query name] in '[Enter Query Location]'
    And now it is only asking me for the start date and end date one time, and it is applying that same start date and same end date to each query in the Union Query. I would still like to know if there is a way to have the date parameters auto update. Meaning, the start date will be whatever Mondays date is, and the end date will be whatever Sundays date is...The report run today's start date was 10/03/11 and the end date was 10/09/11, however the report from two weeks ago's start date was 09/26/11 and the end date was 10/02/11. Is there a way to code in SQL to have access only display the results of a 7 day time span. Maybe use a parameter like Today()-6 for the start date, and Today() for the end date or something like that (but I don't know if you can add something like that to a Union Query, since it is all coded in SQL not in design view)??
    Last edited by jo15765; 10-10-2011 at 09:41 AM.

  4. #4
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Instead of your parameters for StartDate and EndDate, place these:
    Code:
    SELECT Date()-Weekday(Date())+2 AS LastMonday, Date()-Weekday(Date())+8 AS NextSunday

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Just so I am clear....
    I would place that code in the source query SQL code, is that correct?
    And would that go at the beginning or the end of my SQL statement for the source query?

  6. #6
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Yes, you can post your query syntax if you want help.

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    This is the SQL code I am needing to insert that statement into (I shortened the select fields because there is over 40):
    Code:
    SELECT DataInfo.Field1 AS [Main ID], DataInfo.Field2 AS [Secondary ID], DataInfo.Field3 AS [Source Code] 
    FROM DataInfo
    WHERE (((DataInfo.Mailed_Date) Between [start] And [end]) AND ((DataInfo.Field1) Is Not Null) AND ((DataInfo.SalesManager) Like "*Torres*"));
    Can you show me where in the statement to add your modified date parameters?

  8. #8
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Use this:
    Code:
    SELECT DataInfo.Field1 AS [Main ID], 
    DataInfo.Field2 AS [Secondary ID], 
    DataInfo.Field3 AS [Source Code]  FROM DataInfo 
    WHERE (((DataInfo.Mailed_Date) 
    Between [Date()-Weekday(Date())+2] And [Date()-Weekday(Date())+8]) 
    AND ((DataInfo.Field1) Is Not Null) 
    AND ((DataInfo.SalesManager) Like "*Torres*"));

  9. #9
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    When I tried it that way, and I just pasted your code directly into my query, it is asking me to input the parameters for:
    Code:
    Date()-Weekday(Date())+2
    Date()-Weekday(Date())+8

  10. #10
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    I changed the code to:
    Code:
    Between Date()-Weekday(Date())+2 And Date()-Weekday(Date())+8
    And it is running the query now error free now!! Now just so I understand what exactly are the date functions telling the query to do? Can you break it down for me?

  11. #11
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Nice to know you sorted it out. Write these statements in query design and check the results when you run them:
    SELECT DATE() 'Returns today's date
    SELECT Weekday(Date()) 'Returns the weekday number for today where Sunday is 1 and Saturday is 7
    SELECT DATE()-Weekday(Date()) 'Returns last Saturday

  12. #12
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    If Sunday is 1 and Saturday is 7, then why is it + 8? To me that would be outside the range of valid days of the week...

    Actually I think I just picked up on it...The code you gave me is...
    Code:
    Between Date()-Weekday(Date())+2 And Date()-Weekday(Date())+8
    And if Weekday(Date()) Returns last Saturday then Date()-Weekday(Date())+2 Is essentially saying:
    Date() -- Today - Weekday(Date()) --- Saturday + 2 --- Monday
    So it is saying Today - (Saturday+2) --- which would be Monday

    And the plus 8 I was asking about above, is adding 8 days to Saturday to return the following Sunday...is that correct?

  13. #13
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Did you try what I suggested? Nothing gives a programmer more experience than doing hands-on work. Theory does not count. Try the sql statements I indicated and play around with them to see what you get. You are not trying to get the day of the week in your query, are you? What you want is a particular date for a particular Sunday. So think straight and don't get sidetracked by glittering issues.

  14. #14
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Correct, I don't want the date to display, I just want the data being displayed to be within those time constraints. I actually did just pick it up (I edited my last post with my findings), it was a bit confusing at first, but it is really quite simple. I honestly have to say I have never used the Weekday() feature in Access before so I am going to google and see what else you can do with it!

  15. #15
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Programmers use their imagination and creativity to perform some really strange tasks with some of the functions in their respective programming languages such that the original inventor of the language had not anticipated. You will be surprised as you come across some smart developers' code samples and some day you too may be able to conjure up something along the same lines. Hopefully. If you do not tire of experimenting.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Date range not working
    By victoria61485 in forum Queries
    Replies: 4
    Last Post: 09-08-2011, 08:56 AM
  2. Date range Q
    By markjet in forum Queries
    Replies: 13
    Last Post: 07-18-2011, 01:00 PM
  3. Run query by date range
    By ARickert in forum Access
    Replies: 2
    Last Post: 06-23-2011, 10:54 AM
  4. Date range
    By eacollie in forum Queries
    Replies: 7
    Last Post: 06-05-2011, 03:38 PM
  5. Date range help!!
    By bananatodd in forum Access
    Replies: 26
    Last Post: 10-18-2010, 01:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums