issue with timer and record deletion

G

Guest

I'm not sure if this is the right place to post this, but I'm hoping someone
else has ran into this issue and possibly has a workaround. (I think this is
a bug in Access 2003).

I've created a parent table, tblParent, with a primary key.
I've created a child table, tblChild, with a foreign key to parent,
enforcing referential integrity.
I've created a simple single form for both, frmParent and frmChild.
In frmParent, I create a timer (value of say 2000 ms) that runs some code
OnTimer (in this example it just immediately returns).
In frmChild, I made a button that deletes the current record OnClick using
the following code (except for the msgbox, the code was generated by the
wizard):

MsgBox "About to delete child..."
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70

The issue I've noticed is if I have both frmParent and frmChild open at the
same time and allow frmParent to have focus for at least the length of the
timer (in this case 2 seconds), when I attempt to delete the child, the
current PARENT deletes. If I open the parent and child forms but do not
allow the 2 seconds to pass, the delete works as expected. Also, if I remove
the MsgBox call before the DoCmds in the child form, it works fine too.

Can someone else replicate and verify this? If this is a bug, does anyone
have a workaround or even know how to report it?

Thanks.
 
A

Albert D. Kallal

When forms timer code runs, the form does get the focus (in some
cases....read on...)

And, since your deletion code does not specify what form, but in fact
triggers menus, then you can see the problem.

I would first NOT use, or rely on DoMenuItem.

I would use something like:

dim strSql as string

if isnull(me.ID) = true then
strSql = "delete * from tblChild where ID = " & me.ID
currentdb.Execute strSql
end if

docmd.Close acForm, me.name

However, lets try and get your menu code to work anyway (as mentioned, I
don't like that code very much as it can get into trouble if the focus of
the form changes, and your case is a good example of this).

I would simply put in some code to pull/put the focus back to your form
*right* before the deletion code.
MsgBox "About to delete child..."
docmd.SelectObject acForm, me.name <--- add this line to pull focus
back...
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70

As mentioned, when the timer event runs, the focus can temporary switch to
the other form, this is especially so if that timer code does things like
set focus. (I can't really say exactly in what cases the focus will
change...but it does happen, and usually this happens WHEN YOU USE CUSTOM
menu bars. However, your example just shows again that when you use built in
MENU BARS, this focus seems to change even with the built in ones.

So, try the above extra line of code...
 
G

Guest

That seemed to work. Thanks!

Albert D. Kallal said:
When forms timer code runs, the form does get the focus (in some
cases....read on...)

And, since your deletion code does not specify what form, but in fact
triggers menus, then you can see the problem.

I would first NOT use, or rely on DoMenuItem.

I would use something like:

dim strSql as string

if isnull(me.ID) = true then
strSql = "delete * from tblChild where ID = " & me.ID
currentdb.Execute strSql
end if

docmd.Close acForm, me.name

However, lets try and get your menu code to work anyway (as mentioned, I
don't like that code very much as it can get into trouble if the focus of
the form changes, and your case is a good example of this).

I would simply put in some code to pull/put the focus back to your form
*right* before the deletion code.

docmd.SelectObject acForm, me.name <--- add this line to pull focus
back...

As mentioned, when the timer event runs, the focus can temporary switch to
the other form, this is especially so if that timer code does things like
set focus. (I can't really say exactly in what cases the focus will
change...but it does happen, and usually this happens WHEN YOU USE CUSTOM
menu bars. However, your example just shows again that when you use built in
MENU BARS, this focus seems to change even with the built in ones.

So, try the above extra line of code...


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.members.shaw.ca/AlbertKallal
 

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