Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Check Date formats before updating database

What is the best way to check for formatting of dates in a vb form before it get send to the db?

I was considering the text change on the field, but not sure that's the best way.

Code sample would be great.

Thanks in advance.

Tim
0
TimSweet220
Asked:
TimSweet220
  • 2
1 Solution
 
aprestoCommented:
Hi TimSweet220,

do you want to check the format or check the date is valid?

you could just check it is valid and then strip it into the format you want:

If IsDate(strDate) then
    //Do something
Else
   //not valid date
End If


Apresto
0
 
aprestoCommented:
the universally accepted date format is yyyy/mm/dd, i would recommend you use that format
0
 
ZeonFlashCommented:
If you're going to update the DB on a button click and want to validate the date before that, I would suggest using the .Leave event.

    Private Sub txtDate_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDate.Leave
        If txtDate.Text = Nothing Then
            Exit Sub
        ElseIf Not IsDate(txtDate.Text) Then
            MessageBox.Show("The date is invalid!", "Invalid Date", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
            txtDate.Focus()
            txtDate.SelectAll()
            Exit Sub
        End If
    End Sub
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now