UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Nested If Causes Endless Loop Only After Other Code Runs, Access 2016    
 
   
RickKat81
post Sep 13 2017, 03:27 PM
Post#1



Posts: 5
Joined: 13-September 17



Hello,
Newby here... ohyeah.gif
I have two forms that are identical with record sources that are identical except for date range. FormA is based on a query that limits the records to just those entered within the last two days. FormB is based on a query that shows all records. The underlying data and displayed data are the same for both forms with the exception of date range. FormB is a copy of FormA with the record source changed to a different query.
I was having trouble with inserting unwanted date stamps in a field so I put the following code in both forms to eliminate this problem and it works flawlessly as long as I open and close just FormA or FormB.
Private Sub Employee_Exit(Cancel As Integer)
If Not Form_SheetCounts.Employee Then
If Not Form_SheetCounts.DateTime Then
Else: Form_SheetCounts.DateTime = Now()
End If
End If
End Sub
However, the following sequence always results in an endless loop:
Open FormB and then close it.
Open FormA and the instant you click on the close-X, Access begins an endless loop where I can't do anything except the <ESC> key which brings up the debug dialog, but no matter what I do... I can't get out of it unless I <Ctrl-Alt-Del> and Kill Access.
Oddly, I can open and close FormA multiple times without this happening. I can also open and close FormB multiple times without trouble, but the specific sequence above ALWAYS starts the loop.

Thanks for your help. Hope I've given enough detail.
Richard
Go to the top of the page
 
theDBguy
post Sep 13 2017, 03:49 PM
Post#2


Access Wiki and Forums Moderator
Posts: 70,669
Joined: 19-June 07
From: SunnySandyEggo


Hi,

Welcome to UtterAccess!
welcome2UA.gif

Are you saying if you comment out the Exit event code, the problem doesn't happen?

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
RickKat81
post Sep 13 2017, 04:01 PM
Post#3



Posts: 5
Joined: 13-September 17



Yes, that's correct. There is no other code on the form and it doesn't happen if I comment this code out.
Go to the top of the page
 
RickKat81
post Sep 13 2017, 04:02 PM
Post#4



Posts: 5
Joined: 13-September 17



And thank you for the warm welcome, by the way! thanks.gif
Go to the top of the page
 
theDBguy
post Sep 13 2017, 04:13 PM
Post#5


Access Wiki and Forums Moderator
Posts: 70,669
Joined: 19-June 07
From: SunnySandyEggo


Hi,

In your code If Not Form_SheetCounts.Employee, does SheetCounts happen to be FormA?

Also, is Employee a Yes/No field?

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
RickKat81
post Sep 14 2017, 07:08 AM
Post#6



Posts: 5
Joined: 13-September 17



Hello,
Oh, sorry, I was trying to simplify but let me be more specific.

Employee is a lookup field to an employee table.

FormA is SheetCounts with this code:

Private Sub Employee_Exit(Cancel As Integer)
If Not Form_SheetCounts.Employee Then
If Not Form_SheetCounts.DateTime Then
Else: Form_SheetCounts.DateTime = Now()
End If
End If
End Sub

And as I was composing this post I realized, thanks to you, that I had not changed the code to reflect which form it was in! Yep!

FormB was SearchSheetCounts with identical code Like this:

Private Sub Employee_Exit(Cancel As Integer)
If Not Form_SheetCounts.Employee Then
If Not Form_SheetCounts.DateTime Then
Else: Form_SheetCounts.DateTime = Now()
End If
End If
End Sub

but should have been:

Private Sub Employee_Exit(Cancel As Integer)
If Not Form_SearchSheetCounts.Employee Then
If Not Form_SearchSheetCounts.DateTime Then
Else: Form_SearchSheetCounts.DateTime = Now()
End If
End If
End Sub

So I was referencing the wrong form. Not sure why that caused a loop but...

I fixed the above and BINGO.

Thanks for helping me find my silly mistake! thumbup.gif
Richard
Go to the top of the page
 
moke123
post Sep 14 2017, 07:58 AM
Post#7



Posts: 1,091
Joined: 26-December 12
From: Western Ma.,L.I.,N.Y.,Jupiter,Fl.



what exactly is Form_SheetCounts? is it a form?
is the actual form name in the navpane Form_SheetCounts or SheetCounts?

if its SheetCounts then you are referencing the form by its module name which may have unintended results.

see the attached example. note the forms count.
if you run the public sub in form1 without the form being open, it will open a hidden instance of the form and run the sub. this hidden instance will persist. If you then open the Form1 with docmd.openform method there
will be two instances of the form open. You can see this when you have to click the close button twice to close both instances.
Attached File  FormModuleTest.zip ( 22.6K )Number of downloads: 1

Go to the top of the page
 
RickKat81
post Sep 14 2017, 08:04 AM
Post#8



Posts: 5
Joined: 13-September 17



Thank You.
Yes, Form_SheetCounts is a form.
SheetCounts.
Go to the top of the page
 
theDBguy
post Sep 14 2017, 09:46 AM
Post#9


Access Wiki and Forums Moderator
Posts: 70,669
Joined: 19-June 07
From: SunnySandyEggo


Hi,

Glad to hear you got it sorted out. I was thinking along the same lines about modifying the code from FormB because it seemed to be calling FormA instead.

Good luck with your project.

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    23rd September 2017 - 11:37 AM