gromit1
gromit1

Reputation: 587

Label Text Changes During Code but Won't Say Changed

I have code in my program that updates a database. When the database has been updated, I change the text of a label to say "Last Update" and then the actual time when the update occurred. This part works perfectly.

My problem occurs when I close the program and re-open it. I want to code to check the date in the label and if the date in the label is less than the current date I want to update my database. But when I close my program and re-open it the label in the text doesn't stay.

Here is my code:

Public Sub Screen_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Dim Time_of_Update = CDate(Label_Time_of_Update.Text.Split(" "c)(2).Trim())
    Debug.WriteLine(Time_of_Update)
    If Time_of_Update < Today Then
        Update_Data()
    Else
    End If
End Sub

Private Sub Update_Data()
    Update_Daily()
    Ready_Update_Quarterly_and_Annualy()

    Dim Time_of_Update = DateTime.Now
    Label_Time_of_Update.Text = "Last Updated " & Time_of_Update & ""
End Sub

How can I fix this?

Upvotes: 1

Views: 595

Answers (1)

Go to Settings in Project properties and create a LastUpdatedDate setting:

  • Name == "LastUpdatedDate" basically a variable name
  • Type == Date
  • Scope == User (Application scope makes them Read Only)
  • Value == a valid date

This defines the setting name and data type. in code:

Dim Time_of_Update = DateTime.Now
Label_Time_of_Update.Text = "Last Updated " & Time_of_Update & ""

My.Settings.LastUpdatedDate = Time_of_Update
My.Settings.Save

next time you run:

Label_Time_of_Update.Text = My.Settings.LastUpdatedDate

Saving it as a DateTime type will make it easy to date comparison in code rather than converting it back from a String.

Upvotes: 1

Related Questions