Validating field

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

Guest

I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5


I am trying to validate data & force re-entry of the field if it does not
pass. I have only been able to use the setfocus from within the onfocus of
the next field, resetting it to the previous field.

Is there a way to check before leaving the field & stop the focus from
moving to the next field.

Reason:

I can enter 1 to 5 requests each with a start & end Date & time. I need to
check that end time in each row before it tries to go to the next field
because the user may not go to the next field. If a time does not pass the
criteria I want to force re-entry of that field.

Can someone help me with this please.

Thanks,
Tamra
 
Hi Tamra

You can use the BeforeUpdate event of a textbox to validate its value before
moving on. If the value fails validation, simply set the Cancel argument to
True and the focus will not move. Here is a simple example:

Private Sub TextBoxName_BeforeUpdate( Cancel as Integer)
If TextBoxName > 100 then
MsgBox "Value must be less than 100"
Cancel = True
End If
End Sub
 
Hi Graham,
It is a combobox I'm working with. I tried:

Private Sub cboEndHour1_BeforeUpdate(Cancel As Integer)
If IsNull(cboEndHour1) Then
MsgBox "End Hour Required."
Forms!MakeSchedule!cboEndHour1.SetFocus
Cancel = True
End If
End Sub

both with and w/o the setfocus, but it still accepts a null value and
advances to the next field.

Do you have any other ideas?

Thanks,
Tamra
Graham Mandeno said:
Hi Tamra

You can use the BeforeUpdate event of a textbox to validate its value before
moving on. If the value fails validation, simply set the Cancel argument to
True and the focus will not move. Here is a simple example:

Private Sub TextBoxName_BeforeUpdate( Cancel as Integer)
If TextBoxName > 100 then
MsgBox "Value must be less than 100"
Cancel = True
End If
End Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Blondie96 said:
I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5


I am trying to validate data & force re-entry of the field if it does not
pass. I have only been able to use the setfocus from within the onfocus of
the next field, resetting it to the previous field.

Is there a way to check before leaving the field & stop the focus from
moving to the next field.

Reason:

I can enter 1 to 5 requests each with a start & end Date & time. I need
to
check that end time in each row before it tries to go to the next field
because the user may not go to the next field. If a time does not pass the
criteria I want to force re-entry of that field.

Can someone help me with this please.

Thanks,
Tamra
 
Hi Tamra

Tabbing through a null field without changing it does not count an
*updating*, so the BeforeUpdate event will not fire. If this is what you
want, then use the control's Exit event instead. This can be cancelled in
the same way, but it fires before the control loses focus, whether or not it
has been updated.

Also, the Setfocus is unnecessary because the control already has the focus
and will not lose it if you cancel the event.

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Blondie96 said:
Hi Graham,
It is a combobox I'm working with. I tried:

Private Sub cboEndHour1_BeforeUpdate(Cancel As Integer)
If IsNull(cboEndHour1) Then
MsgBox "End Hour Required."
Forms!MakeSchedule!cboEndHour1.SetFocus
Cancel = True
End If
End Sub

both with and w/o the setfocus, but it still accepts a null value and
advances to the next field.

Do you have any other ideas?

Thanks,
Tamra
Graham Mandeno said:
Hi Tamra

You can use the BeforeUpdate event of a textbox to validate its value
before
moving on. If the value fails validation, simply set the Cancel argument
to
True and the focus will not move. Here is a simple example:

Private Sub TextBoxName_BeforeUpdate( Cancel as Integer)
If TextBoxName > 100 then
MsgBox "Value must be less than 100"
Cancel = True
End If
End Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Blondie96 said:
I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5


I am trying to validate data & force re-entry of the field if it does
not
pass. I have only been able to use the setfocus from within the onfocus
of
the next field, resetting it to the previous field.

Is there a way to check before leaving the field & stop the focus from
moving to the next field.

Reason:

I can enter 1 to 5 requests each with a start & end Date & time. I
need
to
check that end time in each row before it tries to go to the next field
because the user may not go to the next field. If a time does not pass
the
criteria I want to force re-entry of that field.

Can someone help me with this please.

Thanks,
Tamra
 
Thank you Graham,
I had tried the code in many of the events. It turns out the problem was, on
a certain condition I moved "" to 2 fields. I was checking for null, or
length <> 4. This am (5:00am) I changed the "" to = NULL. That fixed the
problem. I moved all the checks to the onExit subs & everything works fine.
Thanks again for the assistance.

Tamra

Graham Mandeno said:
Hi Tamra

Tabbing through a null field without changing it does not count an
*updating*, so the BeforeUpdate event will not fire. If this is what you
want, then use the control's Exit event instead. This can be cancelled in
the same way, but it fires before the control loses focus, whether or not it
has been updated.

Also, the Setfocus is unnecessary because the control already has the focus
and will not lose it if you cancel the event.

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Blondie96 said:
Hi Graham,
It is a combobox I'm working with. I tried:

Private Sub cboEndHour1_BeforeUpdate(Cancel As Integer)
If IsNull(cboEndHour1) Then
MsgBox "End Hour Required."
Forms!MakeSchedule!cboEndHour1.SetFocus
Cancel = True
End If
End Sub

both with and w/o the setfocus, but it still accepts a null value and
advances to the next field.

Do you have any other ideas?

Thanks,
Tamra
Graham Mandeno said:
Hi Tamra

You can use the BeforeUpdate event of a textbox to validate its value
before
moving on. If the value fails validation, simply set the Cancel argument
to
True and the focus will not move. Here is a simple example:

Private Sub TextBoxName_BeforeUpdate( Cancel as Integer)
If TextBoxName > 100 then
MsgBox "Value must be less than 100"
Cancel = True
End If
End Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand



I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5


I am trying to validate data & force re-entry of the field if it does
not
pass. I have only been able to use the setfocus from within the onfocus
of
the next field, resetting it to the previous field.

Is there a way to check before leaving the field & stop the focus from
moving to the next field.

Reason:

I can enter 1 to 5 requests each with a start & end Date & time. I
need
to
check that end time in each row before it tries to go to the next field
because the user may not go to the next field. If a time does not pass
the
criteria I want to force re-entry of that field.

Can someone help me with this please.

Thanks,
Tamra
 
I see you found your problem. It is a common problem. It is always best to
check for both nulls and empty strings in this situation. Here are a couple
of ways to do that:

Private Sub cboEndHour1_BeforeUpdate(Cancel As Integer)
If IsNull(cboEndHour1) or cboEndHour1 = "" Then
MsgBox "End Hour Required."
Forms!MakeSchedule!cboEndHour1.SetFocus
Cancel = True
End If
End Sub

(My preferred way)
Private Sub cboEndHour1_BeforeUpdate(Cancel As Integer)
If Nz(cboEndHour1,"") = "" Then
MsgBox "End Hour Required."
Forms!MakeSchedule!cboEndHour1.SetFocus
Cancel = True
End If
End Sub

Blondie96 said:
Hi Graham,
It is a combobox I'm working with. I tried:

Private Sub cboEndHour1_BeforeUpdate(Cancel As Integer)
If IsNull(cboEndHour1) Then
MsgBox "End Hour Required."
Forms!MakeSchedule!cboEndHour1.SetFocus
Cancel = True
End If
End Sub

both with and w/o the setfocus, but it still accepts a null value and
advances to the next field.

Do you have any other ideas?

Thanks,
Tamra
Graham Mandeno said:
Hi Tamra

You can use the BeforeUpdate event of a textbox to validate its value before
moving on. If the value fails validation, simply set the Cancel argument to
True and the focus will not move. Here is a simple example:

Private Sub TextBoxName_BeforeUpdate( Cancel as Integer)
If TextBoxName > 100 then
MsgBox "Value must be less than 100"
Cancel = True
End If
End Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Blondie96 said:
I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5


I am trying to validate data & force re-entry of the field if it does not
pass. I have only been able to use the setfocus from within the onfocus of
the next field, resetting it to the previous field.

Is there a way to check before leaving the field & stop the focus from
moving to the next field.

Reason:

I can enter 1 to 5 requests each with a start & end Date & time. I need
to
check that end time in each row before it tries to go to the next field
because the user may not go to the next field. If a time does not pass the
criteria I want to force re-entry of that field.

Can someone help me with this please.

Thanks,
Tamra
 
Bad part was:
I checked IsNull(cboEndHr)
and
Len(Nz(cboEndHr,"")=0

Neither of those worked.

Blondie96 said:
Hi Graham,
It is a combobox I'm working with. I tried:

Private Sub cboEndHour1_BeforeUpdate(Cancel As Integer)
If IsNull(cboEndHour1) Then
MsgBox "End Hour Required."
Forms!MakeSchedule!cboEndHour1.SetFocus
Cancel = True
End If
End Sub

both with and w/o the setfocus, but it still accepts a null value and
advances to the next field.

Do you have any other ideas?

Thanks,
Tamra
Graham Mandeno said:
Hi Tamra

You can use the BeforeUpdate event of a textbox to validate its value before
moving on. If the value fails validation, simply set the Cancel argument to
True and the focus will not move. Here is a simple example:

Private Sub TextBoxName_BeforeUpdate( Cancel as Integer)
If TextBoxName > 100 then
MsgBox "Value must be less than 100"
Cancel = True
End If
End Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Blondie96 said:
I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5


I am trying to validate data & force re-entry of the field if it does not
pass. I have only been able to use the setfocus from within the onfocus of
the next field, resetting it to the previous field.

Is there a way to check before leaving the field & stop the focus from
moving to the next field.

Reason:

I can enter 1 to 5 requests each with a start & end Date & time. I need
to
check that end time in each row before it tries to go to the next field
because the user may not go to the next field. If a time does not pass the
criteria I want to force re-entry of that field.

Can someone help me with this please.

Thanks,
Tamra
 
Hi Tamra

This is a good way to check for either null or empty string:
If Len(cboEndHour1 & vbNullString) = 0 Then ...

If this is a bound text field, you can set the AllowZeroLength property of
the field in the table to False (No) and then it cannot have a zero length
string and you need only test for null.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Blondie96 said:
Bad part was:
I checked IsNull(cboEndHr)
and
Len(Nz(cboEndHr,"")=0

Neither of those worked.

Blondie96 said:
Hi Graham,
It is a combobox I'm working with. I tried:

Private Sub cboEndHour1_BeforeUpdate(Cancel As Integer)
If IsNull(cboEndHour1) Then
MsgBox "End Hour Required."
Forms!MakeSchedule!cboEndHour1.SetFocus
Cancel = True
End If
End Sub

both with and w/o the setfocus, but it still accepts a null value and
advances to the next field.

Do you have any other ideas?

Thanks,
Tamra
Graham Mandeno said:
Hi Tamra

You can use the BeforeUpdate event of a textbox to validate its value
before
moving on. If the value fails validation, simply set the Cancel
argument to
True and the focus will not move. Here is a simple example:

Private Sub TextBoxName_BeforeUpdate( Cancel as Integer)
If TextBoxName > 100 then
MsgBox "Value must be less than 100"
Cancel = True
End If
End Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand



I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5


I am trying to validate data & force re-entry of the field if it does
not
pass. I have only been able to use the setfocus from within the
onfocus of
the next field, resetting it to the previous field.

Is there a way to check before leaving the field & stop the focus
from
moving to the next field.

Reason:

I can enter 1 to 5 requests each with a start & end Date & time. I
need
to
check that end time in each row before it tries to go to the next
field
because the user may not go to the next field. If a time does not
pass the
criteria I want to force re-entry of that field.

Can someone help me with this please.

Thanks,
Tamra
 
Back
Top