473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

if Data exist for an Emp. ID who already submitted same type leave between two dates

I have a table named [tabLeave] in ms access with following fields:-
1. eeID = Number
2. Emp_LeaveType = Text
3. Leave_From = Date [dd-mmm-yy]
4. Leave_Upto = Date [dd-mmm-yy]
5. LeaveEntryType = Text

I also have unbound form named [frmLeave] with following fields where from I submit data in this table.

1. eID = Number
2. L_Type = Text
3. Start_Date = Date [dd-mmm-yy]
4. End_Date = Date [dd-mmm-yy]
5. EntryType = Text

So my question is that what would be vba code (after update) with Start_Date and End_Date after filling up eID and L_Type with same EntryType so that A message box could appear to prevent duplicate data with same leave period if leave period already exist in tabLeave with all 5 fields match.

Thanks in advance.
Attached Images
File Type: jpg tabLeave Datasheet View.jpg (42.5 KB, 222 views)
File Type: jpg tabLeave Design View.jpg (17.6 KB, 225 views)
File Type: jpg Unbound Form 1.jpg (8.4 KB, 128 views)
File Type: jpg Unbound Form.jpg (18.9 KB, 194 views)
Jan 20 '16 #1

✓ answered by Seth Schrock

You will actually have to perform two tests, one for Start_Date and one for End_Date. I would probably just do two DCount() functions. So something like this (air code):
Expand|Select|Wrap|Line Numbers
  1. Dim strStartCriteria As String
  2. Dim strEndCriteria As String
  3.  
  4. strStartCriteria = "eeID = " & Me.eID & " And Emp_LeaveType = '" & Me.L_Type & _
  5.                    "' And Leave_From <= #" & Me.Start_Date & "# And Leave_Upto > #" & Me.Start_Date & "#"
  6. strEndCriteria = "eeID = " & Me.eID & " And Emp_LeaveType = '" & Me.L_Type & _
  7.                  "' And Leave_From <= #" & Me.End_Date & "# And Leave_Upto > #" & Me.End_Date & "#"
  8.  
  9. If DCount("*", "tabLeave", strStartCriteria) > 0 Or DCount("*", "tabLeave", strEndCriteria) > 0 Then
  10.     'Duplicated leave
  11. End If

16 1148
Seth Schrock
2,965 Expert 2GB
You will actually have to perform two tests, one for Start_Date and one for End_Date. I would probably just do two DCount() functions. So something like this (air code):
Expand|Select|Wrap|Line Numbers
  1. Dim strStartCriteria As String
  2. Dim strEndCriteria As String
  3.  
  4. strStartCriteria = "eeID = " & Me.eID & " And Emp_LeaveType = '" & Me.L_Type & _
  5.                    "' And Leave_From <= #" & Me.Start_Date & "# And Leave_Upto > #" & Me.Start_Date & "#"
  6. strEndCriteria = "eeID = " & Me.eID & " And Emp_LeaveType = '" & Me.L_Type & _
  7.                  "' And Leave_From <= #" & Me.End_Date & "# And Leave_Upto > #" & Me.End_Date & "#"
  8.  
  9. If DCount("*", "tabLeave", strStartCriteria) > 0 Or DCount("*", "tabLeave", strEndCriteria) > 0 Then
  10.     'Duplicated leave
  11. End If
Jan 21 '16 #2
jforbes
1,107 Expert 1GB
I would create a Function that tests for the overlap and put it in the BeforeUpdate Event of the Form, having it Cancel the Save if there is an overlap.

I think you can make the function out of a few DCounts. I would looks something like the following. This is air code, you'll have to take ownership of it:
Expand|Select|Wrap|Line Numbers
  1. Public Function testOverlap() As Boolean
  2.  
  3.     Dim dLeave_From As Date
  4.     Dim dLeave_Upto As Date
  5.     Dim leeID As Long
  6.  
  7.     If IsDate(Me!Leave_From) And IsDate(Me!Leave_Upto) Then
  8.         leeID = Me!eeID
  9.         dLeave_From = Me!Leave_From
  10.         dLeave_Upto = Me!Leave_Upto
  11.  
  12.         ' Default Test to Passing, then look failure points
  13.         testOverlap = True
  14.         If testOverlap Then If DCount("eeID", "tabLeave", "Leave_From<#" & dLeave_From & "# AND dLeave_Upto>#" & dLeave_From & "#") Then testOverlap = False
  15.         If testOverlap Then If DCount("eeID", "tabLeave", "Leave_From<#" & Leave_Upto & "# AND dLeave_Upto>#" & Leave_Upto & "#") Then testOverlap = False
  16.         '...
  17.         ' Any other testing you want to perform
  18.         '...
  19.     Else
  20.         testOverlap = False
  21.     End If
  22.  
  23. End Function
HAHA, Crossposted with Seth, good times, good times.
Jan 21 '16 #3
Thanks to Seth and jforbes for reply

you both not added LeaveEntryType in your formula..may be i Forgot it to mention.
LeaveEntryType is also a text box in table and form which describes about Leave Entry is monthly, PUC, Adjustment etc.

So I tried following code of Seth with above field amendment but It not worked. If I tried formula of "Seth" without LeaveEntryType then it works fine for me. So kindly solve my issue with adding above field..

Here's my code.

Expand|Select|Wrap|Line Numbers
  1. 'My other coding
  2.  
  3. Else
  4. strStartCriteria = "eeID = " & Me.LeID & " And Emp_LeaveType = '" & Me.L_Type & " And LeaveEntryType = '" & Me.LeaveEntryType & _
  5.                    "' And Leave_From <= #" & Me.Start_Date & "# And Leave_Upto > #" & Me.Start_Date & "#"
  6. strEndCriteria = "eeID = " & Me.LeID & " And Emp_LeaveType = '" & Me.L_Type & " And LeaveEntryType = '" & Me.LeaveEntryType & _
  7.                  "' And Leave_From <= #" & Me.End_Date & "# And Leave_Upto > #" & Me.End_Date & "#"
  8.  
  9.     If DCount("*", "tabLeave", strStartCriteria) > 0 Or DCount("*", "tabLeave", strEndCriteria) > 0 Then
  10.         MsgBox " Duplicate Leave"
  11.         Me.End_Date = ""
  12.         Me.End_Date.SetFocus
  13.     Else
  14.         Me!NoDays.Value = Nz((End_Date - Start_Date), 0) + 1
  15.         Me!Leav_Close.Value = Nz(([Leav_Open] - [NoDays]), 0)
  16.     End If
  17. End If
Jan 21 '16 #4
Seth Schrock
2,965 Expert 2GB
Sorry, I guess I read entry type and confused that with the leave type. Here would be the criteria then.
Expand|Select|Wrap|Line Numbers
  1. strStartCriteria = "eeID = " & Me.eID & " And Emp_LeaveType = '" & Me.L_Type & _
  2.                    "' And Leave_From <= #" & Me.Start_Date & "# And Leave_Upto > #" & Me.Start_Date & "#" & _
  3.                    " And LeaveEntryType = '" & Me.EntryType & "'"
  4. strEndCriteria = "eeID = " & Me.eID & " And Emp_LeaveType = '" & Me.L_Type & _
  5.                  "' And Leave_From <= #" & Me.End_Date & "# And Leave_Upto > #" & Me.End_Date & "#" & _
  6.                  " And LeaveEntryType = '" & Me.EntryType & "'"
Jan 21 '16 #5
Sorry to post... I tried after correct in above code and its works fine... but there a problem.

Problem is that an employee already applied a leave type for 01-Jan-16 to 05-Jan-16.. now I goes to apply for leave from 01-Jan-16 to any further date then code prevent it. but it not prevent if I applied 02-Jan-16 to any future date Or 30-Dec-15 to any future date. This code works only if My start date is same with existing database date for above criteria.

So need some guidelines in this regard.


Thanks again.
Jan 21 '16 #6
My full code is following:-

Expand|Select|Wrap|Line Numbers
  1. Private Sub End_Date_AfterUpdate()
  2.  
  3. On Error GoTo ErrorHandler
  4.  
  5. Dim strStartCriteria As String
  6. Dim strEndCriteria As String
  7. Dim HPLBal As String
  8. Dim StartDt As Date
  9. Dim EndDt As Date
  10.  
  11. If IsNull(L_Type) Then
  12. MsgBox " Enter Leave Type"
  13. End_Date = ""
  14. Exit Sub
  15.  
  16. ElseIf IsNull(Start_Date) Then
  17. MsgBox " Enter Leave Starting Date"
  18. End_Date = ""
  19. Exit Sub
  20.  
  21. ElseIf End_Date < Start_Date Then
  22. MsgBox " Leave End Date can't be Less then Leave starting Date...Kindly check."
  23. End_Date = ""
  24. Exit Sub
  25.  
  26. End If
  27.  
  28.  
  29. NoDays = Nz((End_Date - Start_Date), 0) + 1
  30. Leav_Close = Nz(([Leav_Open] - [NoDays]), 0)
  31.  
  32. If Leav_Open < NoDays Or Leav_Close < 0 Or Leav_Close < -1 Then
  33. MsgBox " No sufficient Leave Balance...Kindly Check Or Update Leave Balance"
  34. End_Date = ""
  35. NoDays = ""
  36. Leav_Close = ""
  37. Exit Sub
  38.  
  39.  
  40.  
  41. Else
  42. strStartCriteria = "eeID = " & Me.LeID & " And Emp_LeaveType = '" & Me.L_Type & _
  43.                    "' And Leave_From <= #" & Me.Start_Date & "# And Leave_Upto > #" & Me.Start_Date & "#" & _
  44.                    " And LeaveEntryType = '" & Me.LeaveEntryType & "'"
  45. strEndCriteria = "eeID = " & Me.LeID & " And Emp_LeaveType = '" & Me.L_Type & _
  46.                  "' And Leave_From <= #" & Me.End_Date & "# And Leave_Upto > #" & Me.End_Date & "#" & _
  47.                  " And LeaveEntryType = '" & Me.LeaveEntryType & "'"
  48.  
  49.     If DCount("*", "tabLeave", strStartCriteria) > 0 Or DCount("*", "tabLeave", strEndCriteria) > 0 Then
  50.         MsgBox " This Leave period overlaped with existing Leave in database for this employee with this type of Leave. Kindly check previous record of this employee."
  51.         Me.End_Date = ""
  52.         Me.Start_Date = ""
  53.         Me.NoDays = ""
  54.         Me.Leav_Close = ""
  55.         Me.End_Date.SetFocus
  56.     Else
  57.         Me!NoDays.Value = Nz((End_Date - Start_Date), 0) + 1
  58.         Me!Leav_Close.Value = Nz(([Leav_Open] - [NoDays]), 0)
  59.     End If
  60. End If
  61.  
  62. If L_Type = "Commuted Leave" Then
  63.  
  64. HPLBal = Nz((TempVars!HPL.Value), 0)
  65. Me!NoDays.Value = Nz((End_Date - Start_Date), 0) + 1
  66. Me!Leav_Close.Value = Nz((HPLBal - ([NoDays] * 2)), 0)
  67. MsgBox "Kindly read " & Leav_Close & " Half Pay Leave Balance instead of " & Leav_Close & " Commuted Leave after this leave period."
  68.  
  69. End If
  70.  
  71. ExitSub:
  72.     Exit Sub
  73. ErrorHandler:
  74.     Resume ExitSub
  75.  
  76.  
  77. End Sub
Jan 21 '16 #7
Seth Schrock
2,965 Expert 2GB
Please place the following lines of code between lines 47 and 49:
Expand|Select|Wrap|Line Numbers
  1. Debug.Print "SCr: " & strStartCriteria
  2. Debug.Print "ECr: " & strEndCriteria
  3. Debug.Print "SCo: " & DCount("*", "tabLeave", strStartCriteria)
  4. Debug.Print "ECo: " & DCount("*", "tabLeave", strEndCriteria)
This will put some output in your Immediate window of the code editor (press Ctrl + G if not visible). Please post the results back to this thread so that we can see what is going on.

Also, please verify that the EmployeeID, Entry Type and Leave type are all matching the existing leave record.
Jan 21 '16 #8
There is following result if Start Date is 30-Dec-15 and End Date 02-Jan-16... There's already leave with same details with Leave Period 01-Jan-16 to 05-Jan-16...so now code should prevent duplicate value. but it not works with Start Date 30-Dec-15 and End Date 02-Jan-16... It works good with Start Date 01-Jan-16 and End Date 02-Jan-16....or any future date...


Immediate window result is:
Expand|Select|Wrap|Line Numbers
  1. SCr: eeID = 112233 And Emp_LeaveType = 'Earned Leave' And Leave_From <= #30-12-2015# And Leave_Upto > #30-12-2015# And LeaveEntryType = 'PUC'
  2. ECr: eeID = 112233 And Emp_LeaveType = 'Earned Leave' And Leave_From <= #03-01-2016# And Leave_Upto > #03-01-2016# And LeaveEntryType = 'PUC'
  3. SCo: 0
  4. ECo: 0
Jan 21 '16 #9
Seth Schrock
2,965 Expert 2GB
I believe that this should fix the problem. Change strStartCriteria to the following:
Expand|Select|Wrap|Line Numbers
  1. strStartCriteria = "eeID = " & Me.LeID & " And Emp_LeaveType = '" & Me.L_Type & _
  2.                    "' And Leave_From <= #" & Me.Start_Date & "# And Leave_Upto > #" & Me.Start_Date & "#" & _
  3.                    " And LeaveEntryType = '" & Me.LeaveEntryType & "'"
  4.                    " And (#" & Me.Start_Date & "# Between Leave_From And Leave_Upto" & _
  5.                    " Or Leave_From Between #" & Me.Start_Date & "# And #" & Me.End_Date & "#" & _
  6.                    " Or Leave_Upto Between #" & Me.Start_Date & "# And #" & Me.End_Date & "#" & _
  7.                    " Or #" & Me.End_Date & "# Between Leave_From And Leave_Upto)"
Now on line 49 of your post, remove the second DCount() test. I combined them into one so it should work that way.
Jan 21 '16 #10
Not working respected seth-
Now immediate window reply is following:-
Expand|Select|Wrap|Line Numbers
  1. SCr: eeID = 112233 And Emp_LeaveType = 'Earned Leave' And Leave_From <= #30-12-2015# And Leave_Upto > #30-12-2015# And LeaveEntryType = 'PUC' And (#30-12-2015# Between Leave_From And Leave_Upto Or Leave_From Between #30-12-2015# And #02-01-2016# Or Leave_Upto Between #30-12-2015# And #02-01-2016# Or #02-01-2016# Between Leave_From And Leave_Upto)
  2. SCo: 0
  3.  
Jan 22 '16 #11
Here's problem-

Expand|Select|Wrap|Line Numbers
  1. SCr: eeID = 112233 And Emp_LeaveType = 'Earned Leave' And LeaveEntryType = 'PUC' And (#01-01-2016# Between Leave_From And Leave_Upto Or Leave_From Between #01-01-2016# And #02-01-2016# Or Leave_Upto Between #01-01-2016# And #02-01-2016# Or #02-01-2016# Between Leave_From And Leave_Upto)
  2. SCo: 1
  3. SCr: eeID = 112233 And Emp_LeaveType = 'Earned Leave' And LeaveEntryType = 'PUC' And (#02-01-2016# Between Leave_From And Leave_Upto Or Leave_From Between #02-01-2016# And #03-01-2016# Or Leave_Upto Between #02-01-2016# And #03-01-2016# Or #03-01-2016# Between Leave_From And Leave_Upto)
  4. SCo: 0
  5.  
Here code gives result in 1 if start date is 01-01-2016 but result 0 if start date is 02-01-2016. Leave period in database is from 01-01-2016 to 05-01-2016 then second date [02-01-2016] should give result in 1 also . But there's some problem which I am not understanding....
Jan 22 '16 #12
Seth Schrock
2,965 Expert 2GB
Try this.
Expand|Select|Wrap|Line Numbers
  1. strStartCriteria = "eeID = " & Me.LeID & " And Emp_LeaveType = '" & Me.L_Type & _
  2.                    " And LeaveEntryType = '" & Me.LeaveEntryType & "'"
  3.                    " And (#" & Me.Start_Date & "# Between Leave_From And Leave_Upto" & _
  4.                    " Or Leave_From Between #" & Me.Start_Date & "# And #" & Me.End_Date & "#" & _
  5.                    " Or Leave_Upto Between #" & Me.Start_Date & "# And #" & Me.End_Date & "#" & _
  6.                    " Or #" & Me.End_Date & "# Between Leave_From And Leave_Upto)"
I forgot to remove one of the lines of code.
Jan 22 '16 #13
Thanks a lot Seth Schrok.... Code worked fine...

Thanks a lot
Jan 22 '16 #14
Seth Schrock
2,965 Expert 2GB
Good! I was starting to pull my hair out :) Good luck on your project.

You might want to reset the best answer for any others who might be having the same problem.
Jan 22 '16 #15
Hello Seth....


I have another question regarding my project.


I want to create a query to export data in excel, word or pdf format from existing tables using FORM.

Form should have two list box.. First List box on form have all fields name exists in database.. So user can choose required fields from list box 1 and selected fields should appear in listbox 2 on sameform. Now create button should export selected fields in desired format. I think there will be a combo box with Value :Excel; Word; PDF.. when user choose value i.e. excel from combo box after selected fields in listbox 2... button create/export data in excel with selected fields.
Jan 23 '16 #16
Seth Schrock
2,965 Expert 2GB
Per site rules, only one question per thread. Please post your question in a new thread and we will answer it there.
Jan 23 '16 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ced | last post by:
Hi, i'm not an expert in C but i try to compile BTNG software under linux kernel 2.4.2-2. I get these errors at the very first stage. Does someone could have a rapid look on this and tell me...
1
by: rengaraj | last post by:
Is there any free library/dll with which we can write data into already existing PDF(Adobe) files ?
1
by: asger_gronnow | last post by:
Hi I'm having trouble associating file types with my app. Associating a file type works as long as the file type is already associated with another program but if the file type doesn't exist...
3
by: martin | last post by:
Hello, Could someone here please explain the reason for the error in the subject (the full error below) I dont get this everytime and i've never gotten while debugging. The code (also below)...
5
by: Jon Slaughter | last post by:
Is there a way to collectively get the internal data for primitive types? So for Int32, SByte, etc, I want to be able to get the byte stream used for the data. The reason is that I want a byte...
2
alexphd
by: alexphd | last post by:
I've been creating this webapp and for some weird reason I cant get this part to work. What I'm trying to do is check for a match on the username and startdate. If there is a match then that user has...
3
by: Atropo | last post by:
Hi, all. As you can see I'm just begining on C. with the tutorial "Sams Teach Yourself C in 24 Hours" as a starter. on aix 5.2 when compiling a helloWorld the gcc throws some warnings the...
7
by: L. Ximenes | last post by:
Hello everyone, I know how to parse data from an XML file with Javascript; but what if I would like to parse data from a user-submitted XML file via the " <input type='file'"? Would it be...
2
by: Austin H | last post by:
Hello, I am trying to figure out how to redirect users from accessing a form if they have already submitted it within the past hour. I am trying to figure this out because the TOS with my hosting...
3
by: ChrisAlc | last post by:
I have 2 tables which I need to join. One holds account data based on policy number which is updated each month when a policy makes a payment the other has the policy history. POLICYHISTORY ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.