Results 1 to 8 of 8

Thread: [2005] Date Format - TROUBLE

  1. #1

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Angry [2005] Date Format - TROUBLE

    In my for i have a bounded TextBox (txt_pana_la_data.Text).

    I want to compare the txt_pana_la_data.Text with the curent date in the format "dd/MM/yyyy".

    I have tryed with this code but is not working if i have for example the txt_pana_la_data.Text = 25/10/2007... what's wrong ?

    Code:
        Private Sub txt_pana_la_data_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_pana_la_data.TextChanged
    
            'Dim data_curenta = Date.Now
    
            Dim data_expirarii As Date
            Dim data_curenta As Date
            data_curenta = Today
            Format(data_curenta, "dd/MM/yyyy")
    
             data_expirarii = Me.txt_pana_la_data.Text
            Format(data_expirarii, "dd/MM/yyyy")
    
            If data_expirarii < data_curenta Then
    
                Me.txt_stare_permis.Text = "Expirat"
                Me.btn_prelungire.Enabled = True
    
                Me.Validate()
                Me.permis_BindingSource.EndEdit()
                Me.permis_TableAdapter.Update(Me.permis_DataSet.lista_abonati)
    
                Me.permis_BindingSource.Clear()
                Me.permis_TableAdapter.Fill(Me.permis_DataSet.lista_abonati)
    
               
            Else
    
                'Me.txt_stare_permis.Text = "Activ"
    
            End If
    
        End Sub
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Date Format - TROUBLE

    Is there a reason that you chose to use a TextBox instead of the DateTimePicker, which is designed for dates?

    If you do want to stick with a TextBox then you can't use the '<' operator on strings because it will compare them alphabetically. If you want to compare dates then you have to compare Date objects:
    vb.net Code:
    1. Dim input As Date
    2.  
    3. If Date.TryParseExact(Me.TextBox1.Text, _
    4.                       "dd/MM/yyyy", _
    5.                       Nothing, _
    6.                       Globalization.DateTimeStyles.None, _
    7.                       input) Then
    8.     If input < Date.Today Then
    9.         'The specified date is before today's date.
    10.     End If
    11. Else
    12.     'Invalid input.
    13. End If
    Note also that if you specify "dd/MM/yyyy" as the format the the user will have to include leading zeroes on single-digit days. It might be more appropriate to use "d/MM/yyyy", which will accept single- or double-digit days.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2005] Date Format - TROUBLE

    The TextBox is ReadOnly... user does not type a date.

    My computer date is 25.01.2008 and i have into the txt_pana_la_data.text the value 10/12/2007 and it's not working! Why ?!?!

    This code i've used!

    Code:
            Dim input As Date
    
            If Date.TryParseExact(Me.txt_pana_la_data.Text, "dd/MM/yyyy", Nothing, Globalization.DateTimeStyles.None, input) Then
    
                If input < Date.Today Then
    
                    Me.txt_stare_permis.Text = "Expirat"
                    Me.btn_prelungire.Enabled = True
    
                    Me.Validate()
                    Me.permis_BindingSource.EndEdit()
                    Me.permis_TableAdapter.Update(Me.permis_DataSet.lista_abonati)
    
                    Me.permis_BindingSource.Clear()
                    Me.permis_TableAdapter.Fill(Me.permis_DataSet.lista_abonati)
    
                End If
    
            Else
    
                'Invalid input.
    
            End If
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Date Format - TROUBLE

    If your TextBox is ReadOnly then why do you need to test the value in the TextBox at all? That value must have come from somewhere, and that somewhere is presumably a Date value. You should be testing THAT value against the current Date.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2005] Date Format - TROUBLE

    The value comes from the database there where is stored as Text. I have more textboxes and a DGV that are bounded to a BindingSource. I have to display it (thru BindingSource - It's OK with this), read it and if it's smaller than the current date to display into another TextBox the value = "Expirat" (expired). (that's the point that i don't pass forward).

    I have used your code but seems that is not working! Why... what i am doing wrong ????

    I read a text value from database, i transform it to a date.today... (dd/MM/yyyy) and i compare it with the current date transformed to dd/MM/yyyy too.
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Date Format - TROUBLE

    Why are you reading a text value from the database? If it's a date then why is it not stored as a date? Do you store numbers as text or as numbers? If you store numbers as numbers then why don't you store dates as dates?

    Also, my code does what it's supposed to do. How would I know what you're doing wrong if you haven't shown me what you are doing?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2005] Date Format - TROUBLE



    Code:
        Private Sub frm_gestionare_permise_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Permis_DataSet.lista_abonati' table. You can move, or remove it, as needed.
            Me.permis_TableAdapter.Fill(Me.permis_DataSet.lista_abonati)
    
        End Sub
    
        Private Sub txt_pana_la_data_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_pana_la_data.TextChanged
    
            Dim input As Date
    
            If Date.TryParseExact(Me.txt_pana_la_data.Text, "dd/MM/yyyy", Nothing, Globalization.DateTimeStyles.None, input) Then
    
                If input < Date.Today Then
    
                    Me.txt_stare_permis.Text = "Expirat"
                    Me.btn_prelungire.Enabled = True
    
                    Me.Validate()
                    Me.permis_BindingSource.EndEdit()
                    Me.permis_TableAdapter.Update(Me.permis_DataSet.lista_abonati)
    
                    Me.permis_BindingSource.Clear()
                    Me.permis_TableAdapter.Fill(Me.permis_DataSet.lista_abonati)
    
                End If
    
            Else
    
                'Invalid input.
    
            End If
    
        End Sub
    Last edited by Alexandru_mbm; Jan 25th, 2008 at 06:17 AM.
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Date Format - TROUBLE

    Your txt_pana_la_data TextBox is bound to a column from your DataTable. That columns should contain Date values and you should be comparing that data to the current date. You should not be parsing the string in the TextBox and your DataColumn should not contain strings.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width