Text box validation

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

Guest

I am attempting to validate what a user types into a text box and set a
prompt if no value is entered. This is the code that I have entered, however
when running the code the text box can be blank but no prompt occurs. The
code is in the Sub for a button called Close_Form

If Me.[EquipmentName] = Null Then
MsgBox "Please ensure that an Equipment Name is entered before saving
this record"
Cancel = True
End If
 
First, you cannot compare something to null. Instead, use the IsNull()
function, i.e.:
If IsNull(Me.EquipmentName) Then

For an explanation of why this is so, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

Secondly, you need to do this in the BeforeUpdate event of the *form* (not
control), so that this gets caught before it is written to the table. By the
time the form's Close event fires, it's too late.

Thirdly, if you would like a code-free solution to prevent Access saving the
record if a value is not provided for this field, open the table in design
view, and set the field's Required property to Yes (lower pane in table
design view.)
 
that's perfect, thanks for your help
--
Kevin


Allen Browne said:
First, you cannot compare something to null. Instead, use the IsNull()
function, i.e.:
If IsNull(Me.EquipmentName) Then

For an explanation of why this is so, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

Secondly, you need to do this in the BeforeUpdate event of the *form* (not
control), so that this gets caught before it is written to the table. By the
time the form's Close event fires, it's too late.

Thirdly, if you would like a code-free solution to prevent Access saving the
record if a value is not provided for this field, open the table in design
view, and set the field's Required property to Yes (lower pane in table
design view.)

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

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

kmhnhsuk said:
I am attempting to validate what a user types into a text box and set a
prompt if no value is entered. This is the code that I have entered,
however
when running the code the text box can be blank but no prompt occurs. The
code is in the Sub for a button called Close_Form

If Me.[EquipmentName] = Null Then
MsgBox "Please ensure that an Equipment Name is entered before saving
this record"
Cancel = True
End If
 
Also, how can you validate if a user does not select a value from a combo box?
--
Kevin


kmhnhsuk said:
that's perfect, thanks for your help
--
Kevin


Allen Browne said:
First, you cannot compare something to null. Instead, use the IsNull()
function, i.e.:
If IsNull(Me.EquipmentName) Then

For an explanation of why this is so, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

Secondly, you need to do this in the BeforeUpdate event of the *form* (not
control), so that this gets caught before it is written to the table. By the
time the form's Close event fires, it's too late.

Thirdly, if you would like a code-free solution to prevent Access saving the
record if a value is not provided for this field, open the table in design
view, and set the field's Required property to Yes (lower pane in table
design view.)

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

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

kmhnhsuk said:
I am attempting to validate what a user types into a text box and set a
prompt if no value is entered. This is the code that I have entered,
however
when running the code the text box can be blank but no prompt occurs. The
code is in the Sub for a button called Close_Form

If Me.[EquipmentName] = Null Then
MsgBox "Please ensure that an Equipment Name is entered before saving
this record"
Cancel = True
End If
 
Same: Form_BeforeUpdate, assuming it's a bound combo.

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

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

kmhnhsuk said:
Also, how can you validate if a user does not select a value from a combo
box?
--
Kevin


kmhnhsuk said:
that's perfect, thanks for your help
--
Kevin


Allen Browne said:
First, you cannot compare something to null. Instead, use the IsNull()
function, i.e.:
If IsNull(Me.EquipmentName) Then

For an explanation of why this is so, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

Secondly, you need to do this in the BeforeUpdate event of the *form*
(not
control), so that this gets caught before it is written to the table.
By the
time the form's Close event fires, it's too late.

Thirdly, if you would like a code-free solution to prevent Access
saving the
record if a value is not provided for this field, open the table in
design
view, and set the field's Required property to Yes (lower pane in table
design view.)

I am attempting to validate what a user types into a text box and set
a
prompt if no value is entered. This is the code that I have entered,
however
when running the code the text box can be blank but no prompt occurs.
The
code is in the Sub for a button called Close_Form

If Me.[EquipmentName] = Null Then
MsgBox "Please ensure that an Equipment Name is entered before
saving
this record"
Cancel = True
End If
 
Yes it is a bound combo.

When I put in the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.NameofAssessorcombo) Then
Me.NameofAssessorcombo.SetFocus
Else
End If

End Sub

When editing the record using an edit form and taking out the Assessor Name
(NameofAssessorcombo) value, the prompt does not work and I can close the
form and update that record without having an Assessor Name.

Does the form have to be in Data Entry mode for this validation to work?
--
Kevin


Allen Browne said:
Same: Form_BeforeUpdate, assuming it's a bound combo.

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

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

kmhnhsuk said:
Also, how can you validate if a user does not select a value from a combo
box?
--
Kevin


kmhnhsuk said:
that's perfect, thanks for your help
--
Kevin


:

First, you cannot compare something to null. Instead, use the IsNull()
function, i.e.:
If IsNull(Me.EquipmentName) Then

For an explanation of why this is so, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

Secondly, you need to do this in the BeforeUpdate event of the *form*
(not
control), so that this gets caught before it is written to the table.
By the
time the form's Close event fires, it's too late.

Thirdly, if you would like a code-free solution to prevent Access
saving the
record if a value is not provided for this field, open the table in
design
view, and set the field's Required property to Yes (lower pane in table
design view.)

I am attempting to validate what a user types into a text box and set
a
prompt if no value is entered. This is the code that I have entered,
however
when running the code the text box can be blank but no prompt occurs.
The
code is in the Sub for a button called Close_Form

If Me.[EquipmentName] = Null Then
MsgBox "Please ensure that an Equipment Name is entered before
saving
this record"
Cancel = True
End If
 
Cancel the event if the combo is null:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.NameofAssessorcombo) Then
Cancel = True
MsgBox "Forgot the combo!"
Me.NameofAssessorcombo.SetFocus
End If

End Sub

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

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

kmhnhsuk said:
Yes it is a bound combo.

When I put in the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.NameofAssessorcombo) Then
Me.NameofAssessorcombo.SetFocus
Else
End If

End Sub

When editing the record using an edit form and taking out the Assessor
Name
(NameofAssessorcombo) value, the prompt does not work and I can close the
form and update that record without having an Assessor Name.

Does the form have to be in Data Entry mode for this validation to work?
--
Kevin


Allen Browne said:
Same: Form_BeforeUpdate, assuming it's a bound combo.


kmhnhsuk said:
Also, how can you validate if a user does not select a value from a
combo
box?
--
Kevin


:

that's perfect, thanks for your help
--
Kevin


:

First, you cannot compare something to null. Instead, use the
IsNull()
function, i.e.:
If IsNull(Me.EquipmentName) Then

For an explanation of why this is so, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

Secondly, you need to do this in the BeforeUpdate event of the
*form*
(not
control), so that this gets caught before it is written to the
table.
By the
time the form's Close event fires, it's too late.

Thirdly, if you would like a code-free solution to prevent Access
saving the
record if a value is not provided for this field, open the table in
design
view, and set the field's Required property to Yes (lower pane in
table
design view.)

I am attempting to validate what a user types into a text box and
set
a
prompt if no value is entered. This is the code that I have
entered,
however
when running the code the text box can be blank but no prompt
occurs.
The
code is in the Sub for a button called Close_Form

If Me.[EquipmentName] = Null Then
MsgBox "Please ensure that an Equipment Name is entered before
saving
this record"
Cancel = True
End If
 
Many thanks
--
Kevin


Allen Browne said:
Cancel the event if the combo is null:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.NameofAssessorcombo) Then
Cancel = True
MsgBox "Forgot the combo!"
Me.NameofAssessorcombo.SetFocus
End If

End Sub

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

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

kmhnhsuk said:
Yes it is a bound combo.

When I put in the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.NameofAssessorcombo) Then
Me.NameofAssessorcombo.SetFocus
Else
End If

End Sub

When editing the record using an edit form and taking out the Assessor
Name
(NameofAssessorcombo) value, the prompt does not work and I can close the
form and update that record without having an Assessor Name.

Does the form have to be in Data Entry mode for this validation to work?
--
Kevin


Allen Browne said:
Same: Form_BeforeUpdate, assuming it's a bound combo.


Also, how can you validate if a user does not select a value from a
combo
box?
--
Kevin


:

that's perfect, thanks for your help
--
Kevin


:

First, you cannot compare something to null. Instead, use the
IsNull()
function, i.e.:
If IsNull(Me.EquipmentName) Then

For an explanation of why this is so, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

Secondly, you need to do this in the BeforeUpdate event of the
*form*
(not
control), so that this gets caught before it is written to the
table.
By the
time the form's Close event fires, it's too late.

Thirdly, if you would like a code-free solution to prevent Access
saving the
record if a value is not provided for this field, open the table in
design
view, and set the field's Required property to Yes (lower pane in
table
design view.)

I am attempting to validate what a user types into a text box and
set
a
prompt if no value is entered. This is the code that I have
entered,
however
when running the code the text box can be blank but no prompt
occurs.
The
code is in the Sub for a button called Close_Form

If Me.[EquipmentName] = Null Then
MsgBox "Please ensure that an Equipment Name is entered before
saving
this record"
Cancel = True
End If
 
Back
Top