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
 
OK, but what's all the Ctype("19.02.2005", Date) about?

Is "19.02.2005" just the format of the date, or the actual date it will put in? I don't want to hard code any date obviously as on another day it will then insert the wrong value!

cheers,
L

PS - just tested but still doesn't work anyway, value in dtp is not passed back to database unless I go into dtp and select date.
 
Last edited:
I'm not quite sure that i understand well your question but maybe if you try this:

VB.NET:
If [/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].DateTimePicker1.Value = [/size][size=2][color=#0000ff]Date[/color][/size][size=2].Now [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].DateTimePicker1.Value = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](DateTimePicker2.Text, [/size][size=2][color=#0000ff]Date[/color][/size][size=2])
 
MessageBox.Show("It matching")
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If


btw, yes Ctype("19.02.2005", date) is european date format dd/mm/yyyy

Cheers ;)
 
What I am trying to say is;

I have a datagrid. When the user clicks EDIT or NEW, then the selected row in that datagrid is opened as a form.

One of the fields is a DateTimePicker. I have set up a function ValidData that makes sure all of the fields on the form have data in them. However, because the DateTimePicker shows the Date.Now() when loaded, then it bypasses the ValidData function.

What it doesn't do however is pass that value (Date.Now()) back to the datagrid - I have to click the DTP and select today's date.

..Therefore I'm asking:

How can I validate the DTP so that if the user clicks update without clicking into the DTP, the form sends the current value of the DTP (obviously Date.Now()) back to the datagrid...

That's where I've come unstuck. I have tried adding code to the OK button to say that if the current display of the dtp is today, then so is it's value. But it's not working...

Hope that makes it more clear what I'm trying to achieve.

Cheers,
Luke
 
Last edited:
Are dbNulls allowed?

Luke,

Does the database allow nulls for the date field?
If it is a required, then any time you add a new record, that date field should be set to the current date right away. The edit form should be bound to the field, and therefore have no need to default to the current day, since there are no nulls in the dataset.

If the field allows nulls, then there is no need to validate the contents since the date time picker gives you a valid date anyway. It shows up blank and remains blank when saved. If the date field can be blank, I think you would want to show it as blank on the edit form.

Seems that you need to remove the default to current date on the Date time picker.

I use a ComponentOne DTP - there is a property that is 'ValueIsDBNull' = true under the Data Properties
 
Yes the date field is required. However I changed it to not required to test the form correctly.

When the form is opened, the date appears in the DateTimePicker, I.E. 18th August 2005

If I do not click in the dtp, and fill the other boxes in and submit, that date (18th August 2005) is not sent back as the value - it is left as blank.

If you think about it, if a user want's the dtp value to be the date it is showing, they aren't going to click into it and select the same date, they will just assume that is the value and press submit.
I want a way so that if that happens, the date that is being shown in the dtp is saved as the value.

have I confused everyone?!?!?!?!?!?!?!?!?
 
I'm really sorry because my thoughtless reply but that's because English is not my native language and sometimes it's really hard to me to understand your questions and i'm starting to guess lol.

However, It is possible, depending on the settings of your table, for a date-time field to have a value of null, instead of any form of the date. When you have bound a date column to a DateTimePicker control though, and you navigate to a record containing a null value, it will cause an error in the control and the binding will be lost.
Using the power of inheritance, you can create your own version of the dateTimePicker that does not have this problem. You need to add a new property that you can bind to instead of the existing Value property. This new property will be able to handle Null values and so binding will work just fine. Correct me if i'm wrong. Otherwise let me know if you need an assistence to build sucha custom control.

Cheers ;)
 
Hi Kulrom,

No that's not my issue. I already have a custom dateTimePicker that is blank if the database value is null.

I'm trying to do this the other way round. If it's blank on my form, I want it to put the CurrentDate Value to the database....

Cheers,
Luke
 
Have you tried checking the .text value?

On form load display the datetimepicker1.text
MsgBox(DateTimePicker1.Text)
What do you get?

also check the state.
MsgBox("Checked = " & DateTimePicker1.Checked)

My next guess (this is a Guess) is that:
DateTimePicker1.Text = Todays Date
DateTimePicker1.Checked = False

You could try setting DateTimePicker1.Checked = True before update.
 
hmmmm

Could it be that I don't use the checking of the DTP?

I've just tried on form load and the messagebox shows 22/08/2005

I've also just tried setting Checked = False on Load, but it loads as checked????

It's becoming a pain in the a*se for something so simple. Surely dtp.value = dtp.text would do it? Obviously not!!

Luke
 
ScreenShots...

The first image is the form that pops up when adding a new revision. you can see the DTP is checked and shows today's date.

The user must fill in all fields on this form except documentation. Once done they press Submit.

The second image shows that if I haven't physically opened the DTP and changed the date value, the RS Date isn't sent across.
However, as can be seen in the other rows, if I actually click the DTP, and select a date, even being the current date and press Submit, then the RS Date is sent across.
 

Attachments

  • Image2.jpg
    Image2.jpg
    20.9 KB · Views: 262
  • Image4.jpg
    Image4.jpg
    4.4 KB · Views: 262
I cannot beleive that it is so simple ... maybe i'm wrong again but who knows. Give it a try

Join some code to the submit button

as you said you just want to pass data.now if user hasn't pick up the date.

possible solution 1

Dim myDate as Date
if dataTimePicker1.Value <> Date.Now then
myDate = dataTimePicker1.Value
Else
myDate = Date.Now
End if

possible solution 2

declare a variable with private scope (available through entire form) say

Dim newDate as boolean

then if user pick up a different date than now it will catch that;

Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
newDate = True

End Sub

then just join almost same code with previous code:

if newDate = true then
myDate = dataTimePicker1.Value
Else
myDate = date.Now
End if

i'm sure you will figure out the rest

Cheers ;)

edit: note that now you should pass new variable in your query instead dtp.value or dtp.text :)

Into INSERT statement pass myDate variable instead datatimepicker
 
Last edited:
Back
Top