locked
Compute Lightswitch entity property based on date property in same record using Visual Basic RRS feed

  • Question

  • Hi,

    I would like to add two computed properties to an entity in Lightswitch using VB. 

    1) One property would be computed and enter the month as Jan, Feb, Mar,... or January, February, March,... based on a date property entered in the same record. I assume  12 "If then else" statements would be the easiest but I am open to suggestions.

    2) The second property would compute and fill in the year (YYYY) based on the same date property. 

    I am kind of new to VB.  Any help is appreciated. Thank you.


    Thursday, June 26, 2014 3:05 PM

Answers

  • or

    Private Sub YearString_Compute(ByRef result As String)
         result = Format(Me.ThisDate, "yyyy").ToString
    End Sub

    Case might be important. It is for distinguishing months (MM) from minutes (mm).


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by Simon Jones [MSDL] Saturday, June 28, 2014 4:28 PM
    • Marked as answer by Angie Xu Monday, July 7, 2014 8:16 AM
    Friday, June 27, 2014 1:51 PM

All replies

  • Private Sub MonthString_Compute(ByRef result As String)
        result = Format(Me.ThisDate, "MMMM") 
        'Change to "MMM" for month abbreviation
    end Sub
    
    Private Sub YearString_Compute(ByRef result As String)
        result = Format(Me.ThisDate, "YYYY").ToString
    end Sub


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Thursday, June 26, 2014 5:06 PM
  • Hi Simon,

    Thank you for your response.

    The first one for the Month field works great with both the full name and the Abbreviation.

    The second one for the year however, does not.  When the computed Year property was added to a search screen in Lightswitch the result was the "YYYY" is entered in the column for all records.  I tried

    Private Sub YearString_Compute(ByRef result As String)
        result
    = Format(Me.ThisDate, "YYYY")
    end Sub

    as well as

    Private Sub YearString_Compute(ByRef result As String)
        result
    = Format(Me.ThisDate, "YYYY").ToString
    end Sub

    Friday, June 27, 2014 1:26 PM
  • If that doesn't work, try

    Private Sub YearString_Compute(ByRef result As String)
        result = Year(Me.ThisDate).ToString
    end Sub


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Friday, June 27, 2014 1:47 PM
  • or

    Private Sub YearString_Compute(ByRef result As String)
         result = Format(Me.ThisDate, "yyyy").ToString
    End Sub

    Case might be important. It is for distinguishing months (MM) from minutes (mm).


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by Simon Jones [MSDL] Saturday, June 28, 2014 4:28 PM
    • Marked as answer by Angie Xu Monday, July 7, 2014 8:16 AM
    Friday, June 27, 2014 1:51 PM
  • This one worked.  The parameter "yyyy" is case sensitive.
    Saturday, June 28, 2014 11:42 AM
  • This one did not work.  The Lightswitch build returned an error:

    Error 1 Value of type 'Date?' cannot be converted to 'Integer'. 

    I am not sure why because everything about it looks like it is using "String" and not Integer.

    Saturday, June 28, 2014 11:46 AM