Full Version: Change System Date via VBA
UtterAccess Discussion Forums > Microsoft® Access > Access Modules
mac_baum
Is there any way to change the system date from VBA code? I need to go back one month.However, a general solution would be appreciated ie change day/month/year.

Thanks for your help.
truittb
How about

Date() = #7/31/2004#

or you could use a variable

Date() = dtmYourDate
r_cubed
Do you know, Truitt ..... when I saw YOUR example, I thought ... "NO, this would/should throw an error. Changing system date should NOT be that simple (read dangerous)."

Just tried it , and it worked , for BOTH Date (and Time , as I found out via the : Time() = #11:10:00 AM#).

Is this dangerous .... ????? . Not sure, but it does it !!!

Not sure why you would want to, but you learn something new every day.
truittb
It suprised me too. It was Saturday afternoon and I was bored and just tried it.
I can't think of any legitimate reason to use it, unless you want to set the date/time to match the date/time on another server or website.
truittb
Just looked at VBA's help and it is a documented method
QUOTE
Date Statement Example
This example uses the Date statement to set the computer system date. In the development environment, the date literal is displayed in short date format using the locale settings of your code.

Dim MyDate
MyDate = #February 12, 1985# ' Assign a date.
Date = MyDate ' Change system date.
mishej
Probably not as dangerous as the Shell() command. Setting the date requires specific permissions in MS's business OS's that should prevent unauthorized users from changing the time.
kenick
I looked at Truitt's example and tried the same thing with time: Time = #6:56:00 AM# and it worked also. I noticed that the example didn't have the () after the date or time. It's another reason to warn people about what names they use in their tables. If you have fields in your tables called Date or Time and try to assign values to them, it seems possible that you could be changing your system settings instead.
truittb
Good point. It is always nice to have a concrete example of why reserved words should be avoided.
Gustav
Set up a separate machine not connected to corporate LAN with duplicates of your date.
Anything else will force you into trouble.

/gustav
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.