Results 1 to 5 of 5
  1. #1
    dssrun is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2010
    Posts
    83

    Queries Criteria with date

    Below is a SQL statement that works, but I do not want to rely on the "Date()" criteria. Rather I want to rely on that records [Date]. For example if Record1 had a date of 3/2010, I want to use that dates ending balance number and the previous four months charges in my calculation based on what that [date] value is in the record. How do I go about doing this? (I am doing this to update all records with this calculation and with my current calculation I can only do the most recent records.)

    Code:
    SELECT [Table1].[Service Date], [Table1].[ending balance]/(Sum([Table1_1].[Charge])) AS [Bucket], [Facility].[A]
    
    FROM (Facility INNER JOIN Table1_1 ON Facility.[A] = Table1_1.[A]) INNER JOIN [Table1] ON Facility.[A] = [Table1].[A]
    
    WHERE [Table1].[Service Date])=DateAdd("m",-1,Date()) AND Table1_1.[Service Date] Between DateAdd("m",-4,Date()) And Date()
    GROUP BY Facility.[A], [Table1].[Service Date],[Table1].[Ending Balance];


  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Replace each Date() with a reference to a control on a form that has the date you want to use. I have a control that I named "ViewDate".

    Using this control, I can make the "current day" in my mdb to appear to be long in the past.

    old
    Code:
    WHERE [Table1].[Service Date])=DateAdd("m",-1,Date()) AND Table1_1.[Service Date] Between DateAdd("m",-4,Date()) And Date()
    new
    Code:
    WHERE [Table1].[Service Date])=DateAdd("m",-1,Forms!FormName.ViewDate) AND Table1_1.[Service Date] Between DateAdd("m",-4,Forms!FormName.ViewDate) And Forms!FormName.ViewDate
    (this is air code - I didn't test it.)

  3. #3
    dssrun is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2010
    Posts
    83
    So the only way to get this is to create a form and reference it?

  4. #4
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by dssrun View Post
    So the only way to get this is to create a form and reference it?

    AFAIK, you have three choices if you want change a parameter in a query:

    1) Refer to a control on a form

    2) Edit a hard coded parameter

    3) Use an unknown name enclosed in brackets, so that a message box pops up asking for the value.

    Options 2 & 3 are a real PITA.
    This is an example of option 3:

    Code:
    WHERE [Table1].[Service Date])=DateAdd("m",-1,[Enter a date]) AND Table1_1.[Service Date] Between DateAdd("m",-4,[Enter a date]) And [Enter a date]

  5. #5
    dssrun is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2010
    Posts
    83
    I played around with it a little more and got it to work through 2 queries, though I still need to fix it to where if there are not three months of charges do not calculate it, but that is for another day. Query 2 just groups the ending balance and sum the charge.

    Query 1:
    Code:
    SELECT tbl1.[Service Date], tbl1_1.[Service Date], tbl1.[Ending balance], Sum(tbl1_1.[Charge]) AS [SumOfTotal Charge],
    FROM tbl1 INNER JOIN (tbl1 AS tbl1_1 INNER JOIN [Facility] ON tbl1_1.[A] = [Facility.[A]) ON tbl1.[A] = [Facility].[A]
    GROUP BY tbl1.[A], tbl1.[Service Date], tble1_1.[Service Date], tbl1.[Ending Balance]
    HAVING (((tble1_1.[Service Date]) Between DateAdd("m",-2,[tbl1].[service date]) And DateAdd("m",0,[tbl1].[service date]))

Please reply to this thread with any new information or opinions.

Similar Threads

  1. crosstab queries (criteria) won't work
    By Rich P in forum Queries
    Replies: 1
    Last Post: 02-15-2011, 10:53 AM
  2. >Date() criteria
    By BED in forum Queries
    Replies: 3
    Last Post: 12-15-2010, 03:29 AM
  3. Criteria for date due
    By Desstro in forum Queries
    Replies: 4
    Last Post: 11-30-2010, 12:54 PM
  4. Replies: 1
    Last Post: 07-02-2010, 03:55 AM
  5. Date Criteria
    By tcollins02 in forum Queries
    Replies: 1
    Last Post: 09-09-2008, 08:27 AM

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