Solved

Automatic Date format functionality

Posted on 2008-10-25
9
566 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 16

Accepted Solution

by:
ororiole earned 250 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Arka3L
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now