Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Shamli is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jul 2018
    Location
    California
    Posts
    142

    Text box restriction based on the values from two tables


    Hello Everyone
    I have 2 tables. Holiday table (Holiday date, Holiday Desc, company checkboxes) and Employee Table (EmpID, EmpName, Company).
    I am working on the timesheet management system.
    what I am trying to do is
    • Each contractor company has their own set of holidays.
    • I have to limit the employees not to enter hours on a holiday based on the company they are working for.
    • How should I link both the tables. Holiday and employee table (Employee table has the company information).
    • I used the below query. but this worked for the holidays without any company specific. (In general). How should I change the query to make my requirement work.


    Code:
    Private Sub Monday_AfterUpdate()
    Dim LResponse As Integer
    
     
    If Monday.Value > 0 And Text284 = DLookup("[HolidayDate]", "Holiday_T", "HolidayDate = #" & Format(Nz(Forms!Create_New_Entry_F!Text284, 0), "Short Date") & "#") Then
    LResponse = MsgBox("Please note this day is a holiday. Do you still want to charge on this day? ", vbYesNo, "CONTINUE")
    If LResponse = vbYes Then
    Me.Monday.SetFocus
    Else
    Me.Monday.Value = 0
    End If
       End If
    End Sub
    Holiday Table:
    Click image for larger version. 

Name:	Company.PNG 
Views:	11 
Size:	21.7 KB 
ID:	35529

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The H & L aren't companies, are they? I'd expect a table with fields for each holiday/company combination, and you just add a company criteria to your code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    The checkbox fields are a company - so company H and company L? Company H doesn't celebrate Christmas?

    You need the appropriate company checkbox field in the DLookup criteria. This might mean you need to first do a lookup on the employee to determine the company identifier.

    companyfield = some code to return "H" or "L"

    DLookup("[HolidayDate]", "Holiday_T", "HolidayDate = #" & Format(Nz(Forms!Create_New_Entry_F!Text284, 0), "Short Date") & "# AND " & companyfield)
    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.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,421
    include a column for the companyFK in the holiday table then by joining to the company table which in turn joins to the employee table you will get the relevant days. This does mean if you have 10 companies, the holiday dates will be allocated 10 times

    perhaps more sophisticated you could identify holidays which are relevant to all companies by setting the companyFK to 0, then modify your query to bring those through as well i.e. a cartesian query (no joins) with a criteria of something like

    WHERE (tblHolidays.companyFK=0 or tblHolidays.companyFK=tblCompanies.companyPK)

  5. #5
    Shamli is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jul 2018
    Location
    California
    Posts
    142
    Yes they are companies.
    I have changed H and L to company1 and company2


    Do you want me to change the table like this.

    HolidayDate HolidayDescription Firms
    1/1/2018 New Year's Day Company1
    1/15/2018 Birthday of Martin Luther King, Jr. Company2
    2/19/2018 Washington's Birthday Company1
    5/28/2018 Memorial Day Company2
    7/4/2018 Independence Day Company1
    9/3/2018 Labor Day Company2
    11/11/2018 Veterans Day Company1
    11/22/2018 Thanksgiving Day Company2
    12/25/2018 Christmas Day Company1
    12/24/2018 Christmas Eve Company2
    11/23/2018 Day after thanksgiving day Company1
    3/31/2018 Cesar Chavez Company2
    3/31/2018 Cesar Chavez Company1
    11/23/2018 Day after thanksgiving day Company2


    Quote Originally Posted by pbaldy View Post
    The H & L aren't companies, are they? I'd expect a table with fields for each holiday/company combination, and you just add a company criteria to your code.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That would be the normalized structure, and how I'd probably structure it. Ajax makes a suggestion on how to handle holidays everybody uses.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Shamli is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jul 2018
    Location
    California
    Posts
    142
    Yes they are company names.

    Company H doesn't celebrate Christmas?
    Lol I am using a sample to explain the problem

    Quote Originally Posted by June7 View Post
    The checkbox fields are a company - so company H and company L?

    You need the appropriate company checkbox field in the DLookup criteria. This might mean you need to first do a lookup on the employee to determine the company identifier.

    companyfield = some code to return "H" or "L"

    DLookup("[HolidayDate]", "Holiday_T", "HolidayDate = #" & Format(Nz(Forms!Create_New_Entry_F!Text284, 0), "Short Date") & "# AND " & companyfield)
    So I have to first create a Dlookup to check for the company name associated to the Employee and then another dlookup for the holiday.
    Is that right?

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    Going with the current data structure, that's my suggestion.
    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.

  9. #9
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,421
    if you are storing the companyfk/name in the employee table, you don't need to go to the company table to get the companyfk/name, you already have it. However dlookup code provided is missing a bit

    this

    ….AND " & companyfield

    should be

    ….AND Companyfield ='" & companyfield & "'"

    (assumes company field is text and is named the same in both tables)

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    Not with the company as yes/no type fields as shown in the original Holidays table. That's what I meant by 'current data structure'. If OP has changed the table then yes, my suggestion is not relevant.

    The first lookup would be to Employees table or a query that joins Employees to Companies to get the company (depends what exactly is stored in Employees - companyID or companyName - and exactly how the fields are named).
    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.

  11. #11
    Shamli is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jul 2018
    Location
    California
    Posts
    142
    I tried the below code:

    Code:
    Private Sub Monday_AfterUpdate()
    Dim LResponse As Integer
     
    If Monday.Value > 0 And Text284 = DLookup("[HolidayDate]", "Holiday_T", "HolidayDate = #" & Format(Nz(Forms!Create_New_Entry_F!Text284, 0), "Short Date") & "#") And Text295 = DLookup("[FIRMS]", "Holiday_T") Then
    LResponse = MsgBox("Please note this day is a holiday. Do you still want to charge on this day? ", vbYesNo, "CONTINUE")
    If LResponse = vbYes Then
    Me.Monday.SetFocus
    Else
    Me.Monday.Value = 0
    End If
       End If
    End Sub
    But its not showing any warning message.
    Text295 stores the company name from the employee Table.
    I think there is a problem with the Dlookup.
    Its not looking up the firm name. How can I change the query?
    And the company name is a text and it is same in both the tables.

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    Meaningful names for textboxes can be helpful. This second lookup will just find random record since there is no search criteria. But why a second lookup instead of additional criteria in the first?

    If DLookup does not find match it returns Null.
    Code:
    If Monday.Value > 0 And Not IsNull(DLookup("[HolidayDate]", "Holiday_T", "HolidayDate = #" & Format(Nz(Forms!Create_New_Entry_F!Text284, 0), "Short Date") & "# And Firms='" & Text295 & "'")) Then
    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.

  13. #13
    Shamli is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jul 2018
    Location
    California
    Posts
    142
    Thank you for your reply.
    Yeah. later realized to add an another criteria.
    But I am getting a type mismatch.(Run Time Error 13)
    Both the fields are in the Short text data type.
    Why is this error?
    Is the syntax wrong?

    Quote Originally Posted by June7 View Post
    Meaningful names for textboxes can be helpful. This second lookup will just find random record since there is no search criteria. But why a second lookup instead of additional criteria in the first?

    If DLookup does not find match it returns Null.
    Code:
    If Monday.Value > 0 And Not IsNull(DLookup("[HolidayDate]", "Holiday_T", "HolidayDate = #" & Format(Nz(Forms!Create_New_Entry_F!Text284, 0), "Short Date") & "# And Firms='" & Text295 & "'")) Then

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    Syntax looks fine. Are you sure HolidayDate is a date/time type and Firms is text type?
    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.

  15. #15
    Shamli is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jul 2018
    Location
    California
    Posts
    142
    yes .
    They are as mentioned.


    Click image for larger version. 

Name:	Company1.PNG 
Views:	9 
Size:	4.9 KB 
ID:	35543


    Click image for larger version. 

Name:	Emp.PNG 
Views:	9 
Size:	9.7 KB 
ID:	35544
    Quote Originally Posted by June7 View Post
    Syntax looks fine. Are you sure HolidayDate is a date/time type and Firms is text type?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 06-18-2017, 09:17 AM
  2. Restriction User Edits based on selected Login.
    By MikeEmerald in forum Access
    Replies: 6
    Last Post: 03-08-2017, 09:21 AM
  3. find values based on text criteria
    By boboivan in forum Access
    Replies: 2
    Last Post: 12-09-2015, 05:37 AM
  4. Replies: 2
    Last Post: 02-20-2014, 05:54 PM
  5. Filtering restriction on tables
    By pcasper in forum Access
    Replies: 3
    Last Post: 02-15-2012, 12:19 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