Solved

# Difference between two dates in Visual Basic

Posted on 2012-09-17
Medium Priority
483 Views
Trying to calculate the difference between two dates. I have tried to use DateDiff like follows: DateDiff("d", Now, StDate) where StDate = 9/22/2012. When I execute this in the immediate window[?SetWeek2(9/22/2012)] I get -41169. I also output the value of StDate. It is 12:00:18 AM. Apparently StDate is not being evaluated as 9/22/2012. I am sure this is something elementary. Thanks in advance for the help.
0
Question by:StampIT

LVL 19

Accepted Solution

Ken Butters earned 1000 total points
ID: 38406257
I just ran a quick test in Access...:

``````Sub test()
Dim x As Variant

x = DateDiff("d", Now, #9/22/2012#)

End Sub
``````

It assigned a value of 5 to x.

you have setweek(9/22/2012).... maybe you need to surround by # to indicate a date?
0

LVL 17

Expert Comment

ID: 38406269
In my example I force stNow to be the same format as the date stDate that I am trying to compare

Dim stDate As Date
Dim stNow As Date

stDate = "9 / 22 / 2012"
stNow = Format(Now(), "mm/dd/yy")

MsgBox (DateDiff("d", stNow, stDate))
0

LVL 29

Expert Comment

ID: 38406339
In calling your function SetWeek2(9/22/2012), you are not sending a date value but rather the quotient of 9 divided by 22 divided by 2012.  In order for Access to see this as a date it needs to be enclosed with # symbols just as buttersk mentioned.
SetWeek2(#9/22/2012#)
0

LVL 52

Expert Comment

ID: 38406465
Don't mix up the format with the value.

Dim datDate As Date
Dim datNow As Date
datDate = #9/22/2012#
datNow = Date

/gustav
0

Author Closing Comment

ID: 38406815
It was as simple as that. Thank you.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
###### Suggested Courses
Course of the Month17 days, 4 hours left to enroll