Validating Date Field?

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
Hi guys,

Hope someone can help, this has had me pulling my hair out all morning.

On a small data entry form I have a few text boxes, a combobox and a dateTimePicker.

When the user fills in and clicks OK, it makes sure that all of the fields have been entered.

This is done by using a function called ValidData;

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Function[/color][/size][size=2] ValidData() [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean

[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]True

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sMessage [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] txtResponseHeader.Text = "" [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]False

[/color][/size][size=2]sMessage = "Please enter a Response Header"

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] txtResponse.Text = "" [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]False

[/color][/size][size=2]sMessage = "Please enter Response details"

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] cboCreatedBy.Text = "" [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]False

[/color][/size][size=2]sMessage = "Please select who created Response"

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] dtpCreatedDate.Text = "" [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]False

[/color][/size][size=2]sMessage = "Please select date Response took place"

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] [/size][size=2][color=#0000ff]Not[/color][/size][size=2] ValidData [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]MessageBox.Show(sMessage, "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Function

[/color][/size]

The problem I have is that by default DateTimePickers show the current date. Therefore the ValidData sees it has a value and marks it as true. But the value of the DTP is not being passed across when the user clicks OK.

Most of the time the dateTimePicker will need to show the current date anyway, it will only be changed when someone is filling the form out at a later date.

Is there a way that I can say "If dtpCreatedDate is Today() Then dtpCreatedDate.Value = dtpCreatedDate.Text"

I have tried the above but it doesn't work. I either need to get the dtp to take it's text and save as it's value, or get the validation part working and say "Open Dtp and click today's date".

Cheers,
Luke
 
Hi Kulrom,

Thanks for the reply, have tried both solutions but neither of them work!!

I am stumped. Completely....

I'm again wondering whether this problem has something to do with currency manager opening the form. Do you remember a post about the OpenFileDialog filename that was in a textbox not being sent back to the database when the user clicks Submit? Well I figured out that you have to focus on the textbox before submitting.

I'm wondering whether this is the same. Is there a way of forcing the app to think that the dtpCreatedDate_ValueChanged has actually run?
 
Wait a minute ... i assure you that if you declare and pass a myDate variable as such it will either pass dtp value or date.now ... at least will always return date.Now. If you do that right as i said above the var cannot be empty in else statement at least (will work if you don't change dtp value)

Cheers ;)
 
OK, heres the code then. Maybe I missed something:

VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] dsDWR [/size][size=2][color=#0000ff]As[/color][/size][size=2] dsSearchDWR
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] drvDetail [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataRowView
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] dvDetail [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataView
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] newDate [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] RevAdd([/size][size=2][color=#0000ff]ByRef[/color][/size][size=2] cm [/size][size=2][color=#0000ff]As[/color][/size][size=2] CurrencyManager)
 
drvDetail = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](cm.Current, DataRowView)
 
dvDetail = drvDetail.DataView
 
dsDWR = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](dvDetail.Table.DataSet, dsSearchDWR)
 
[/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2]conEmployee.Open()
 
daRevCreatedBy.Fill(DsEmployeeLookups1.RevCreatedBy)
 
[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception
 
MessageBox.Show(ex.Message)
 
[/size][size=2][color=#0000ff]Finally
 
[/color][/size][size=2]conEmployee.Close()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2]cboCreatedBy.SelectedIndex = -1
 
txtResponseHeader.Focus()
 
[/size][size=2][color=#008000]'open the form bound to the revision selected in the Revision Grid
 
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].BindingContext(dvDetail).Position = cm.Position
 
[/size][size=2][color=#0000ff]With[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].dtpCreatedDate
 
.DataBindings.Add("Text", dvDetail, "ResponseCreatedDate")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2][color=#0000ff]With[/color][/size][size=2] cboCreatedBy
 
.DataSource = DsEmployeeLookups1.RevCreatedBy
 
.DisplayMember = "EmployeeName"
 
.ValueMember = "EmployeeID"
 
.DataBindings.Add("SelectedValue", dvDetail, "ResponseCreatedBy")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2]txtDWRNo.DataBindings.Add("Text", dvDetail, "DWRNumber")
 
txtResponseHeader.DataBindings.Add("Text", dvDetail, "ResponseHeader")
 
txtResponse.DataBindings.Add("Text", dvDetail, "Response")
 
txtResponseDoc.DataBindings.Add("Text", dvDetail, "ResponseDocumentation")
 
txtRevisionNo.DataBindings.Add("Text", dvDetail, "RevisionNumber")
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].ShowDialog = DialogResult.OK [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]cm.EndCurrentEdit()
 
[/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]cm.CancelCurrentEdit()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] txtResponseDoc_LinkClicked([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.LinkLabelLinkClickedEventArgs)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strDoc [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2]strDoc = txtResponseDoc.Text
 
Process.Start(strDoc)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] btnOK_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] btnOK.Click
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myDate [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Date
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] newDate = [/size][size=2][color=#0000ff]True[/color][/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]myDate = dtpCreatedDate.Value
 
[/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]myDate = [/size][size=2][color=#0000ff]Date[/color][/size][size=2].Now
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] ValidData() [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]dtpCreatedDate.Value = myDate
 
DialogResult = DialogResult.OK
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] btnCancel_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] btnCancel.Click
 
NewRevision = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2]DialogResult = DialogResult.Cancel
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] ValidData() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean
 
[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sMessage [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] txtResponseHeader.Text = "" [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2]sMessage = "Please enter a Response Header"
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] txtResponse.Text = "" [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2]sMessage = "Please enter Response details"
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] cboCreatedBy.Text = "" [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]ValidData = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2]sMessage = "Please select who created Response"
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] ValidData [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]MessageBox.Show(sMessage, "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] btnRDBrowse_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] btnRDBrowse.Click
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] x [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] OpenFileDialog
 
[/size][size=2][color=#0000ff]With[/color][/size][size=2] x
 
.InitialDirectory = "\\uk-ban-fs1\shared"
 
.Title = "Browse Response documentation"
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] .ShowDialog = DialogResult.OK [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]txtResponseDoc.Text = .FileName
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2]txtResponseDoc.Focus()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
 
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] dtpCreatedDate_ValueChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] dtpCreatedDate.ValueChanged
 
newDate = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
End[/color][/size][size=2][color=#0000ff]Class[/color][/size]
[size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff]

[/color][/size]
 
Back
Top