Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Date Validation

CarterLangleyCarterLangley Posts: 29Member
Hi guys,

Got a richtextbox control. Users are entering dates into this thing. Now, I need to make sure that the date that gets entered into my database is in the following format - dd/mm/yyyy - regardless of what the users enter into the control. as long as they enter a date, the code should convert it into what I want. I have tried the following but it always returns the current date, which is not what is required.

.text = format(Date,"dd/mm/yyyy")

Is it possible to have a mask in the control, sort of grayed out in the background, that will guide the user on how to enter the date? Or is it best to just validate the hell out of what the users enter and then convert it to what I need? Former probably simpler, latter most definately harder.

Using VB6 Enterprise, sp6 and access 2003.

Comments

  • lionblionb Posts: 1,685Member
    :
    [color=Red]First.[/color] Date formating is real pain of the ass in VB 6. There is no particular recepy how to do that. Most developers created their own code to do that. As advise, use IsDate() function to validate value
    Something like that
    [code]If IsDate(Text1.Text) then
    Text1.Text = Format(Text1.text, "dd/mm/yyyy")
    Else
    MsgBox ("Wrong Date"), vbCritical
    Text1.SetFocus
    End If[/code]You can try to play with Microsoft Mask control but IMHO opinion it's not that useful.
    [color=Red]Second.[/color]
    Your problem is here:
    .text = format([color=Blue][b]Date[/b][/color],"dd/mm/yyyy")

    You are using [b][color=Blue]Date[/color][/b] function which is always currenr system date. Replace it by value of date you want to format
  • CarterLangleyCarterLangley Posts: 29Member
    : :
    : [color=Red]First.[/color] Date formating is real pain of the ass in
    : VB 6. There is no particular recepy how to do that. Most developers
    : created their own code to do that. As advise, use IsDate() function
    : to validate value
    : Something like that
    : [code]: If IsDate(Text1.Text) then
    : Text1.Text = Format(Text1.text, "dd/mm/yyyy")
    : Else
    : MsgBox ("Wrong Date"), vbCritical
    : Text1.SetFocus
    : End If[/code]: You can try to play with Microsoft Mask control but IMHO opinion
    : it's not that useful.
    : [color=Red]Second.[/color]
    : Your problem is here:
    : .text = format([color=Blue][b]Date[/b][/color],"dd/mm/yyyy")
    :
    : You are using [b][color=Blue]Date[/color][/b] function which is
    : always currenr system date. Replace it by value of date you want to
    : format

    OK, thought that might be the case. Sometimes VB6 can be such a nice language and other times I end up pulling my hair out by the roots. But suppose all said and done it is still a lot easier than the C languages.

    I will try your first example first, and second one second and see what I get.
    Do you have any standard code in a module hiding away somewhere on some hard drive you have long since forgotten you had that might be of help to me?
    Things used to be clear and simple - until I started coding!!
  • dokken2dokken2 Posts: 532Member
    : Hi guys,
    :
    : Got a richtextbox control. Users are entering dates into this thing.
    : Now, I need to make sure that the date that gets entered into my
    : database is in the following format - dd/mm/yyyy - regardless of
    : what the users enter into the control. as long as they enter a date,
    : the code should convert it into what I want. I have tried the
    : following but it always returns the current date, which is not what
    : is required.
    :
    : .text = format(Date,"dd/mm/yyyy")
    :
    : Is it possible to have a mask in the control, sort of grayed out in
    : the background, that will guide the user on how to enter the date?
    : Or is it best to just validate the hell out of what the users enter
    : and then convert it to what I need? Former probably simpler, latter
    : most definately harder.
    :
    : Using VB6 Enterprise, sp6 and access 2003.
    :

    first off, you're assigning the current date instead of the text input into the control. second, good practice to use the IsDate function to test if the user input a date or not.

    Private Sub RichTextBox1_LostFocus()
    If IsDate(Me.RichTextBox1.Text) Then
    Me.RichTextBox1.Text = Format(Me.RichTextBox1.Text, "dd/mm/yyyy")
    Else
    MsgBox "invalid date"
    'Me.RichTextBox1.SetFocus 'keep focus on control until correct?
    End If
    End Sub
  • lionblionb Posts: 1,685Member
    : : :
    : Do you have any standard code in a module hiding away somewhere on
    : some hard drive you have long since forgotten you had that might be
    : of help to me?
    : Things used to be clear and simple - until I started coding!!

    Like I said before, there is no any standard code to do date validation/formating. Besides, I do not work with VB 6 last 4 years. There is some code example I remember I used while ago. This is not the best example of VB code and [b][u]of course [/u][/b][u][/u][u]this is not [color=Blue]standard[/color][b][/b][/u]
    [code]
    Option Explicit
    Dim intCode As Integer


    Private Sub Text1_Change()
    Text1.MaxLength = 10
    If intCode <> 8 Then [color=Green]' backspace is not pressed [/color]

    Select Case Len(Text1.Text)
    Case 2, 5
    Text1.Text = Text1.Text & "/"
    Text1.SelStart = Len(Text1.Text) + 1
    End Select
    End If
    End Sub


    Private Sub cmdNext_Click()

    If IsDate(Text1.Text) Then
    Text1.Text = Format(Text1.Text, "dd/mm/yyyy")
    Else
    MsgBox "Oooops! Wrong date", vbCritical
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
    Text1.SetFocus
    End If

    End Sub

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    intCode = KeyAscii [color=Green]' backspace pressed [/color]
    End Sub[/code]
  • CarterLangleyCarterLangley Posts: 29Member
    : : Hi guys,
    : :
    : : Got a richtextbox control. Users are entering dates into this thing.
    : : Now, I need to make sure that the date that gets entered into my
    : : database is in the following format - dd/mm/yyyy - regardless of
    : : what the users enter into the control. as long as they enter a date,
    : : the code should convert it into what I want. I have tried the
    : : following but it always returns the current date, which is not what
    : : is required.
    : :
    : : .text = format(Date,"dd/mm/yyyy")
    : :
    : : Is it possible to have a mask in the control, sort of grayed out in
    : : the background, that will guide the user on how to enter the date?
    : : Or is it best to just validate the hell out of what the users enter
    : : and then convert it to what I need? Former probably simpler, latter
    : : most definately harder.
    : :
    : : Using VB6 Enterprise, sp6 and access 2003.
    : :
    :
    : first off, you're assigning the current date instead of the text
    : input into the control. second, good practice to use the IsDate
    : function to test if the user input a date or not.
    :
    : Private Sub RichTextBox1_LostFocus()
    : If IsDate(Me.RichTextBox1.Text) Then
    : Me.RichTextBox1.Text = Format(Me.RichTextBox1.Text, "dd/mm/yyyy")
    : Else
    : MsgBox "invalid date"
    : 'Me.RichTextBox1.SetFocus 'keep focus on control until correct?
    : End If
    : End Sub
    :

    I have implemented this and it seems to work. However, as well as this does seem to work, will this handle all the different ways of entering a date? If so, then I am going to keep on using this.
    Things used to be clear and simple - until I started coding!!
  • CarterLangleyCarterLangley Posts: 29Member
    : : : :
    : : Do you have any standard code in a module hiding away somewhere on
    : : some hard drive you have long since forgotten you had that might be
    : : of help to me?
    : : Things used to be clear and simple - until I started coding!!
    :
    : Like I said before, there is no any standard code to do date
    : validation/formating. Besides, I do not work with VB 6 last 4 years.
    : There is some code example I remember I used while ago. This is not
    : the best example of VB code and [b][u]of course
    : [/u][/b][u][/u][u]this is not [color=Blue]standard[/color][b][/b][/u]
    : [code]:
    : Option Explicit
    : Dim intCode As Integer
    :
    :
    : Private Sub Text1_Change()
    : Text1.MaxLength = 10
    : If intCode <> 8 Then [color=Green]' backspace is not pressed [/color]
    :
    : Select Case Len(Text1.Text)
    : Case 2, 5
    : Text1.Text = Text1.Text & "/"
    : Text1.SelStart = Len(Text1.Text) + 1
    : End Select
    : End If
    : End Sub
    :
    :
    : Private Sub cmdNext_Click()
    :
    : If IsDate(Text1.Text) Then
    : Text1.Text = Format(Text1.Text, "dd/mm/yyyy")
    : Else
    : MsgBox "Oooops! Wrong date", vbCritical
    : Text1.SelStart = 0
    : Text1.SelLength = Len(Text1.Text)
    : Text1.SetFocus
    : End If
    :
    : End Sub
    :
    : Private Sub Text1_KeyPress(KeyAscii As Integer)
    : intCode = KeyAscii [color=Green]' backspace pressed [/color]
    : End Sub[/code]:

    Thanks for this example, I will try this as well as what I have got there now. I am sure that between this and what I have I can now do something!! Thanks for the help and the example.

    Things used to be clear and simple - until I started coding!!
  • basheerpm777basheerpm777 Posts: 1Member
    Fine, the program works well, but try entering the first DD as 13 or more, the answers start varying, similarly if MM is more than 12 the format again misfires

    Please help

    Regards
    Basheer




    : : : Hi guys,
    : : :
    : : : Got a richtextbox control. Users are entering dates into this thing.
    : : : Now, I need to make sure that the date that gets entered into my
    : : : database is in the following format - dd/mm/yyyy - regardless of
    : : : what the users enter into the control. as long as they enter a date,
    : : : the code should convert it into what I want. I have tried the
    : : : following but it always returns the current date, which is not what
    : : : is required.
    : : :
    : : : .text = format(Date,"dd/mm/yyyy")
    : : :
    : : : Is it possible to have a mask in the control, sort of grayed out in
    : : : the background, that will guide the user on how to enter the date?
    : : : Or is it best to just validate the hell out of what the users enter
    : : : and then convert it to what I need? Former probably simpler, latter
    : : : most definately harder.
    : : :
    : : : Using VB6 Enterprise, sp6 and access 2003.
    : : :
    : :
    : : first off, you're assigning the current date instead of the text
    : : input into the control. second, good practice to use the IsDate
    : : function to test if the user input a date or not.
    : :
    : : Private Sub RichTextBox1_LostFocus()
    : : If IsDate(Me.RichTextBox1.Text) Then
    : : Me.RichTextBox1.Text = Format(Me.RichTextBox1.Text, "dd/mm/yyyy")
    : : Else
    : : MsgBox "invalid date"
    : : 'Me.RichTextBox1.SetFocus 'keep focus on control until correct?
    : : End If
    : : End Sub
    : :
    :
    : I have implemented this and it seems to work. However, as well as
    : this does seem to work, will this handle all the different ways of
    : entering a date? If so, then I am going to keep on using this.
    : Things used to be clear and simple - until I started coding!!

Sign In or Register to comment.