473,407 Members | 2,306 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,407 software developers and data experts.

Last Quarterly Updated Record VBA Code

294 256MB
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

11 1440
zmbd
5,501 Expert Mod 4TB
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
32,556 Expert Mod 16PB
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
32,556 Expert Mod 16PB
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
5,501 Expert Mod 4TB
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
32,556 Expert Mod 16PB
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
5,501 Expert Mod 4TB
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
mcupito
294 256MB
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
5,501 Expert Mod 4TB
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
mcupito
294 256MB
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
32,556 Expert Mod 16PB
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
mcupito
294 256MB
NeoPa, thanks for the encouragement. MS Access can be quite a monster, sometimes.
Nov 14 '13 #12

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

Similar topics

2
by: BT3 | last post by:
I am trying to replace a single record in a flat file. The file is relatively small and no need for database. I open the file, and save the ftell() value in a variable. I read a record using...
1
by: Matik | last post by:
Hello everybody, ------------------------------------------------- CREATE TABLE ( (18, 0) IDENTITY (1, 1) NOT NULL , NOT NULL, (10) NOT NULL , (10) NULL , CONSTRAINT PRIMARY KEY ...
0
by: Do | last post by:
AFter updating a record in a datagrid, I know there is some code to write to get the page scrolled back to the same record position in the page. I saw this at a DevDays .NET conference. Anyone?...
1
by: Karthiga1984 | last post by:
Hi Table Structure company name Phone No Products Example Company Name Phone No Products
1
by: capdownlondon | last post by:
Im using the following code to duplicate a record varCnt(retrieved from a combo box on the form) many times, and it only duplicates the record with the fields present on the form for that record....
4
by: GarthRodgers | last post by:
Hey I know that the command for the last record in a form is "acLast" but was wondering if there was a command for the penultimate record? Thanks
10
by: Gilberto | last post by:
Hello Is there a way to indicate a form that it should open in the LAST+1 record, so that the user can start entering information directly. Because right now when the user opens the form, it...
5
Microblitz
by: Microblitz | last post by:
Hi All, I started messing about with VBA yesterday (I'm a newbie at this language) and have run across a problem which i hope you can help me with. I'm writing a little stock control program in...
6
by: TAAJ Singh | last post by:
Hi; I have a situation where a query is returning multiple rows for each group. For example :- ID Groups Value Status Modified_Time 1 JAA 190 1 2011-10-14 12.01.23 2 JAA 4354 1 2011-5-23 ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.