Create date validation public function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Happy New Year to you all

My app has several forms where users enter or update data.
There are 2 fields for date: ADate and CDate where Adate is the assessment
date and Cdate is the completion date, as txtADate and txtCDate.

I am creating a public function to call from a before update event of the
forms.
The function will check if the date is between 1 Jan 1998 and today’s date.

I can’t figure out how to check for the value of the date in a public
function.

If I write separate code for each form I put

If txtADate <= #1/1/1998# Or txtADate > Now Then
Cancel = True
MsgBox "Date must be between 1998 and today's date.", , "Error"
End if

If txtCDate <= #1/1/1998# Or txtCDate > Now Then
MsgBox "Date must be between 1998 and today's date.", , "Error"
End if


My public function looks like this:

Public Function fnDateOutsideRange(frm As Form) As Boolean

Dim ctl As Access.Control

For Each ctl In frm.Controls
With ctl
If .Tag = "DateRange" Then
boolDate = False
* * How do I say if the date <= #1/1/1998# or > Now Then
For both txt controls txtADate and txtCDate ?**
boolDate = True
End If
If boolDate Then
MsgBox "Date must be between 1998 and today's date.", , "Error"
End If
 
First, sorry to everyone for using RTF. I wanted to preserve the formatting and eliminate any line wrapping.

Liz, try this function:

Public Function CheckDate(frm As Form) As Boolean

Dim ctl As Control

For Each ctl In frm.Controls
With ctl
If .Tag = "DateRange" Then
If ctl.Value <= #1/1/1998# Or ctl.Value > Date Then
MsgBox "Date must be between 1998 and today's date.", vbExclamation, "Date Range Error"
ctl.SetFocus
CheckDate = False
Exit Function
End If
End If
End With
Next

CheckDate = True

End Function

Good luck.

Happy New Year.

Sco

M.L. "Sco" Scofield, Microsoft Access MVP, MCSD, MCP, MSS, A+
Useful Metric Conversion #18 of 19: 8 nickels = 2 paradigms (My personal favorite)
Miscellaneous Access and VB "stuff" at www.ScoBiz.com


Liz James said:
Happy New Year to you all

My app has several forms where users enter or update data.
There are 2 fields for date: ADate and CDate where Adate is the assessment
date and Cdate is the completion date, as txtADate and txtCDate.

I am creating a public function to call from a before update event of the
forms.
The function will check if the date is between 1 Jan 1998 and today’s date.

I can’t figure out how to check for the value of the date in a public
function.
<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top