I have a comma separated text file which stores dates from SAP in the following format “YYYYMMDD”. I read in the file, convert the date to a proper format ready for writing to a table. The problem is that the following code works perfectly for half of the data but for some reason, some of the dates it reads in, it puts the month and days the wrong way around so when I use my DateDiff function, only half of the days differences are correct! I can’t see what I’m doing wrong. I’ve used this method in the past with total success!
Below is an example of some of the dates reading in fine and some getting jumbled up.
Converted Table from VB Original text file
ID Start Date End Date Difference Correct Original Start Date
010640017403| 04/11/2005 |14/04/2005 |-204 N 20050411
010640017405| 25/04/2005 |26/04/2005 |1 Y 20050425
010640017409| 19/04/2005 |20/04/2005 |1 Y 20050419
010640017412| 27/04/2005 |27/04/2005 |0 Y 20050427
010640017421| 03/10/2005 |10/05/2005 |-146 N 20050310
010640017430| 04/06/2005 |06/04/2005 |-59 N 20050406
010640017434| 04/05/2005 |11/05/2005 |7 Y 20050405
My code to convert the original into a proper date:
(F1 is the original start date with ‘marks around it, i.e. ‘20050512’)
NewDate = "#" & CDate(Mid(f1, 8, 2) & "/" & Mid(f1, 6, 2) & "/" & Mid(f1, 2, 4)) & "#"
My DateDiff function:
varDaysDiff = DateDiff("D", rs.Fields(2), rs.Fields(4))
(where rs.Fields(2) is the start date I’m using from the text file and rs.Fields(4) is the end date I’m getting from elsewhere.)
Any ideas?
Thanks
Andrew
Below is an example of some of the dates reading in fine and some getting jumbled up.
Converted Table from VB Original text file
ID Start Date End Date Difference Correct Original Start Date
010640017403| 04/11/2005 |14/04/2005 |-204 N 20050411
010640017405| 25/04/2005 |26/04/2005 |1 Y 20050425
010640017409| 19/04/2005 |20/04/2005 |1 Y 20050419
010640017412| 27/04/2005 |27/04/2005 |0 Y 20050427
010640017421| 03/10/2005 |10/05/2005 |-146 N 20050310
010640017430| 04/06/2005 |06/04/2005 |-59 N 20050406
010640017434| 04/05/2005 |11/05/2005 |7 Y 20050405
My code to convert the original into a proper date:
(F1 is the original start date with ‘marks around it, i.e. ‘20050512’)
NewDate = "#" & CDate(Mid(f1, 8, 2) & "/" & Mid(f1, 6, 2) & "/" & Mid(f1, 2, 4)) & "#"
My DateDiff function:
varDaysDiff = DateDiff("D", rs.Fields(2), rs.Fields(4))
(where rs.Fields(2) is the start date I’m using from the text file and rs.Fields(4) is the end date I’m getting from elsewhere.)
Any ideas?
Thanks
Andrew