Results 1 to 4 of 4
  1. #1
    sandeebee123 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    7

    Help!! with my macro

    I'm trying to run a macro, but I keep getting this error.
    Compile error.in query expression '((([work hours].[Date Worked]) Between (Date()-Weekday(Date()))-6 And Date()-Weekday(Date()))'.

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    There's a problem with parenthesis, at least.
    Code:
     '(
       (
        ([work hours].[Date Worked]) 
         Between 
         (
          Date()-Weekday(Date())
         ) -6 
         And 
          Date()-Weekday(Date())   
       )'.
    try this instead
    Code:
       '(([work hours].[Date Worked]) 
         BETWEEN 
         (Date()-Weekday(Date()) -6 ) 
         AND 
         (Date()-Weekday(Date())    )   
        )'.

  3. #3
    sandeebee123 is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    7

    This is the actual macro:

    SELECT Employees.Supervisor, [Work Hours].[Full Name], [Work Hours].[Date Worked], [Work Codes].[Productivity Type], [Work Codes].WorkCode, Sum([Work Hours].Hours) AS Hours, [Work Hours].Description, [Work Hours].ManualCount, [Employees-Active].WindowsID AS SupWinID
    FROM (Employees INNER JOIN ([Work Hours] LEFT JOIN [Work Codes] ON [Work Hours].[Work Code] = [Work Codes].ID) ON Employees.[Full Name] = [Work Hours].[Full Name]) INNER JOIN [Employees-Active] ON Employees.Supervisor = [Employees-Active].[Employee Name]
    WHERE (((Employees.EmpType)="RS"))
    GROUP BY Employees.Supervisor, [Work Hours].[Full Name], [Work Hours].[Date Worked], [Work Codes].[Productivity Type], [Work Codes].WorkCode, [Work Hours].Description, [Work Hours].ManualCount, [Employees-Active].WindowsID
    HAVING ((([Work Hours].[Date Worked]) Between (Date()-Weekday(Date()))-6 And Date()-Weekday(Date())))
    ORDER BY Employees.Supervisor, [Work Hours].[Full Name], [Work Hours].[Date Worked];

  4. #4
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742

    A couple of Suggestions

    I don't see anything syntactically incorrect about it. One of the things I'd encourage you to do, when trying to figure out what is wrong with SQL, is to space it out, indent it, and give each field a line of its own.

    Copy it to the clipboard and do this in a blank notepad, since Access will "helpfully" squash it all back together into unreadable form every time you switch views.

    You can even give each parenthesis that is not part of an expression a line and indentation of its own, if it helps you know which one goes with which one.

    This can often help you catch goofs that are obvious to a compiler, but not to a human. No apparent goofs here, though.
    Code:
    SELECT 
       Employees.Supervisor, 
       [Work Hours].[Full Name], 
       [Work Hours].[Date Worked], 
       [Work Codes].[Productivity Type], 
       [Work Codes].WorkCode, 
       Sum([Work Hours].Hours) AS Hours, 
       [Work Hours].Description, 
       [Work Hours].ManualCount, 
       [Employees-Active].WindowsID AS SupWinID
    FROM 
       (Employees 
           INNER JOIN 
              ([Work Hours] 
              LEFT JOIN 
              [Work Codes] 
              ON [Work Hours].[Work Code] = [Work Codes].ID
              ) 
           ON Employees.[Full Name] = [Work Hours].[Full Name]
       ) 
          INNER JOIN 
             [Employees-Active] 
          ON Employees.Supervisor = [Employees-Active].[Employee Name]
    WHERE (((Employees.EmpType)="RS"))
    GROUP BY 
       Employees.Supervisor, 
       [Work Hours].[Full Name], 
       [Work Hours].[Date Worked], 
       [Work Codes].[Productivity Type], 
       [Work Codes].WorkCode, 
       [Work Hours].Description, 
       [Work Hours].ManualCount, 
       [Employees-Active].WindowsID
    HAVING 
       (  (  (
              [Work Hours].[Date Worked]
             ) 
                BETWEEN 
             (
              Date()-Weekday(Date()) 
             )-6 
                AND
              Date()-Weekday(Date())   
       )  )
    ORDER BY 
       Employees.Supervisor, 
       [Work Hours].[Full Name], 
       [Work Hours].[Date Worked];
    I don't like the look of that -6 sitting outside the parens, so I'd either move it inside the parens for clarity, or I'd more likely use the DateAdd function for even more clarity and certainty.

    Code:
    HAVING
       (   [Work Hours].[Date Worked] 
        BETWEEN 
           DateAdd ("d", -6 - Weekday(Date()), Date()) 
        AND 
           DateAdd ("d", - Weekday(Date()), Date()) 
       )
    Of course, it would also make sense to move all the HAVING clause up to the WHERE clause, since it's not testing against GROUP/aggregate data anyway.

    Code:
    WHERE 
       (   (   (Employees.EmpType)="RS" ) 
       AND (   [Work Hours].[Date Worked] 
            BETWEEN 
               DateAdd("d", - 6 - Weekday(Date()), Date()) 
            AND 
               DateAdd("d", - Weekday(Date()), Date()) 
           )
       )

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

Similar Threads

  1. Replies: 2
    Last Post: 06-30-2013, 09:59 AM
  2. Replies: 2
    Last Post: 05-05-2012, 02:34 AM
  3. Macro Help!
    By karanvemuri in forum Reports
    Replies: 5
    Last Post: 10-09-2011, 11:56 AM
  4. Help With Macro's
    By GDubbs780 in forum Programming
    Replies: 1
    Last Post: 02-25-2011, 11:43 AM
  5. Replies: 0
    Last Post: 01-12-2011, 12:43 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