Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through text fields by index not name 1

Status
Not open for further replies.

cmmrfrds

Programmer
Feb 13, 2000
4,690
US
My query has pairs of fields form 2 tables that I want to compare.<br>I do a recordset clone in my Open event for the Form, then iterate through the fields checking for a change in pairs of fields.<br><br>Dim comp1 as varient, comp2 as varient<br>For indx = 0 to 16 <br>&nbsp;comp1 = RSMT.Fields(indx).Value<br>&nbsp;comp2 = RSMT.Fields(indx + 17).Value<br>etc.........<br><br>I want to set the corresponding field on the Form to a color when the pairs are not equal. <br>No problem in checking the pairs, but how do I set the field on the Form. I know the relative location of the fields through the index, but need the syntax. What is the way to iterate through fields on the Form ie. the&nbsp;&nbsp;Me.
 
I can use this syntax to set the column but it stays set for all the rows. <br>ctl as controls<br>set ctl = me.Controls<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx + 4).BackColor = 4227200<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx + 20).BackColor = 16711680<br><br>Is it possible to set the color attribute on a row by row basis or does the attribute setting hold for all rows?<br>
 
Question 1) <br>Could you give us an example of the field names both in your table, and on your form, and then how you are comparing them. This will help for a sufficient response.<br><br>Question 2)<br>You can set the color row by row if you use some decision structures inside your loop:<br><br>For example:<br><br>==========<br>For indx = 0 to 16 <br>&nbsp;&nbsp;&nbsp;If ThisIsTrue Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx).BackColor = 4227200<br>&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx).BackColor = 16711680<br>&nbsp;&nbsp;&nbsp;End If<br>Next indx<br>==========<br><br><b>OR</b><br><br>==========<br>For indx = 0 to 16 <br>&nbsp;&nbsp;&nbsp;Select Case ????<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case ???<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx).BackColor = 4227200<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case ???<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx).BackColor = vbRed<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case ???<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx).BackColor = vbWhite<br>&nbsp;&nbsp;&nbsp;End Select<br>Next indx<br>==========<br><br><br><br> <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Here is my code, which is a little lengthy. I have a different index setting for the form than the recordset since there are additional labels on the form. If I don't reset the color each time then once it is set it stays set for all the rows. If I reset the color then the reset color stays for all the rows. So, I was going to try a different twist and add a variable to my recordset which I would add as the first field in the row and flag if any columns had differnces but it won't let me update the variable.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Dim mKey As Integer, RSMT As DAO.Recordset<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim indx As Integer, indx2 As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim comp1 As Variant, comp2 As Variant<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim ctl As Controls<br>&nbsp;&nbsp;&nbsp;&nbsp;indx2 = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;Set RSMT = Me.RecordsetClone<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;RSMT.MoveFirst<br>&nbsp;&nbsp;&nbsp;&nbsp;Set ctl = Me.Controls&nbsp;&nbsp;&nbsp;' 3 label fields so starts at 3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;For indx2 = 0 To (RSMT.RecordCount - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For indx = 0 To 16&nbsp;&nbsp;&nbsp;'Match first 17 to second 17 fields<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx + 4).BackColor = -2147483643&nbsp;&nbsp;'---- initialize colors for the row<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx + 21).BackColor = -2147483643<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comp1 = RSMT.Fields(indx).Value<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comp2 = RSMT.Fields(indx + 17).Value<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If (Nz(comp1, 0)) &lt;&gt; (Nz(comp2, 0)) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' -- Value is different then set color ---------<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'''' Me.Bookmark = RSMT.Bookmark<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx + 4).BackColor = 4227200<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctl.Item(indx + 20).BackColor = 16711680<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next '-- loop thru the fields<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RSMT.MoveNext<br>&nbsp;&nbsp;&nbsp;&nbsp;Next '-- loop thru records<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;RSMT.Close<br>
 
Before I even look at the code, <br><br>Is this a continuous or datasheet form? If it is, you will <b><font color=red>not</font></b> be able to change properties for individual rows. If you change any property (backcolor, Bold, forecolor, etc.) it changes for EVERY row. This is the case whether you do it in design view, or programmatically. Let's say a form opens with a certain field backcolor of white. You then change the backcolor to red in code. That field's backcolor changes to red for <b>ALL</b> rows. If you changed it back to white, <b>ALL</b> rows would change back to white.<br><br>If this is not the case, then we can look at what you are trying to do. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
i think you can change the properties of individual rows (on continuous form OR subform) by Conditional Format available in Access 2000 ... <p>Mohamed Aly<br><a href=mailto:samara_79@hotmail.com>samara_79@hotmail.com</a><br><a href= > </a><br>
 
It is continuous and I discovered as you pointed out that the property could not be changed on a row by row basis. I simply wanted to highlight the differences when retreiving the data. So, for example, if 20 records changed out of 3000 then they would be easy to spot when scrolling through the output and since there are quite a number of fields to compare, I didn't want to do the compare in the query or referencing each unique name. I solved how to easily detect the differences in a code loop but I am not sure how to highlight these differences and display on the continuous form. <br>I tried another approach of putting a calculated field in my query which was initialized to blank and then filling it with a flag in the code loop, to finally display this field in front of the row. Access won't let me do this since the field is not updateable. Maybe there is no easy way to do this in Access. <br><br>Thank you,<br>Jerry
 
Samara, I am using access 2000. Can you elaborate a little? I am not sure what you mean by&nbsp;&nbsp;Conditional Format. Have you done something similar?<br><br>Thank you,<br>Jerry
 
Ok, conditional format available in Access 2000 for text boxes and combo boxes, and in every view of form (single, continuous and datasheet).<br>Anyway. select the control you want to change its properties then from the format menu choose &quot;Conditional Formatting&quot;, then you'll be brought up with a dialog box to choose your conditional statements and the colors you wanna change.<br>If you'd like rather to do it by VBA, you could use the new property of (text box and combo box)which called &quot;FormatCondition&quot;. i didnt try this before but i read about it and i know it works man, so go ahead.<br><br>Hope this would help YOU... <p>Mohamed Aly<br><a href=mailto:samara_79@hotmail.com>samara_79@hotmail.com</a><br><a href= > </a><br>
 
I have never used what Samara has talked about with A2K (I tend to stay away from 2000 unless it is required). So if it works, great, if it does not work, then let us know, there is another way, but try Samara's way first. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Thank you both. I will try it tonight since I have to go now.<br><br>Jerry
 
I am happy to say that it worked perfectly! There were a few false starts until I got the&nbsp;&nbsp;syntax down. Nulls caused me some consternation for a while, but it now works with and without nulls. <br><br>The syntax is: Nz([Table852ZANew_QC],0)&nbsp;&nbsp;'this is an example of one field<br><br>I used &quot;field value&quot;&nbsp;&nbsp;and &quot;Not Equal&quot; to Nz([Table852ZANew_QC],0)<br><br>Now it is very easy to see what fields have changed on the form.<br><br>Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top