Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: dates


Message #1 by "Ken" <ken.mcintosh@s...> on Mon, 12 Mar 2001 17:26:39
Hi,



Can anyone tell me how to validate each part of a date. I need to make 

sure the user only inputs from 01-31 for the day, 01-12 for the month and 

96-02 for the year.  I'm using the mask edit control to search for 

specific records in a database.  The code below traps an empty date but I 

also need to make sure to check each entry for a valid day/month/year.



How is that done..??



Thanks in advance

Ken



Private Sub cmdOK_Click()

If mskFrom.Text = "  /  /  " Then

    MsgBox "You must enter valid a date", vbExclamation, "From Date"

    mskFrom.SetFocus

    Else

        MyDateFrom = mskFrom.Text

        frmFrom.Hide

        frmTo.Show

        Unload Me

End If

End Sub



Message #2 by Brian Skelton <brian_skelton@o...> on Mon, 12 Mar 2001 17:53:14 GMT
Seems to me that you just need to check that the date is 

between 01/01/1996 and 30/12/2002?



if this is the case change your if test to something like:



If mskFrom.Text = "  /  /  " Or cdate(mskFrom.Text)< #01/01/96

# Or cdate(mskFrom.Text)> #30/12/02# Then



BDS



---- Original message ----

>Date: Mon, 12 Mar 2001 17:26:39

>From: "Ken" <ken.mcintosh@s...>

>Subject: [access] dates

>To: "Access" <access@p...>

>

>Hi,

>

>Can anyone tell me how to validate each part of a date. I 

need to make 

>sure the user only inputs from 01-31 for the day, 01-12 for 

the month and 

>96-02 for the year.  I'm using the mask edit control to 

search for 

>specific records in a database.  The code below traps an 

empty date but I 

>also need to make sure to check each entry for a valid 

day/month/year.

>

>How is that done..??

>

>Thanks in advance

>Ken

>

>Private Sub cmdOK_Click()

>If mskFrom.Text = "  /  /  " Then

>    MsgBox "You must enter valid a date", vbExclamation, 

"From Date"

>    mskFrom.SetFocus

>    Else

>        MyDateFrom = mskFrom.Text

>        frmFrom.Hide

>        frmTo.Show

>        Unload Me

>End If

>End Sub

>

>
Message #3 by "Brian Paniccia" <bpanicci@n...> on Mon, 12 Mar 2001 16:49:35 -0500
Ken,



This is best solved by parsing, the use of left right mid and instring

functions are used to segment each component

of the overall date mm dd yyyy or (yy) and then check it's value against

your desired range.  You can approach it from a couple of ways.  One if you

know the leading zero must be used in mm or dd then just use



let intMonth = int (left(the date as string,2))

if intDay <1 or intMonth >12 then

    fail

end if



ect for each part then

re-combine the components of intday ,  and do an isdate (combined string)

to further validate the days is valid for the month IE. 07/31 would fail



Their really is no better way from a standpoint of simplicity and accuracy

to this otherwise.

If this above code example is not clear look under help of left,right,mid

string functions, isdate or e-mail again

and If I have time I'll write a more complete function





BrianSigma















----- Original Message -----

From: "Ken" <ken.mcintosh@s...>

To: "Access" <access@p...>

Sent: Monday, March 12, 2001 5:26 PM

Subject: [access] dates





> Hi,

>

> Can anyone tell me how to validate each part of a date. I need to make

> sure the user only inputs from 01-31 for the day, 01-12 for the month and

> 96-02 for the year.  I'm using the mask edit control to search for

> specific records in a database.  The code below traps an empty date but I

> also need to make sure to check each entry for a valid day/month/year.

>

> How is that done..??

>

> Thanks in advance

> Ken

>

> Private Sub cmdOK_Click()

> If mskFrom.Text = "  /  /  " Then

>     MsgBox "You must enter valid a date", vbExclamation, "From Date"

>     mskFrom.SetFocus

>     Else

>         MyDateFrom = mskFrom.Text

>         frmFrom.Hide

>         frmTo.Show

>         Unload Me

> End If

> End Sub

>

>
Message #4 by "John Ruff" <papparuff@c...> on Mon, 12 Mar 2001 14:51:45 -0800
Ken,



Set the format property of the date textbox to mm/dd/yyyy (We'll call it

txtDate).  If a person enters a date that is not in the proper format, an

error will occur that you can trap.



If you want to trap a "date range" error, then add the following code to the

txtDate textbox's Before_Update event:  Any date entered that is before

01/01/1996 or after 12/31/2002 will be captured and the user will be

notified of the error.



Private Sub txtDate_BeforeUpdate(Cancel As Integer)



    If  txtDate < #1/1/1996# Or txtDate > #12/31/2002#) Then

        MsgBox "Date is in error"

        SendKeys "{ESC}"

        Cancel = True

    End If



End Sub



If you want to capture the day, month and year; add the following code to

the AfterUpdate event of the txtDate textbox.  In this example, I've added

three more textboxes.  They are called txtDay, txtMonth, and txtYear.  The

DatePart function permits you to easily capture the day, month, and year

entered into the txtDate field.  The captured data will be placed into the

appropriate textboxes.



Private Sub txtDate_AfterUpdate()



        txtDay = DatePart("d", txtDate)

        txtMonth = DatePart("m", txtDate)

        txtYear = DatePart("yyyy", txtDate)



End Sub



Last but not least.  Add the following code to the form's On Error event.

If a user enters characters that do not conform with the "mm/dd/yyyy" format

of the txtDate field, an error message will be displayed notifying the user

of the error.



Private Sub Form_Error(DataErr As Integer, Response As Integer)

Const conErroneousDate = 2113

    Dim strMsg As String



    If DataErr = conErroneousDate Then

        Response = acDataErrContinue

        strMsg = "The Date is incorrect, pleas re-enter"

        MsgBox "the date is incorrect"

        DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

    End If



I hope this helps.





John Ruff - The Eternal Optimist :)

 -----Original Message-----

From: 	Ken [mailto:ken.mcintosh@s...]

Sent:	Monday, March 12, 2001 5:27 PM

To:	Access

Subject:	[access] dates



Hi,



Can anyone tell me how to validate each part of a date. I need to make

sure the user only inputs from 01-31 for the day, 01-12 for the month and

96-02 for the year.  I'm using the mask edit control to search for

specific records in a database.  The code below traps an empty date but I

also need to make sure to check each entry for a valid day/month/year.



How is that done..??



Thanks in advance

Ken



Private Sub cmdOK_Click()

If mskFrom.Text = "  /  /  " Then

    MsgBox "You must enter valid a date", vbExclamation, "From Date"

    mskFrom.SetFocus

    Else

        MyDateFrom = mskFrom.Text

        frmFrom.Hide

        frmTo.Show

        Unload Me

End If

End Sub


  Return to Index