Custom Error Messages

R

Ripper

I have a single form that contains several fields from tblStudents. I set
the required property to true on several key fields. I am attempting to use
custom error messages in the BeforeUpdate properties without any success. If
I set the..

If (Me.StuID) IsNull Then ... (Create Error Msgbox) I can just tab right
by it without an error. How do I create a custom error message that will
open if the field is null and the user just tabs by the field? We can assume
that the form is activated by entering some kind of information before we tab
past that field.
 
K

Keith Wilby

Ripper said:
I have a single form that contains several fields from tblStudents. I set
the required property to true on several key fields. I am attempting to
use
custom error messages in the BeforeUpdate properties without any success.
If
I set the..

If (Me.StuID) IsNull Then ... (Create Error Msgbox) I can just tab right
by it without an error. How do I create a custom error message that will
open if the field is null and the user just tabs by the field? We can
assume
that the form is activated by entering some kind of information before we
tab
past that field.

You're using SQL in VBA instead of the IsNull function. Try

If IsNull(Me.StuID) Then

Keith.
www.keithwilby.com
 
R

Ripper

Yep. Just pass right by like nothing is wrong. I did place this event in
the BeforeUpdate section. I do have conditional formatting on the field to
set it to yellow background when it has the focus. Could that affect the
field?
 
B

Beetle

You should be using the *forms* Before Update event to trap for this.
If your StuID field is a numeric data type then;

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Nz(Me.StuID, 0) = 0 Then
MsgBox "You have not entered all of the required information"
Cancel = True
Me.StuID.SetFocus
End If

If it's a text field then maybe use Nz(Me.StuID, "") = ""

If you have several controls on yoru form that you need to check, then you
could use the Tag property rather than coding for each control separately.
For example, you could enter a 1 in the Tag property of each of those
controls, then your code might look like;

Dim ctl As Control

For Each ctl in Me.Controls
If ctl.Tag = 1 Then
If Nz(ctl, "") = "" Then
MsgBox "You have not entered all of the required information."
Cancel = True
End If
End If
Next ctl
 
D

Dale Fye

Actually, I prefer to use a function I wrote (IsNullOrBlank) for this purpose.

Public Function IsNullOrBlank(SomeValue as Variant) as boolean

IsNullOrBlank = False

IF IsNull(SomeValue) Then
IsNullOrBlank = True
Elseif len(SomeValue) = 0 then
IsNullOrBlank = True
Endif
End Function

Then I put code in the forms BeforeUpdate event that looks something like:

Private Sub Form_BeforeUpdate(Cancel as Integer)

If IsNullOrBlank(me.StuID) Then
msgbox "Enter a student ID"
me.StuID.setfocus
Cancel = true
Elseif IsNullOrBlank(me.[someother field]) Then
msgbox "Enter ...."
me.[SomeOther Field].setfocus
Cancel = true
End if

End Sub

If you are absolutely set on forcing your user to fill in the StuID before
the cursor is allowed to leave that control, then you could put the test in
the controls LostFocus event.

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.
 
K

Klatuu

If IsNullOrBlank(me.StuID) Then

I would use
If Len(Me.StuID & vbNullString) = 0 Then

The way VBA works internally, it is more efficient.

Note to the OP regarding tabbing right through the control.
If you do not change the value in the control, The Before Update and After
Upate events do not fire (nothing to update) That is why the Form Before
update event is a better choice.
--
Dave Hargis, Microsoft Access MVP


Dale Fye said:
Actually, I prefer to use a function I wrote (IsNullOrBlank) for this purpose.

Public Function IsNullOrBlank(SomeValue as Variant) as boolean

IsNullOrBlank = False

IF IsNull(SomeValue) Then
IsNullOrBlank = True
Elseif len(SomeValue) = 0 then
IsNullOrBlank = True
Endif
End Function

Then I put code in the forms BeforeUpdate event that looks something like:

Private Sub Form_BeforeUpdate(Cancel as Integer)

If IsNullOrBlank(me.StuID) Then
msgbox "Enter a student ID"
me.StuID.setfocus
Cancel = true
Elseif IsNullOrBlank(me.[someother field]) Then
msgbox "Enter ...."
me.[SomeOther Field].setfocus
Cancel = true
End if

End Sub

If you are absolutely set on forcing your user to fill in the StuID before
the cursor is allowed to leave that control, then you could put the test in
the controls LostFocus event.

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.



Ripper said:
I have this in the before update of the field not the form.
 
D

Dale Fye

Dave,

I used that code forever, but found the function to be easier for others to
read during peer reviews.

--
Dale

email address is invalid
Please reply to newsgroup only.



Klatuu said:
If IsNullOrBlank(me.StuID) Then

I would use
If Len(Me.StuID & vbNullString) = 0 Then

The way VBA works internally, it is more efficient.

Note to the OP regarding tabbing right through the control.
If you do not change the value in the control, The Before Update and After
Upate events do not fire (nothing to update) That is why the Form Before
update event is a better choice.
--
Dave Hargis, Microsoft Access MVP


Dale Fye said:
Actually, I prefer to use a function I wrote (IsNullOrBlank) for this purpose.

Public Function IsNullOrBlank(SomeValue as Variant) as boolean

IsNullOrBlank = False

IF IsNull(SomeValue) Then
IsNullOrBlank = True
Elseif len(SomeValue) = 0 then
IsNullOrBlank = True
Endif
End Function

Then I put code in the forms BeforeUpdate event that looks something like:

Private Sub Form_BeforeUpdate(Cancel as Integer)

If IsNullOrBlank(me.StuID) Then
msgbox "Enter a student ID"
me.StuID.setfocus
Cancel = true
Elseif IsNullOrBlank(me.[someother field]) Then
msgbox "Enter ...."
me.[SomeOther Field].setfocus
Cancel = true
End if

End Sub

If you are absolutely set on forcing your user to fill in the StuID before
the cursor is allowed to leave that control, then you could put the test in
the controls LostFocus event.

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.



Ripper said:
I have this in the before update of the field not the form.
--
Thanks As Always
Rip


:

I have a single form that contains several fields from tblStudents. I set
the required property to true on several key fields. I am attempting to
use
custom error messages in the BeforeUpdate properties without any success.
If
I set the..

If (Me.StuID) IsNull Then ... (Create Error Msgbox) I can just tab right
by it without an error. How do I create a custom error message that will
open if the field is null and the user just tabs by the field? We can
assume
that the form is activated by entering some kind of information before we
tab
past that field.

You're using SQL in VBA instead of the IsNull function. Try

If IsNull(Me.StuID) Then

Keith.
www.keithwilby.com
 
K

Klatuu

Nothing wrong with your code at all, Dale. In fact, I think using such a
function for things that you will be doing all the time is a great idea.

In fact, here is one I like because I get tired of writing

If x >= y and x <= Z Then

Public Function IsBetween(varCheckVal As Variant, varLowVal As Variant, _
varHighVal As Variant) As Boolean
IsBetween = (varCheckVal >= varLowVal And varCheckVal <= varHighVal)
End Function


Works for any data type. So all you need is

If IsBetween(x, y, z) Then



--
Dave Hargis, Microsoft Access MVP


Dale Fye said:
Dave,

I used that code forever, but found the function to be easier for others to
read during peer reviews.

--
Dale

email address is invalid
Please reply to newsgroup only.



Klatuu said:
If IsNullOrBlank(me.StuID) Then

I would use
If Len(Me.StuID & vbNullString) = 0 Then

The way VBA works internally, it is more efficient.

Note to the OP regarding tabbing right through the control.
If you do not change the value in the control, The Before Update and After
Upate events do not fire (nothing to update) That is why the Form Before
update event is a better choice.
--
Dave Hargis, Microsoft Access MVP


Dale Fye said:
Actually, I prefer to use a function I wrote (IsNullOrBlank) for this purpose.

Public Function IsNullOrBlank(SomeValue as Variant) as boolean

IsNullOrBlank = False

IF IsNull(SomeValue) Then
IsNullOrBlank = True
Elseif len(SomeValue) = 0 then
IsNullOrBlank = True
Endif
End Function

Then I put code in the forms BeforeUpdate event that looks something like:

Private Sub Form_BeforeUpdate(Cancel as Integer)

If IsNullOrBlank(me.StuID) Then
msgbox "Enter a student ID"
me.StuID.setfocus
Cancel = true
Elseif IsNullOrBlank(me.[someother field]) Then
msgbox "Enter ...."
me.[SomeOther Field].setfocus
Cancel = true
End if

End Sub

If you are absolutely set on forcing your user to fill in the StuID before
the cursor is allowed to leave that control, then you could put the test in
the controls LostFocus event.

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.



:

I have this in the before update of the field not the form.
--
Thanks As Always
Rip


:

I have a single form that contains several fields from tblStudents. I set
the required property to true on several key fields. I am attempting to
use
custom error messages in the BeforeUpdate properties without any success.
If
I set the..

If (Me.StuID) IsNull Then ... (Create Error Msgbox) I can just tab right
by it without an error. How do I create a custom error message that will
open if the field is null and the user just tabs by the field? We can
assume
that the form is activated by entering some kind of information before we
tab
past that field.

You're using SQL in VBA instead of the IsNull function. Try

If IsNull(Me.StuID) Then

Keith.
www.keithwilby.com
 

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

Top