Results 1 to 6 of 6
  1. #1
    Backpacker is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Posts
    5

    SQL statement from multiple tables

    I am pretty new to access and am trying to create an SQL statement in Access 2007 with VBA. What I would like to do is create a list based on two different tables and criteria from each table. Here is a description of what I have:



    This part of the database is for scheduling people based on their qualifications and if they are available or not. There is a table called “tblUsers” which contains a list of users and their qualifications. There is another table “tblUserAvailability” which contains date/time periods that the user will be unavailable to work. This table could contain multiple entries per user for different dates and time blocks. The two tables are linked via a PK “UserID” on “tblUsers” and a FK “UserID” on “tblUserAvailability”

    The end goal is to have the query check to see if a user is qualified, then check to see if they have blocked out the time as unavailable and then finally make sure that they are not currently assigned to anything else during that time frame. Right now I am only working on getting the first two parts to work. Eventually there would likely be a third table to check based on current assignments.

    I have tried different setups for the query but keep getting strange results that don’t make a lot of sense to me. I tried using “WHERE” and flipping the greater than/less than signs, as well as different combinations of AND & OR. One main problem I have is that I need any user that is qualified to show up if they are currently available.

    The main problems are:
    1. Some users are not listed at all in “tblUserAvailability” if their schedule is currently wide open.
    2. Users with multiple entries show up multiple times even when “SELECT DISTINCT” is used based on them having multiple entries in “tblUserAvailability” it seems. The output that I want is just a list of who is available for an assignment during a certain time period.

    Here is what I have right now:

    Code:
     
    Private Function UserSearch()
    Dim strSQL As String 
    strSQL = "SELECT DISTINCT tblUsers.FirstName, tblUsers.LastName, tblUsers.UserID, " & _
              "FROM   tblUsers, tblUserAvailability " & _
              "WHERE NOT  tblUsers.Qual = False " & _
              "AND (Forms!frmSchedulingTest!txtStartDate + Forms!frmSchedulingTest!txtStartTime) <= (tblUserAvailability.EndDate + tblUserAvailability.EndTime) " & _
              "AND (Forms!frmSchedulingTest!txtEndDate + Forms!frmSchedulingTest!txtEndDate) >= (tblUserAvailability.StartDate + tblUserAvailability.StartTime) " & _
              "ORDER BY tblUsers.LastName"
    Me.lstSQLTest.RowSource = strSQL
    Me.lstSQLTest.Requery
    Me.lstSQLTest = Null
     
    End Function
    I am unsure if using an inner or outer join would be more correct here and how exactly to implement that. Any suggestions or advice would be appreciated.

    Thanks,
    Kevan

  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    There is a table called “tblUsers” which contains a list of users and their qualifications.
    This sounds like you should also have a table of qualifications and possibly a junction table. A user can have many qualifications and a qualification can be had by many users.

    I would use a saved query for the row source for the list box rather than using code to change the list box SQL. Since you posted the VBA, try this example:

    Code:
    Private Function UserSearch()
       Dim strSQL As String 
    
       strSQL = "SELECT DISTINCT tblUsers.UserID, tblUsers.FirstName, tblUsers.LastName"
       strSQL = strSQL & " FROM tblUsers LEFT JOIN tblUserAvailability ON tblUsers.UserID = tblUserAvailability.UserID_FK"
       strSQL = strSQL & " WHERE tblUsers.Qual = True"
       strSQL = strSQL & " AND ([StartDate]+[StartTime] <= [Forms]![frmSchedulingTest]![txtStartDate]+[Forms]![frmSchedulingTest]![txtStartTime])"
       strSQL = strSQL & " AND ([EndDate]+[EndTime] >= [Forms]![frmSchedulingTest]![txtEndDate]+[Forms]![frmSchedulingTest]![txtEndDate])"
       strSQL = strSQL & " ORDER BY tblUsers.LastName;"
    
       Me.lstSQLTest.RowSource = strSQL
       Me.lstSQLTest.Requery
       Me.lstSQLTest = Null
     
    End Function
    You might have to flip the "<" and ">" signs.

    I start writing SQL statements by using the query designer. Then, if necessary, I edit the SQL. For me, this way is easier and quicker.

    You had "WHERE NOT tblUsers.Qual = False " .
    I used "WHERE tblUsers.Qual = TRUE ". Is there a reason you used "NOT" ? Also, how do you determine which of the qualifications to use or decide if a user is qualified?

    Using "LEFT JOIN" will include a user if: they are qualified, but there are not any records in "
    tblUserAvailability".

  3. #3
    Backpacker is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Posts
    5
    ssanfu,

    Thanks for the assistance. I took your code and modified it slightly to work in the way that I think it should for the dates rules. I believe that there needs to be an OR, because you can't have both true at the same time generally.

    Quote Originally Posted by ssanfu View Post
    This sounds like you should also have a table of qualifications and possibly a junction table. A user can have many qualifications and a qualification can be had by many users.
    Quote Originally Posted by ssanfu View Post
    I would use a saved query for the row source for the list box rather than using code to change the list box SQL.
    I'll definitely look into setting it up like that.


    Quote Originally Posted by ssanfu View Post
    You had
    Quote Originally Posted by ssanfu View Post
    WHERE NOT tblUsers.Qual = False " . I used "WHERE tblUsers.Qual = TRUE ". Is there a reason you used "NOT" ?
    I couldn't give you a good reason, at the time it seemed necessary, but you can accomplish the same thing switching the signs and using an OR.

    Quote Originally Posted by ssanfu View Post
    Also, how do you determine which of the qualifications to use or decide if a user is qualified?
    Quote Originally Posted by ssanfu View Post
    Using "LEFT JOIN" will include a user if: they are qualified, but there are not any records in "tblUserAvailability".
    In the end, there would be a combo box to select the qualification, it is just simplified for this example.


    I have attached my modified version with your code.

    I am experiencing some issues though:

    1. It does not seem to display the qualified users that do not have entries on the availability table for some reason. For example the user "Jane Doe" should show up, but does not.
    2. For cases where there are date entries on multiple dates for a given user, the query will return them as being available, even when they are not, for example "John Doe" shows up if you put in 25 June 2011, as the date on the form because he has an entry on 21 June in the availability table.
    3. Lastly it does not seem to always sort out people correctly based on the times they are unavailable.

    Any other advice that you may have would be appreciated.

    Thanks,
    Kevan

  4. #4
    Backpacker is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Posts
    5
    I am still can’t seem to crack this problem. In order to hopefully clarify what I am trying to do, here are some examples of what I would like to happen.



    The main steps are:
    1. Check to see is the user is qualified
    2. Check to see if the user has any times that they cannot work as listed in tblUserAvailability. If they are free, then list them as available for that event. A problem here seems to be that if John Doe is unavailable on 6/25/2011, since there is an entry on 6/21/2011 that is outside of the event date range, it shows him as available.
    3. If a qualified person has no entries in tblUserAvailability then they are automatically available and should be listed as well. This does not seem to be occurring correctly for me.
    tblUserAvailability
    Code:
    AvailabilityID     UserID     LastName     FirstName     StartDate     EndDate     StartTime     EndTime
    11                 1          Doe          John          6/21/2011     6/21/2011   7:00:00 AM    9:00:00 AM
    12                 1          Doe          John          6/25/2011     6/25/2011   10:00:00 AM   12:00:00 PM
    15                 1          Doe          John          6/25/2011     6/25/2011   6:00:00 PM    8:00:00 PM
    16                 4          Smith        Bill          6/25/2011     6/25/2011   12:00:00 PM   8:00:00 PM
    tblUsers
    Code:
    UserID     LastName     FirstName     Qual
    1          Doe          John          Yes
    4          Smith        Bill          Yes
    5          Doe          Jane          Yes
    6          Schmoe       Joe           No
    Scenario 1
    Event Start Date: 6/25/2011, Event Start Time: 5:00 AM
    Event End Date: 6/25/2011, Event End Time: 6:00 AM

    Desired Results for Scenario 1
    In this case, Joe Schmoe is not included since he is not qualified, however all of the other users are qualified and do not have any other periods of unavailability.

    Available Personnel
    Code:
    LastName     FirstName
    Doe          John
    Doe          Jane
    Smith        Bill
    Scenario 2
    Event Start Date: 6/25/2011, Event Start Time: 10:00 AM
    Event End Date: 6/25/2011, Event End Time: 11:00 AM

    Desired Results for Scenario 2
    In this case Joe Schmoe is still not included, but John Doe is removed because he has an overlap with his 10:00 AM to 12:00 PM blocked out time. This only leaves Jane Doe and Bill Smith.

    Available Personnel
    Code:
    LastName     FirstName
    Doe          Jane
    Smith        Bill
    Scenario 3
    Event Start Date: 6/25/2011, Event Start Time: 5:00 PM
    Event End Date: 6/25/2011, Event End Time: 9:00 PM

    Desired Results for Scenario 3
    In this case Joe Schmoe is still not included. Now, John Doe is removed because he has an overlap with his 10:00 AM to 12:00 PM blocked out time, and Bill Smith is removed because of his 12:00 PM to 8:00 PM blocked out time. This only leaves Jane Doe as available.

    Available Personnel
    Code:
    LastName     FirstName
    Doe          Jane
    I would like this to be done in a query if possible, I just can’t seem to figure out how to accomplish this task. Any further ideas would really be helpful.

    Thanks,
    Kevan

  5. #5
    Backpacker is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Posts
    5
    I made some progress, making a few formatting changes to the SQL statement. It now works correctly as far as eliminating people from the list if they are not available.

    I do still experience these problems though:
    1. People that do not have any availability data should show up in the results, but don’t, i.e. Jane Doe.
    2. If someone is busy during the event, then they won’t show up because of that entry, however if they are listed in the availability table on another date then they will show up incorrectly.
    3. It seems that the distinct function does not work correctly as it will show multiple entries of the same person.

    Any other advice would be great.

    Thanks,
    Kevan

  6. #6
    Backpacker is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2011
    Posts
    5
    With the help of a coworker I think I was able to get it working the way it should. I am not sure if this is the best design of the query, but it appears to solve all of the previous problems.

    The main change made was to use a nested query to first make a list of all people that are unavailable based on tblUserAvailability, and then select all qualified users that were not in the subquery.

    I have attached the current version that appears to now work correctly.

    Thanks,
    Kevan

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

Similar Threads

  1. iif statement with multiple conditions
    By ragsgold in forum Queries
    Replies: 7
    Last Post: 08-24-2011, 05:38 PM
  2. Help with if statement to compare multiple fields
    By usmcgrunt in forum Programming
    Replies: 2
    Last Post: 12-01-2010, 06:43 PM
  3. multiple IF Statement
    By newtoAccess in forum Queries
    Replies: 3
    Last Post: 11-29-2010, 09:18 AM
  4. Multiple tables with the same value
    By Desverger in forum Programming
    Replies: 4
    Last Post: 08-31-2010, 12:21 PM
  5. multiple iif statement NEED HELP PLZ
    By scott munkirs in forum Reports
    Replies: 1
    Last Post: 09-27-2006, 05:21 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