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

    Date Function

    I don't know much about the Date() function and how to apply it.



    Scenario: I have a table called Household which holds main information, all i need from this is the HouseholdID. I also have a table called Attendence which has twelve yes/no fields labeled for each month ([Jan], [Feb],...).

    Problem: I need an IIf statement that asks if the current month(my variable)= Yes then return "Yes", else "No".

    i.e., Picked Up Current Month: IIf([Jul]=Yes, "Yes", "No") but changes from month to month instead of twelve queries.

    Any help will be appreciated.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The Date function just returns the current date.

    12 months is too many items for a nested IIf. Use Switch function, like:

    SELECT Switch(Month(Date())=1,"Jan",Month(Date())=2,"Feb" ,Month(Date())=3,"Mar",Month(Date())=4,"Apr",Month (Date())=5,"May",Month(Date())=6,"Jun",Month(Date( ))=7,"Jul",Month(Date())=8,"Aug",Month(Date())=9," Sep",Month(Date())=10,"Oct",Month(Date())=11,"Nov" ,Month(Date())=12,"Dec") AS CurMon, Switch(Month(Date())=1,[Jan],Month(Date())=2,[Feb],Month(Date())=3,[Mar],Month(Date())=4,[Apr],Month(Date())=5,[May],Month(Date())=6,[Jun],Month(Date())=7,[Jul],Month(Date())=8,[Aug],Month(Date())=9,[Sep],Month(Date())=10,[Oct],Month(Date())=11,[Nov],Month(Date())=12,[Dec]) AS Test
    FROM Household;
    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
    joewhitt is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    8
    That didn't work. It included all records as a Yes, even though some are not.

    I really just need it to return a list of households who have attended the current month. To filter my report of only the people who came that month and not the whole database.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Do you have a table that is storing months this way?

    Code:
    HouseHoldID Jan Feb Mar Apr ....
    1           Yes No  Yes  No
    2           No  No  No  Yes
    Why wouldn't your attendance table be set up with the

    Code:
    HouseholdID  AttendanceDate
    1            1/1/2011
    1            3/1/2011
    2            4/1/2011
    This would accomplish 2 things
    1. You could update this table forever without having to reuse any fields (when the new year rolls over)
    2. You could use the datepart("m",attendancedate) and datepart("yyyy", attendancedate) to see if a person had attended their monthly meeting for the current month or not

  5. #5
    joewhitt is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    8
    because of the users. households attend every month if they want, but user needs to know every month they have attended. (which i decided is a yes/no question, i dont know much about the date and its limitations). i only need the date because i want code to update every month. that way i just write it one time.

    my if statement works but i dont want to write twelve of them.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I did a test with data and records return with -1 or 0 as entered in the field returned by the Switch function. What I did not include in the query is a WHERE clause.

    SELECT ID, Switch(Month(Date())=1,"Jan",Month(Date())=2,"Feb" ,Month(Date())=3,"Mar",Month(Date())=4,"Apr",Month (Date())=5,"May",Month(Date())=6,"Jun",Month(Date( ))=7,"Jul",Month(Date())=8,"Aug",Month(Date())=9," Sep",Month(Date())=10,"Oct",Month(Date())=11,"Nov" ,Month(Date())=12,"Dec") AS CurMon
    FROM Household
    WHERE (((Switch(Month(Date())=1,[Jan],Month(Date())=2,[Feb],Month(Date())=3,[Mar],Month(Date())=4,[Apr],Month(Date())=5,[May],Month(Date())=6,[Jun],Month(Date())=7,[Jul],Month(Date())=8,[Aug],Month(Date())=9,[Sep],Month(Date())=10,[Oct],Month(Date())=11,[Nov],Month(Date())=12,[Dec]))=True));

    This query returns only those records that show attendance for the current month. So if I run this on last day of month I get records. If I run on first day I of course get nothing. If you want to run report to return data for a month other than that currently in, then need user input instead of the Month(Date()) input. Also, with your data structure, need a Year field, assuming you want to keep attendance info for all years. Then include the year as another criteria.

    So if this does not work for you then maybe my data sample is different from what you have and I need a sample of your data to test. And maybe I don't understand what you want. If you want to return records that have Yes in any month field, then need an expression like: IIf([Jan]=Yes ... Or [Dec]=Yes,True, False). Then criteria for this woud be =True.

    I agree with rpeare about data structure. What you seem to have is not normalized.
    Last edited by June7; 07-17-2011 at 07:01 PM.
    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.

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Quote Originally Posted by joewhitt View Post
    because of the users. households attend every month if they want, but user needs to know every month they have attended. (which i decided is a yes/no question, i dont know much about the date and its limitations). i only need the date because i want code to update every month. that way i just write it one time.

    my if statement works but i dont want to write twelve of them.
    Anything you can do with your data structure you can do with a normalized data structure and probably more efficiently over the long haul. If June's query works as you want that's great, but there really is no good reason to store the data as you have, your querying, table updating, record addition is going to be far more effective with a normalized structure.

  8. #8
    joewhitt is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    8
    Thanks June7 that worked. That's twice you've saved me.

    I like how you adjust your knowledge to fit the problem I had instead of showing me you are smarter and more experienced than I am.

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

Similar Threads

  1. Using a date range with Dcount function
    By mleberso in forum Reports
    Replies: 4
    Last Post: 06-17-2011, 08:56 AM
  2. Julian Date Function Help Please
    By campanellisj in forum Programming
    Replies: 3
    Last Post: 05-13-2011, 12:59 PM
  3. Date Function
    By tmcrouse in forum Queries
    Replies: 2
    Last Post: 12-02-2010, 08:53 AM
  4. Replies: 0
    Last Post: 07-15-2010, 11:26 PM
  5. Date Past Function
    By Laney in forum Access
    Replies: 4
    Last Post: 05-21-2008, 07:19 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