Solved

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

Posted on 2013-06-27
8
769 Views
Last Modified: 2013-06-28
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
Comment
Question by:Rlm214
  • 4
  • 4
8 Comments
 
LVL 40
ID: 39283581
Assuming dates in d1 and d2:

DateDiff(DateInterval.Month, d2, d1)
0
 

Author Comment

by:Rlm214
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

by:
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

by:Rlm214
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 =
code
Error =
error
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 40
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

by:Rlm214
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

by:Rlm214
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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.net Excel Add-in TaskPane not opening 3 46
How to list some OU in AD with visual basic .net 1 24
Expando 4 33
Showdialog 8 20
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now