By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,501 Members | 2,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,501 IT Pros & Developers. It's quick & easy.

Last Quarterly Updated Record VBA Code

100+
P: 294
I hate to do this..but can anyone tell me exactly what this is doing, piece by piece?

Expand|Select|Wrap|Line Numbers
  1. CurrentNAVDate = Format(DateAdd("s", -1, DateAdd("q", DateDiff("q", "1/1/1900", Date), "1/1/1900")), "Short Date")
I feel stupid and I can't figure it out. I kind of know what it's doing, but I don't REALLY know. I don't understand why it is adding -1 second to whatever the hell it is adding to...

Thanks...
Nov 11 '13 #1

✓ answered by NeoPa

Expand|Select|Wrap|Line Numbers
  1. CurrentNAVDate = Format(
  2.                  DateAdd("s", -1,
  3.                  DateAdd("q",
  4.                  DateDiff("q", "1/1/1900", Date),
  5.                  "1/1/1900")),
  6.                  "Short Date")
If we break this up into separate (numbered) lines then we can see more easily what's going on.
  1. Line #4.
    This determines the number of quarters from the base date (in this case 1/1/1900) up to the current date.
  2. Lines #3-5
    Using the result from Line #4 this finds the start point of the current quarter.
  3. Lines #2-5
    Using the result from lines #3-5 it subtracts a second to find the end point of the previous quarter.
  4. Lines #1-6
    It displays the date value from lines #2-5 as a Short Date

Share this Question
Share on Google+
11 Replies


zmbd
Expert Mod 5K+
P: 5,287
Starting from the outside working in:
(although, I actually started from the inside and workded out (^-^) 0

Format: Is attempting to return the calculate value using the "short date" format of your system usually m/dd/yy
My System short date is set to "mm/dd/yyyy" because I have databases that require that format; thus on my system:
(=09/30/2013)

First DateAdd: Is attempting to subtract 1 second from the given date, which in this case is calculated
(=09/30/2013 11:59:59 PM)

Second DateAdd: Is attempting to add the number of quarters being calculated in the datediff function to 1/1/1900
(=10/01/2013)

DateDiff: Is attempting to calculate the number of quarters between today's date (2013-11-11) and 1/1/1900
(= 455)

As entered this will error
Date which will give you a #NAME? or a #Type! error in the calculated control/field it should be Date()
Nov 11 '13 #2

NeoPa
Expert Mod 15k+
P: 31,186
Expand|Select|Wrap|Line Numbers
  1. CurrentNAVDate = Format(
  2.                  DateAdd("s", -1,
  3.                  DateAdd("q",
  4.                  DateDiff("q", "1/1/1900", Date),
  5.                  "1/1/1900")),
  6.                  "Short Date")
If we break this up into separate (numbered) lines then we can see more easily what's going on.
  1. Line #4.
    This determines the number of quarters from the base date (in this case 1/1/1900) up to the current date.
  2. Lines #3-5
    Using the result from Line #4 this finds the start point of the current quarter.
  3. Lines #2-5
    Using the result from lines #3-5 it subtracts a second to find the end point of the previous quarter.
  4. Lines #1-6
    It displays the date value from lines #2-5 as a Short Date
Nov 11 '13 #3

NeoPa
Expert Mod 15k+
P: 31,186
Zmbd is correct in stating that the SQL engine (Jet) will not recognise Date as the function call Date(). This needs to be amended.

Not so correct in adding 23 hours, 59 minutes and 59 seconds instead of subtracting a second though ;-)

Essentially, that part is a crude way of subtracting a day as it depends on the start position being midnight and all remaining hours being stripped by formatting it simply as a date. Using a parameter of 'd' would have made more sense.
Nov 11 '13 #4

zmbd
Expert Mod 5K+
P: 5,287
Neopa:(...)Not so correct in adding 23 hours, 59 minutes and 59 seconds instead of subtracting a second though ;-) (...)
zmbd: (...) First DateAdd: Is attempting to subtract 1 second from the given date, which in this case is calculated (...)
NeoPa, Normally I can follow you; however, you lost me here?
Nov 12 '13 #5

NeoPa
Expert Mod 15k+
P: 31,186
I'll try to explain in a way that makes sense and is easier to understand. if you can't understand what I said then there's a good chance that most will be confused.

Dates are stored as numbers. Dates, without any time components, are stored as whole numbers. Dates with time components (EG. #2/28/1909 13:04:52#) are stored as floating point numbers where the integral part (to the left of the decimal point - or comma in many European countries) represents the date and the fractional part (to the right of the decimal point) represents the time. Thus, a date value with no time contingent is equivalent to that same date with a time of midnight.

If you go back one second from midnight on a particular date you end up at 23:59:59 of the previous date. That works the same way numerically when subtracting a second from midnight (or a date-only value).

Let me know if that perspective helps.
Nov 12 '13 #6

zmbd
Expert Mod 5K+
P: 5,287
That is what I had thought I had said? (@_@)
However, it's been a very long day.

The Gremlins have moved into my lab and I just can not seem to get the little mischive makers to get out of my instrumentation! One thing after another after another... job security of course; hoewever, it starts to wear down the mind, soul, and body!
Nov 12 '13 #7

100+
P: 294
Guys, thank you!! GREAT explanations. I sort-of understood it, however, all of the DateFormat and DateAdds were making me lose focus each run-through (as dumb as that sounds).
Nov 13 '13 #8

zmbd
Expert Mod 5K+
P: 5,287
mcupito:
You're very welcome and no, it's not dumb sounding. The more steps the harder to follow. For example, I really hate nesting more that two IF/Then or IF/Then/ElseIF constructs. Instead, if possible, I'll use Select/Case just because it's easier to follow.
Nov 13 '13 #9

100+
P: 294
Thanks for the kind words, zmbd. I definitely understand where you're coming from with your griefs in nesting constructs.
Nov 13 '13 #10

NeoPa
Expert Mod 15k+
P: 31,186
I agree with Z on that. Getting too complicated with code is not a great idea. I can be a little guilty of that sometimes, but generally I avoid it.

Mark, you're certainly not the only one to find it hard to decipher complex commands that are built up by linking multiple calls into one 'line'.
Nov 14 '13 #11

100+
P: 294
NeoPa, thanks for the encouragement. MS Access can be quite a monster, sometimes.
Nov 14 '13 #12

Post your reply

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