Xscatolare
IS-IT--Management
So I have researched this forum and the innerwebs trying to find some similar solution that I could outfit to work for my needs. Since I am posting here I have not succeeded in finding the solution so I appreciate your time to help me out.
Here is what I am looking to do via VBA and future one button execution.
Sample Data:
ID Dte
11009 1/9/2017
11009 3/21/2017
119957 2/21/2017
226715 1/15/2017
226715 3/5/2017
226715 3/13/2017
226715 3/14/2017
239416 2/3/2017
239416 2/6/2017
239416 2/7/2017
239416 2/8/2017
239416 2/15/2017
239416 2/16/2017
239416 3/8/2017
239416 3/30/2017
239416 4/3/2017
239416 4/4/2017
239416 4/5/2017
239416 4/6/2017
239416 4/13/2017
273887 3/14/2017
273887 4/11/2017
273887 4/12/2017
394892 2/20/2017
What I am trying to do is read through the records and determine at what point there are 2 consecutive dates per ID. For example when reading the list you will see that ID 226715 has 4 entries and only 2 of them are consecutive. The ultimate goal is to determine just the first 2 consecutive dates. So in example of 239416 there are dates 4/3, 4/4, 4/5, 4/6. Although there are 4 dates consecutively I only care that the first match of 4/3 and 4/4 exist. For example if there was a range of 4/3, 4/4, 4/5, 4/7, 4/8 this would produce 2 instances of the consecutive days since there was a break in sequence with nothing being recorded for 4/6.
Here is the code thus far. I realize that in the ELSE statement I have the record moving twice when the IDs don't match. It works sometimes but not on long runs and then it skips the first instance of the mismatch.
It is crud as I am just playing with the scenarios and wanting to see what is being returned thus the message boxes. I am not sure that cloning is the right answer however I do not have the skills for arrays. I have thought possibly of doing case statements to test each entry per ID but still thinking that through. I confess I am not fluent in coding but understand most things and typically can find what I need and then tweak it to my needs. I appreciate anyone's time and insight on this project.
Here is what I am looking to do via VBA and future one button execution.
Sample Data:
ID Dte
11009 1/9/2017
11009 3/21/2017
119957 2/21/2017
226715 1/15/2017
226715 3/5/2017
226715 3/13/2017
226715 3/14/2017
239416 2/3/2017
239416 2/6/2017
239416 2/7/2017
239416 2/8/2017
239416 2/15/2017
239416 2/16/2017
239416 3/8/2017
239416 3/30/2017
239416 4/3/2017
239416 4/4/2017
239416 4/5/2017
239416 4/6/2017
239416 4/13/2017
273887 3/14/2017
273887 4/11/2017
273887 4/12/2017
394892 2/20/2017
What I am trying to do is read through the records and determine at what point there are 2 consecutive dates per ID. For example when reading the list you will see that ID 226715 has 4 entries and only 2 of them are consecutive. The ultimate goal is to determine just the first 2 consecutive dates. So in example of 239416 there are dates 4/3, 4/4, 4/5, 4/6. Although there are 4 dates consecutively I only care that the first match of 4/3 and 4/4 exist. For example if there was a range of 4/3, 4/4, 4/5, 4/7, 4/8 this would produce 2 instances of the consecutive days since there was a break in sequence with nothing being recorded for 4/6.
Here is the code thus far. I realize that in the ELSE statement I have the record moving twice when the IDs don't match. It works sometimes but not on long runs and then it skips the first instance of the mismatch.
Code:
Dim strSQL As String
Dim rsA As DAO.Recordset
Dim rsB As DAO.Recordset
Set db = CurrentDb
strSQL = "SELECT tblUnexcused.SalesID, tblUnexcused.Dte, tblUnexcused.CallInLogFullName" _
& " FROM tblUnexcused" _
& " ORDER BY tblUnexcused.SalesID, tblUnexcused.Dte;"
Set rsA = CurrentDb.OpenRecordset(strSQL)
If Not rsA.BOF Then
rsA.MoveLast
rsA.MoveFirst '@ 1st record
Set rsB = rsA.Clone
rsB.MoveLast
rsB.AbsolutePosition = 1 '@ 2nd record
'----------------------------------
Do Until rsB.EOF
If rsA!salesid = rsB!salesid Then
Duration = DateDiff("D", rsA!dte, rsB!dte)
MsgBox rsA!salesid & ", " & rsA!dte & Chr(13) & rsB!salesid & ", " & rsB!dte & Chr(13) & "Duration in Days = " & Duration
rsA.MoveNext
rsB.MoveNext
Else
Duration = DateDiff("D", rsA!dte, rsB!dte)
MsgBox rsA!salesid & ", " & rsA!dte & Chr(13) & rsB!salesid & ", " & rsB!dte & Chr(13) & "Duration in Days = " & Duration, , "In Else b4 move"
rsA.Move 2
rsB.Move 2
MsgBox rsA!salesid & ", " & rsA!dte & Chr(13) & rsB!salesid & ", " & rsB!dte & Chr(13) & "Duration in Days = " & Duration, , "In Else after move"
End If
Loop
End If
'----------------------------------
Set rsB = Nothing
Set rsA = Nothing
Set db = Nothing
It is crud as I am just playing with the scenarios and wanting to see what is being returned thus the message boxes. I am not sure that cloning is the right answer however I do not have the skills for arrays. I have thought possibly of doing case statements to test each entry per ID but still thinking that through. I confess I am not fluent in coding but understand most things and typically can find what I need and then tweak it to my needs. I appreciate anyone's time and insight on this project.