473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

VBA endless loop when doing "For Each" against "Me.Controls"

cgiAlexis
Using VBA 6.5 with Access 2003 I'm trying to see if the user has edited a field and made it a value that is not the one recorded in the table.

I was planning on making the code nice and neat and having VBA change background colours so the user gets a visual reference of what they have changed. The code is also meant to change the background back if the user changes the value back to the original.

Currently I run a public sub that looks like this:
Expand|Select|Wrap|Line Numbers
  1. Sub AmendUpdate()
  2.  
  3. Dim ctlCollection As Variant
  4. Dim ctl As Control
  5. Dim ctlName As String
  6. Dim OldVal, CurrVal As Variant
  7. Dim lngRed As Long, lngGreen As Long
  8. lngRed = RGB(255, 0, 0)
  9. lngGreen = RGB(0, 255, 0)
  10.  
  11. Set ctlCollection = Me.Controls
  12. On Error GoTo BoxColour_err
  13.  
  14.     For Each ctl In Me.Detail.Controls
  15.         If ctl.ControlType = acTextBox Then 'Look at all text boxes
  16.             If ctl.Tag = "AmendCheck" And Left(ctl.ControlSource, 1) <> "=" Then  'Looks only for bound controls that are also tagged for check
  17.             ctlName = ctl.Name
  18.             OldVal = Nz(Me.Controls(ctlName).OldValue, "")  'Change Null to Zero length
  19.             CurrVal = Nz(Me.Controls(ctlName).Value, "")
  20.                 If CurrVal <> OldVal Then   'Check that it has changed from .OldValue
  21.                     ctl.BackColor = lngRed  'Make it red if it has
  22.                     GoTo BoxColour_nextCtl  'Go to the next control
  23.                 Else: ctl.BackColor = lngGreen    'Detecting the opposite is true for debug
  24.                     GoTo BoxColour_nextCtl  'Go to the next control
  25.  
  26.                 End If
  27.             End If
  28.         End If
  29.  
  30. BoxColour_nextCtl:   ' <--- THIS NEEDS FIXING
  31.     'MsgBox "I changed something!"  'Debug the loop problem
  32.     Next ctl
  33.  
  34. BoxColour_err:
  35.     'MsgBox "I broke!"  'Same as above but for failed loops
  36.     Resume BoxColour_nextCtl
  37.  
  38. BoxColour_end:
  39.     'Beep
  40.     Exit Sub
  41.  
  42. End Sub
  43.  
But it ends up looping endlessly while looking through all the text box controls that have the tag "AmendCheck". When I interrupt it we see that the tagged boxes are changed to green (For debug).
I've put my own notes in to these other people's code so if it appears I've misunderstood something then you can see it easily.

For now I guess the first hurdle is to make sure that For Each loop only asseses each of the controls in the set once before ending.

Resources from:
Social.msdn shows detailed use of the code
Easy to understand explaination of the required code
Bytes.com trying to get the .OldValue with BeforeUpdate
Tried to use this
Proof that I used a search engine
Some info I used to understand where to point this code

This is all in aid of the database I'm still developing from this post.

Regards and thanks in advance,
Alexis
Sep 12 '13 #1

✓ answered by Rabbit

There's no need for the GoTo lines. Also line 23 should be split up. Also, your exit sub needs to come before your error handling.

2 1769
Rabbit
12,516 Expert Mod 8TB
There's no need for the GoTo lines. Also line 23 should be split up. Also, your exit sub needs to come before your error handling.
Sep 12 '13 #2
Grade A derpness, thanks very much for your perserverance Rabbit.

If there is anything to learn from this it's that most of the time VBA errors are from very simple things like simply giving the code every possible way to run into an infinite loop as I did with my initial code.

Now I'm off to look into making the code run constantly without locking up VBA, seeing why it doesn't process date changes, blocking write changes to the table until the user is ready to commit all changes to a record.
Sep 13 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: codesmithsf | last post by:
I'm using PHP 4.2.2 and Apache 2.0.4.0. A script running on a virtual host had an endless loop in it that brought Apache to its knees. I have max_execution_time=90 and memory_limit=32M to...
3
by: John F. | last post by:
Hi all, I'm searching fulltime for days now, and I don't find the solution, so I'm thinking this is more a bug :( If i use following code in form&subforms:
24
by: Tweaxor | last post by:
This has been puzzling me all this week. This is actually a homework assignment from last semesmter. But the teacher wouldn't tell us why certain things didn't work, but it didn't just work. My...
13
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include...
0
by: Bill Borg | last post by:
Hello all, Not sure I can describe this adequately, but I am creating an endless loop when an authenticated user signs out. When the user signs out, I want to remove authentication, abandon...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
2
by: Dave Kelly | last post by:
This is a continuation of a previous thread:"Displaying text with include file." This is the PHP include file. I have about googled myself out on this one. If I have found what it is I seek ,...
1
by: cory6903 | last post by:
i am having a problem with an endless loop **int x; **a: **cout << "enter a #" << endl; **cin >> x **switch(x) **{ ** case 1:
6
by: uche | last post by:
This function that I have implemented gives me an infinite loop. I am trying to produce a hexdum program, however, this function is not functioning correctly.....Please help. void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.