Results 1 to 4 of 4
  1. #1
    kkyork is offline Novice
    Windows XP Access 2010 64bit
    Join Date
    May 2012
    Posts
    7

    If Then Statements in Access

    Let me just first preface this by saying I am relatively new to access. Used it many years ago (version 2000) but have recently picked it back up again (now version 2010) so my database skills are a little rusty. Here's the situation I am trying to rectify and would love to get some perspective on it. I have a table of employee hours by date ("Employee Hours"). I also have another table which includes the start and end dates of each payroll period ("Payroll Calendar"). I would like to assign the employee hours to the corresponding payroll period in the "Employee Hours" table. If I were using excel I would have created a forumula that looks something like this...

    =IF(AND(Date1>=PayrollBeginDate1,Date1<=PayrollEnd Date1),Pay Period 1,IF(AND(Date1>=PayrollBeginDate2,Date1<=PayrollEn dDate2),Pay Period 2,if(AND(Date1>=PayrollBeginDate3,Date1<=PayrollEn dDate3),Pay Period 3,IF...etc,etc.

    Is there a way to do a similar function in access? I tried using the lookup but obvsiously having to key in 50,000+ entries is not really realistic.

    Thanks!

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Welcome to the forum!

    You should be able to do what you want without using any IF...THEN statements or IIF() functions. A subquery should be able to do what you want provided your tables are structured properly

    Could you provide the list of field names in the Calendar Payroll table? and Employee Hours?

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Using the DLookup function would probably work for you. Assuming the pay period can be identified by an integer number (which makes sense), then you could write a function something like this:

    function GetPayPeriod(paydate as date) as integer
    Dim FormattedPayDate as string
    FormattedPayDate = "#" & format(paydate,"mm/dd/yyyy") & "#"
    PeriodNumber = Dlookup("Period_ID","[Payroll Calendar]","[Start Date] <= " & FormattedPayDate & " AND [End Date] > " & FormattedPayDate)
    end function

    I use FormattedPayDate just to make the DLookup simpler, and to ensure that the date is in the format that DLookup requires, i.e. mm/dd/yyyy. For example, FormattedPayDate for today would be #05/14/2012# .

    I have made the assumption that a paydate the same as the Start Date or End Date of a pay period will return the number of the pay period that starts with your paydate.

    to use it : MyPayPeriod = GetPayPeriod(MyPayDate)

    HTH

    John

  4. #4
    kkyork is offline Novice
    Windows XP Access 2010 64bit
    Join Date
    May 2012
    Posts
    7
    Thanks everyone!

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

Similar Threads

  1. Replies: 3
    Last Post: 09-14-2011, 05:27 PM
  2. IIF() statements
    By ajetrumpet in forum Tutorials
    Replies: 0
    Last Post: 06-17-2011, 06:51 PM
  3. If statements
    By swagger18 in forum Programming
    Replies: 6
    Last Post: 01-28-2011, 08:13 PM
  4. IIF statements?
    By staceyo in forum Queries
    Replies: 15
    Last Post: 09-28-2010, 08:45 AM
  5. If statements
    By Ezeecopy in forum Access
    Replies: 0
    Last Post: 03-24-2009, 04:54 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