inconsistency in forms behavior

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

Guest

i'm having a problem with the inconsistency of the behavior of the code in
the forms modules, i'm working with inbound forms and i'm trying to trap the
event of blank or null fields, so i'm putting the following code on the exit
event of the controls:

if me!controlname="" or isnull(me!controlname)=true then
me!controlname.setfocus
end if

for some reason it does work in some of the forms but not in others, could
someone please tell what's going on, or how can i fix this.

thanks
 
The focus is already in the control when the Exit event runs, so the
SetFocus does nothing. Instead, cancel the event:
If ... Then
Cancel = True

The reason it would be working sometimes is that you may have the Required
property set to Yes for some fields. If you start making an entry and
backspace it out, Access will keep you in the control until you enter
something (unless you undo the field).

The more serious issue, though is that this code still won't work. If the
user clicks the mouse into the 4th control, focus may never go to the 3rd
one, and so its Exit event never fires. Set set the Required property for
the field in table design, and your update/append query will then fail.
Alternatively, you will have to write code to examine each control before
you execute your append/update query to write the record.

Of course, it would be much easier to use a bound form, so you can use the
BeforeUpdate event of the form to perform the record-level validation.
 
mariobro said:
i'm having a problem with the inconsistency of the behavior of the code in
the forms modules, i'm working with inbound forms and i'm trying to trap the
event of blank or null fields, so i'm putting the following code on the exit
event of the controls:

if me!controlname="" or isnull(me!controlname)=true then
me!controlname.setfocus
end if

for some reason it does work in some of the forms but not in others, could
someone please tell what's going on, or how can i fix this.


I don't know why it might work in some places, but the way
to validate data is to use the BeforeUpdate event with
Cancel = True (to prevent the focus from moving) if it
doesn't pass the tests.

If you want to check it in each control, use the control's
BeforeUpdate event:
If Nz(Me!controlname, "") = "" Then Cancel = True

But, you may want to consider waiting until the record is
being updated by using the form's BeforeUpdate event to
check all the required controls.
 
Hi Marshall, thanks for your promp response, i tried your sugestion and it
works, but only if type something on the control and then i delete it if just
hit enter it dosent work. i setp thru the code leaving my original code on
the exit event routine and weird thing is that i found out is that it goes
thru the evaluation of the condition whcih is true, then it goes to the next
line and executes the setfocus instruction but it dosent do anything, it
follows the execution whit the next statement after the end if line. And does
not all after that there's a piece of code that validates the controls value
for a date range and if it isn't true then it executes the same setfocus
instruction only this time it does work. so my question is why it dosen't
work on the first evaluation but it does on the second.
 
As Allen already explained, the focus can not be set in an
Exit event.

As we both said before, a control's BeforeUpdate event won't
do anything unless the user actually enters/changes the
value. That's why we recommend using the form's
BeforeUpdate event istead of any individual control's event.
 
that's what i'm confuse by , if what you're saying is true then why the
following code works:

if me!controlname < dateadd("m",-1,date) then
me!controlname.setfocus
end if

and why on the other form the form on the exit event this also works.

if me!controlname="" or isnull(me!controlname)=true then
me!controlname.setfouces
end if

Marshall Barton said:
As Allen already explained, the focus can not be set in an
Exit event.

As we both said before, a control's BeforeUpdate event won't
do anything unless the user actually enters/changes the
value. That's why we recommend using the form's
BeforeUpdate event istead of any individual control's event.
--
Marsh
MVP [MS Access]


Hi Marshall, thanks for your promp response, i tried your sugestion and it
works, but only if type something on the control and then i delete it if just
hit enter it dosent work. i setp thru the code leaving my original code on
the exit event routine and weird thing is that i found out is that it goes
thru the evaluation of the condition whcih is true, then it goes to the next
line and executes the setfocus instruction but it dosent do anything, it
follows the execution whit the next statement after the end if line. And does
not all after that there's a piece of code that validates the controls value
for a date range and if it isn't true then it executes the same setfocus
instruction only this time it does work. so my question is why it dosen't
work on the first evaluation but it does on the second.
 
I don't understand enough about your code to be able to
explain it. All I can say is that I've never seen a text
box's Exit event that could change the focus.

Even if it could, it still wouldn't be able to deal with the
issue of a user that never enters the text box.
 
Back
Top