i have taken a masked text box for entering dob
however i need to validate it...ie
suppose if a user enters a date more than the current date
it should give an error.
i tried using system.datetime fun however i get error as it is a part of system and hence
cannot be used as expression...
pl tell me how to do!

cya
Rohan

You have to create a date object from the text box text before you can compare it to another date object

I believe not a VB.NET programmer :/

System.DateTime.Now returns current date (and time).
You could validate dob in following way

Dim MinDate As Date
Dim dob As Date
MinDate = CDate("1.1.2008")
dob = CDate(MaskedTextBox1.Text)
' Check that dob is between MinDate and Now()
If MinDate <= dob AndAlso dob <= System.DateTime.Now Then
  ' Dob is valid
Else
  ' Dob is outside date bounds
End If

This checks that dob is between 1. Jan. 2008 and current date. Change "1.1.2008" to match your date format. And of course, set a proper start date.

Pl. bear with my guys i am very new at vb.net & also as a programer.:)

1. Ya...the code is working however, the format is mm/dd/yyyy format . how can i change it to dd/mm/yyyy format ?

Secondly when i user enters date say mm/dd/yyyy format n then leaves the text box he should get a prompt as :
You have entered
Date : dd (as entered by user)
Month: mm '' ----''
Year : yyyy "------"


2. Secondly i have an attendance marking form. Here when i enter id of the student i get
his batch and also his name etc... Date is also displayed.
I want attendance to be marked for this ID only once..i mean at present the user can mark attendance(present/abs) as many times as he wishes....but thats not right...
I want that for a sp. date n ID attendance should be markd only once how can this be done ??
If a user tries to mark attendance again ..he should get a prompt as to Attendn already marked ?
Here which field to set unique ?? am confused.....

Awating for your dearly help as always....
BECAUSE OF UR RESPONSES I AM LEARNING EACH DAY SOMETHING NEW :) THANKS A LOT

cya
Rohan

System.DateTime.Now returns current date (and time).
You could validate dob in following way

Dim MinDate As Date
Dim dob As Date
MinDate = CDate("1.1.2008")
dob = CDate(MaskedTextBox1.Text)
' Check that dob is between MinDate and Now()
If MinDate <= dob AndAlso dob <= System.DateTime.Now Then
  ' Dob is valid
Else
  ' Dob is outside date bounds
End If

This checks that dob is between 1. Jan. 2008 and current date. Change "1.1.2008" to match your date format. And of course, set a proper start date.

Here's an answer for your first question.

The date format depends on your locale. This code "swaps" mm/dd/yyyy to dd/mm/yyyy

Dim DD As String
Dim MM As String
Dim YYYY As String
Dim NewDob As String

' Make a new date (as a string), use CDate(NewDob) to cast string to date (if dd/mm/yyyy is a valid date format in this locale)
DD = dob.Day.ToString
MM = dob.Month.ToString
YYYY = dob.Year.ToString
NewDob = DD & "/" & MM & "/" & YYYY

Use LostFocus event to show user what he/she entered

Private Sub MaskedTextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.LostFocus
  Dim DD As String
  Dim MM As String
  Dim YYYY As String
  Dim dob As Date

  If Date.TryParse(MaskedTextBox1.Text, dob) Then
    DD = dob.Day.ToString
    MM = dob.Month.ToString
    YYYY = dob.Year.ToString
    MessageBox.Show("You have entered" & Environment.NewLine & _
      "Day: " & DD & Environment.NewLine & _
      "Month: " & MM & Environment.NewLine & _
      "Year: " & YYYY, _
      "Check Date", _
      MessageBoxButtons.OK, _
      MessageBoxIcon.Information)
  Else
    MessageBox.Show("The date you entered is not a valid date", _
      "Invalid Information", _
      MessageBoxButtons.OK, _
      MessageBoxIcon.Exclamation)
  End If

End Sub

and the same TryParse fix to my previous code :$

Dim MinDate As Date
Dim dob As Date

MinDate = CDate("1.1.2008")
If Date.TryParse(MaskedTextBox1.Text, dob) Then
  ' Check that dob is between MinDate and Now()
  If MinDate <= dob AndAlso dob <= System.DateTime.Now Then
    ' Dob is valid
  Else
    ' Dob is outside date bounds
  End If
Else
  ' Date was not valid
End If

Now the second question. This has nothing to do with the previous question, right?
Ok, is this some data entry form with a database behind it? With students and courses I guess?

Here's how I would do it. A student can attend multiple courses. You have StudentID in the Students-table and CourseID in Courses-table. You'll need a junction table "Attendance":

StudentID INTEGER
CourseID INTEGER
Present BOOLEAN

When a user enters student attendance to a course, you check if StudentID and CourseID exist in the Attendance table. If it exists then the student is present or absent in that course and the information has already been set. Otherwise insert StudentID, CourseID and Present information to the table.

Hi,
Any specific reason for using Masked Text Box? If not, u can also use DateTimePicker control to get date / time input.

Teme64, i shall surely try your suggesstions.. C actually i have an attendance form ( attendance database table with fileds like id, name,course name , attendance, date )
in the attendance form i have fields like id , name , cname, date, and check box for present and absent...
When i enter a id in the id box , for that corresponding id name ,course he/she has registered for etc...gets listed(these fields are read only) my only task is to mark present/absent.
However if for a day /date the attendance is already marked it should generate a error as to attendance already marked...
at present i am ABLE to mark the attendance for the ID i enter as many number of times as i can...and this is logically incorrect!

So i need help in this matter....

hi selvaganapathy, yeah there is no specific reason to use masked text box...although i used it 'coz if a person is born in some 1950's then i ll have to scroll back to these many days....( i guess v need to do so) so for comfort i am using mtbox.

cya
AWATING RESPONSE AS ALWAYS...

THANK YOU
ROHAN

Hi,
Any specific reason for using Masked Text Box? If not, u can also use DateTimePicker control to get date / time input.

Ok. I would do it in this way: if the attendance is already marked, disable the option (checkbox?) to change it. This way user won't get any error messages which are always irritating.

If that's not possible, then try it this way. I assume that you have a checkbox to mark absent/present?
Disable checkbox after user clicks it

Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
  ' Update your DB
  ' Disable this checkbox
  CheckBox1.Enabled = False
End Sub

or, if you prefer error message:

Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
  ' Check if attendance marked
  ' No -> Update DB
  ' Yes -> MessageBox.Show("Allready set attendance", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub

yeah there is no specific reason to use masked text box...although i used it 'coz if a person is born in some 1950's then i ll have to scroll back to these many days....( i guess v need to do so) so for comfort i am using mtbox.

I agree Masked Textbox is much better than DateTimePicker for Data entry. But you can also type in DataTimePicker control. For example if you want to set 12/2/1950
Just type
> 12 Then Press Right Arrow
> 2 Then Press Right Arrow
> 1950

and No need to scroll.
More flexible way may be exist.
May be hard to change Masked Text box to DTPicker. Any way continue on Masked Text Box

thanks a lot KSG.....

HOWEVER ONE MORE DOUBT :

MY DIALOG IS NOT WORKING.....FOR DATE OF BIRTH

I MEAN WHEN I CLICK ON YES STILL MY TXT BOX GETS CLEAR... I WANT IT TO HAPPEN
ONLY IF USER CLICKS ON ' NO '. IF THE USER CLICK S ON YES...IT SHOULD NOT CLEAR THE TEXTBOX...BUT THEN CARRY OUT ANOTHER FUN...

HERE IS THE PIECE OF CODE :
MY MESSAGE STRING CONTAINS SERIES OF MESSAGE LINES

MsgBox(message, MsgBoxStyle.YesNo)
Dim DIA As New DialogResult
                    If Windows.Forms.DialogResult.No Then
                        TXT,Clear()
                        TXT.Focus()
                    Else
TXT1.FOCUS
                    End If

PL. GUIDE ME....I AM NOT UNDERSTANDING WHTS WRONG... ? :(

I agree Masked Textbox is much better than DateTimePicker for Data entry. But you can also type in DataTimePicker control. For example if you want to set 12/2/1950
Just type
> 12 Then Press Right Arrow
> 2 Then Press Right Arrow
> 1950

and No need to scroll.
More flexible way may be exist.
May be hard to change Masked Text box to DTPicker. Any way continue on Masked Text Box

How to check in the data base WHETHER the Attendance for a particular date of a particular id exists ???? :( WELL PLZ...DONT HELP ME HERE...LET ME TRY MYSELF.....IF I HAVE DOUBT I LL GET BAKC TO U....NEWAYZ...............PLZ HELP ME OUT ON THE ABOVE PROBLEM OF DIALOG BOX...

PL..I MAY SOUND STRANGE BUT I AM REALLY NEW TO THIS VB.NET WORLD...:(


Ok. I would do it in this way: if the attendance is already marked, disable the option (checkbox?) to change it. This way user won't get any error messages which are always irritating.

If that's not possible, then try it this way. I assume that you have a checkbox to mark absent/present?
Disable checkbox after user clicks it

Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
  ' Update your DB
  ' Disable this checkbox
  CheckBox1.Enabled = False
End Sub

or, if you prefer error message:

Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
  ' Check if attendance marked
  ' No -> Update DB
  ' Yes -> MessageBox.Show("Allready set attendance", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub

Your code

MsgBox(message, MsgBoxStyle.YesNo)
Dim DIA As New DialogResult
If Windows.Forms.DialogResult.No Then
TXT,Clear()
TXT.Focus()
Else
TXT1.FOCUS
End If

maybe changed as

Dim DIA As DialogResult = MsgBox(message, MsgBoxStyle.YesNo)
If DIA = Windows.Forms.DialogResult.No Then
   TXT,Clear()
   TXT.Focus()
Else
   TXT.FOCUS
End If

I think u know whats wrong with your code.

I HAVE ATTENDANCE FORM IN WHICH THERE ARE FILEDS LIKE ID , NAME, DATE, ATTENDANCE ETC...SIMILARLY I HAVE DATA BASE TABLE ATTENDANCE....
I NEED A CODE WHEREIN ONE CAN MARK ATTENDANCE ONLY ONCE FOR A ID FOR A PARTICULAR DATE...
SAY IF MY ID IS 1 AND TODAY IS 02/10/2008 THEN ONCE THE ATTENDANCE IS MARKED FOR THIS ID AND DATE...IF ANOTHER ATTEMPT IS MADE TO MARK ATTENDANCE FOR THE "SAME ID AND SAME DATE "THEN PROMPT SHOULD BE GIVEN AS "ATTENDANCE ALREADY

Dim cnn As OleDbConnection = New OleDbConnection
        cnn.ConnectionString = " "
        Dim strSelect As String
        Dim adapter As New OleDbDataAdapter(strSelect, cnn)
        Dim dtattendance As New DataTable
        Dim dsattendance As New DataSet
        strSelect = "select admid,vDate  from tblstudent_attendance"
        adapter.Fill(dsattendance, "Attendance")

        If (dtattendance.Rows.Count) > 0 Then
            If txtattendid.Text = dtattendance.Rows(0).Item(0) And txtdate.Text = dtattendance.Rows(0).Item(1) Then
                MsgBox("Attendance Already Marked for the day !", MsgBoxStyle.Information)
            Else

                strSelect = ("Insert into tblstudent_attendance values('" & (txtattendid.Text) & "','" & (txtattendname.Text) & "','" & (txtattendbatch.Text) & "','" & (txtdate.Text) & "','" & attendance & "','" & (cmbMonth.SelectedItem) & "','" & percent & " '")
                dbadapter.Fill(dsattendance, "attendance")
                MsgBox("Attendance Marked for " & txtdate.Text)
            End If

        End If

ERROR REPORTED : Command text was not set for the command object.
IS MY THIS CODE CORRECT ???? WILL I GET THE DESIRED RESULT AS STATED ABOVE??? PLZ TELL ME AND CORRECT MY CODE.....I AM FEDUP AND EXHAUSTED....I AM NOT GETTING THE RESULT

This article has been dead for over six months. Start a new discussion instead.