Can I put a contol value into a validation text?

  • Thread starter Thread starter Guest
  • Start date Start date
-----Original Message-----
I want to say;

"Invalid date: Enter a date later than " [DateFrom]

A better option would be to use the textbox's
BeforeUpdate event to ensure that the value entered by
the user meets your requirements.

i.e.

Private Sub txtDateTo_BeforeUpdate(Cancel As Integer)
Const MsgText As String = "Invalid date: Enter a date
later than "
Const MsgTitle As String = "Invalid date"
If CDate(Me.txtDateTo) <= CDate(Me.txtDateFrom) Then
MsgBox MsgText, vbExclamation + vbOkOnly, MsgTitle
Cancel = True
End If
End Sub
 
Nick, are you doing this in the table, or in the form?

Table context
==========
You do not know whether the user will enter the From date before the To
date, so you need to use the Validation Rule of the table (not that of the
field).
1. Open your table in Design view.
2. Open the Properties box (View menu).
3. Enter this beside the table's validation rule (the one in the Properties
box, not the one in the lower pane of table design, which is the rule for
the field):
([FromDate] Is Null) OR ([ToDate] Is Null) OR ([FromDate] <=
[ToDate])
4. For the Validation Text, just use:
'To Date' cannot be before 'From Date'.

Form context
==========
In the context of a form, use the form's BeforeUpdate event to perform the
comparision:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ToDate < Me.FromDate Then
Cancel = True
MsgBox "Enter a date later than " & Me.[DateFrom]
Me.ToDate.SetFocus
End If
End Sub
 
in the form

Allen Browne said:
Nick, are you doing this in the table, or in the form?

Table context
==========
You do not know whether the user will enter the From date before the To
date, so you need to use the Validation Rule of the table (not that of the
field).
1. Open your table in Design view.
2. Open the Properties box (View menu).
3. Enter this beside the table's validation rule (the one in the Properties
box, not the one in the lower pane of table design, which is the rule for
the field):
([FromDate] Is Null) OR ([ToDate] Is Null) OR ([FromDate] <=
[ToDate])
4. For the Validation Text, just use:
'To Date' cannot be before 'From Date'.

Form context
==========
In the context of a form, use the form's BeforeUpdate event to perform the
comparision:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ToDate < Me.FromDate Then
Cancel = True
MsgBox "Enter a date later than " & Me.[DateFrom]
Me.ToDate.SetFocus
End If
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Nick in Tokyo said:
I want to say;

"Invalid date: Enter a date later than " [DateFrom]

thanks
 
Back
Top