Results 1 to 4 of 4
  1. #1
    Brians440 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    3

    Create a field in a select query based upon created fields in the same query

    All,



    I'm very new to advanced Access queries and I'm trying to re-create the output from an Excel spreadsheet for a group. I've created two fields and would like to use those two field names to create a third in the same query. Is that possible? Here are the two fields I created.

    Fall: IIf(DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #8/15/2014# And #12/1/2014#,[HOURS],0)
    and
    Spring: IIf(DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #1/10/2015# And #5/5/2015#,[HOURS],0)

    Here's what I came up with in the interim:
    Total Hours: IIf(DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #8/15/2014# And #12/1/2014# Or DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #1/10/2015# And #5/5/2015#,[HOURS],0)

    I would rather have something like "Tot_hours: [Fall] + [Spring]". When I try this I get prompted for the value of "Fall".

    Thanks in advance for any help.

    Brian

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Why are you using DateSerial? Is WeekStartDate not a date/time field? How is date value structured?

    The references to Fall and Spring should work.

    Post the full SQL statement for analysis.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Brians440 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    3
    June7,

    Hey. Here is the format the report (.CSV) it comes out of a canned SSRS report, I'm just trying to make the process automated. (They're currently keying this info into Excel.)

    EMPLID,EmployeeName,WeekStartDate,Textbox17,HOURS, Textbox3,Textbox8,Textbox21,Textbox30,Textbox27,Te xtbox31,Textbox20
    1234567,"Smith, John",07/06/14 Summer,1,9.1,0,0,0,37.5,"2,532.9","103,401.6","2,5 32.9"
    1234567,"Smith, John",07/13/14 Summer,2,8.6,0,0,0,37.5,"2,485.9","103,401.6","5,0 18.8"

    Here is the query I'm trying to work with/create.

    SELECT tblimport_new.EMPLID, tblimport_new.EmployeeName, Sum(tblimport_new.HOURS) AS [Total Year], IIf(DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #8/15/2014# And #12/1/2014#,[HOURS],0) AS Fall, IIf(DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #1/10/2015# And #5/5/2015#,[HOURS],0) AS Spring, Sum(IIf(DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #8/15/2014# And #12/1/2014# Or DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #1/10/2015# And #5/5/2015#,[HOURS],0)) AS [Total Hours], [fall]-[spring] AS [Hours left to work]
    FROM tblimport_new
    GROUP BY tblimport_new.EMPLID, tblimport_new.EmployeeName, IIf(DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #8/15/2014# And #12/1/2014#,[HOURS],0), IIf(DateSerial(Mid([WeekStartDate],7,2),Left([WeekStartDate],2),Mid([WeekStartDate],4,2)) Between #1/10/2015# And #5/5/2015#,[HOURS],0), [fall]-[spring];

    Thanks ever so much for you help!

    Brian

  4. #4
    Brians440 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    3
    June7,

    Hey. It appears I had an ID10T error when I was writing the query. I didn't look at all the "HOURS" values. There's blank cells in there. So, thanks for your help in making me dig into the data and question my formulas. I'm glad it was my screw up....

    Brian

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

Similar Threads

  1. Replies: 4
    Last Post: 01-23-2015, 10:40 AM
  2. Replies: 1
    Last Post: 06-03-2014, 06:56 AM
  3. Replies: 2
    Last Post: 05-05-2013, 12:41 PM
  4. Replies: 1
    Last Post: 08-31-2012, 01:56 PM
  5. Create a variable from a query created in VBA
    By kaelcarp in forum Programming
    Replies: 4
    Last Post: 06-11-2010, 09:13 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