Solved

Lightswitch difference between two dates in months (VB) does not have to be exact

Posted on 2013-06-27
794 Views
I have two date entries that are filled out with random dates (datfields). I want to compute the time between two dates in months. Dividing the days by 30 is fine does not have to be exact. The computed field is a String. Not sure if this matters or if it would be better as an integer.

I know the following is not correct, but it should give some type of idea of what I am trying to do.

If Not StartDate Is Nothing And Not EndDate Is Nothing Then
result = (StartDate - EndDate) /30
0
Question by:Rlm214
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 4

LVL 40

Expert Comment

ID: 39283581
Assuming dates in d1 and d2:

DateDiff(DateInterval.Month, d2, d1)
0

Author Comment

ID: 39284644
I've tried the above. However it seems that actual dates need listed in that code to work. I have two dates for each entry tracking in my program. I need it to look at those two dates and come up with the difference in months.

I receive the following error with the code below.

Datediff(DateInterval.Month, StartDate, EndDate)

I get the error "Overload resolution failed because no acessible 'DateDiff' can be called wthout a narrowing conversion:........
0

LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 total points
ID: 39285222
The method works on Date variables. Your dates are probably in another format such as a String.

Try the following:

Datediff(DateInterval.Month, CDate(StartDate), CDate(EndDate))
0

Author Comment

ID: 39285281
To be more specific about the situation

Name: LeaseCommencementDate
Type: DATE

Name LeaseExpirationDate
Type: DATE

TotalTerm
Type: STRING
Properties: IS COMPUTED

EDIT METHOD =

Error =
0

LVL 40

Expert Comment

ID: 39285357
If I understand, CDate corrected the call to DiffDate, but you get a new error this time.

This one happens somewhere else and might be more difficult to grasp because it does not come from your code.

Why do you store the result in a String, while it is a number? Always try to use the correct type for values. Dates should be in Date variables, numbers in the most appropriate type for the values you expect, most of the time an Integer for numbers that do not have decimals. Otherwise you often end up with conversion problems here and there.

But that might not be your new problem. Where does that MULTI_USER comes from?
0

Author Comment

ID: 39285381
Things seem to be on the right track. Even with the error during my upload, your code is working in my application. The months are being calculated as expected.

In my SQL database lists my date entries as DateTime. My lightswitch application lists them as Dates. I'm not sure if this has anything to do with it or not. Possibly that my computed field is a string instead of integer?

The important thing is that the code is working. I can work on the discrepancy between lightswitch and the database.
0

Author Comment

ID: 39285466
I resolved the error. Thanks again for your help!!!

The error was coming from publishing my lightswitch program with the "TotalTerm" as a non calculated field, then trying to publish again after making it a calculated field.

To correct the error, I deleted "TotalTerm" from my database and everything is now working as planned.
0

LVL 40

Expert Comment

ID: 39285793
You do not have to "fear" differences in types between SQL Server and VB.

The Date type in VB is in fact a datetime, it always contain time information along with the date. Anyway, it does not hold the information in the same format as SQL Server, but the conversion is automatically done for you by the methods that LightSwitch use to communicate with SQL Server.

The same holds true for many types of variables. For instance, VB as only String to work with text, while SQL Server has many different types for Text. They will always be mapped correctly for you in the background.
0

Featured Post

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses
Course of the Month5 days, 23 hours left to enroll