Solved

Automatic Date format functionality

Posted on 2008-10-25
9
572 Views
Last Modified: 2012-05-05
I have a text box with a max length of 10 chars so mm/dd/yyyy, however I've been spending the past 30 minutes searching the forums and other sites and the only answer I found was for vb6 and even trying to format it for 2005 it still had a few problems I couldn't iron out.

Right now I have this basic line of code:

 Format(Convert.ToDateTime(DOB.Text.ToString), "DD/MM/YYYY")

But this is throwing up an exception error "String was not recognized as a valid DateTime." When I try typing any digit in the text box.
What I'm really looking for is for VB to automatically add / in the right places after the person adds the date, for example 06051986 or 561985.

Any help would be great, i'm sure this is a quick fix but I've been coding all day and I can't get my head around this.
0
Comment
Question by:Arka3L
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 250 total points
ID: 22806100
You can not pass a string to "Convert.ToDateTime" that is not a valid date.
There are two tools that are available to you:

The first is that VB has a control MaskedTextBox where you can set a mask that looks like a date.

The second is a function "IsDate" that will let you know if a string can be interpreted as a date.

What you need to do is once the user has keyed in the full date, use IsDate to test if the string is a valid date, and if it is not, popup a message box telling them the field does not have a valid date in it and set focus back to the date field.  Only AFTER the string has passed the IsDate test should you use Convert.ToDateTime to convert the input string to a date value.
0
 
LVL 1

Author Comment

by:Arka3L
ID: 22806101
I'm also trying to error check for Letters using the following code:

If IsNumeric(DOB.Text) = False Then
            MsgBox("Please use correct format")

        End If

However it doesn't accept / and if a letter has been entered it stays in the box and i have to manually erase it, would there be a way for vb to stop it going in altogether with the error msg still being displayed?
0
 
LVL 16

Expert Comment

by:ororiole
ID: 22806132
IsDate will accept /

You can erase the box yourself like this:
If DateTime.IsDate(DOB.Text) = False Then
            DPB.Text = ""
            MsgBox("Please use correct format")

        End If
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 16

Accepted Solution

by:
ororiole earned 250 total points
ID: 22806148
sorry that should be IsDate not DateTime.IsDate, and DOB.Text not DPB.Text

You can also do it all at once using this
dim dt As DateTime

  If  DateTime.TryParse(textBox1.Text, dt) Then
   'if true then textBox1.Text held a valid date string and dt will hold a valid date. If false then textBox1.Text held an invalid date string.
  EndIf
0
 
LVL 1

Author Comment

by:Arka3L
ID: 22806175
Ororiole your methods are returning false or dont work and the first is making my msgbox show each time. I tried changing the second around to If DateTime.TryParse(DOB.Text, dt) = False Then but still no luck.

When I'm pulling dates from the database they're already in mm/dd/yyyy format, but when someones entering a new date in that same field it needs to only allow the numbers and /. It's giving me the error even when entering a number.
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 250 total points
ID: 22806192
One way to allow only numeric (or '/') characters into the edit box is to filter out everything else out on the Key Press Event
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Select Case e.KeyChar
            Case "0" To "9", "/"
                'NOP - Do Nothing
            Case Else
                e.KeyChar = vbNullChar
        End Select
    End Sub

Open in new window

0
 
LVL 16

Expert Comment

by:ororiole
ID: 22806317
this does work. I just tried it.

If you are doing this:
If DateTime.TryParse(DOB.Text, dt) = False
then DOB.Text is in fact an invalid date string. The user needs to enter it like 12/15/1988 and it will return true, otherwise false. You can also use the masked text box like Hoo mentioned and select a date format and that already has the / / in there. Then the user does not need to enter them.
0
 
LVL 1

Author Comment

by:Arka3L
ID: 22810336
I just found out that theres a maskedtextbox option which no one mentioned lol, I will be using that from now on instead of hassling with this as its working for me. It has all the built in features with limiting the user to so many characters, to digits only etc instead of hard coding it. So for anyone else who reads this its another option to take a gander at.

Thanks for your help anyway.
0
 
LVL 1

Author Comment

by:Arka3L
ID: 22810341
I apologise about my previous post, HooKooDooKu did indeed mention it, but i thought it was a coded attribute that I needed to apply with my code, such as DOB.MaskedTextBox which took me in circles for hours until I realised it was an actual textbox option from my toolbox window. This is what I meant to reference.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

724 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