473,378 Members | 1,609 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,378 software developers and data experts.

How do I get the index of a control for reference?

Hello,

I'm trying to obtain a form control's index. For example, a form may have 30 or so controls on it. To refer to control 15, I can do:
Expand|Select|Wrap|Line Numbers
  1. frm.Controls(15)
  2.  
My goal is to populate a listbox with the indexes of all controls that have a tag, executing on Form_Load. Then, I wish to reference all controls in the listbox by index on Form_BeforeUpdate. I do this for change tracking in a form. Currently I refer to the controls by their name (a string), but it would be faster if I can refer to them by index (a number) as shown above. This is important as some of my slowest forms are long winded datasheets with about 50 controls and 100 labels! Here is a simplified version of what I currently have:
Expand|Select|Wrap|Line Numbers
  1.  
  2. '---FORM CODE---
  3.  
  4. Private Sub Form_Load()    'Place in each form with a listbox of control sources
  5.     Call populateControlList(Me, Me!lstControlSources)
  6. End Sub
  7.  
  8. Private Sub Form_BeforeUpdate(Cancel As Integer)
  9.     If Not fillLastUpdated(Me, Me!txtItemKey, Me!lstControlSources, Me!cbbProcured.Value = True) Then
  10.         Cancel = True
  11.         Me.Undo
  12.     End If
  13. End Sub
  14.  
  15. '---GLOBAL MODULE CODE---
  16.  
  17. Sub populateControlList(ByRef frm As Form, ByRef listCtl As Control)
  18.     Dim ctl As Control
  19.  
  20.     listCtl.RowSource = ""
  21.     For Each ctl In frm.Controls
  22.         If LenB(ctl.Tag) > 0 Then
  23.             Call listCtl.addItem(ctl.Name)
  24.         End If
  25.     Next ctl
  26. End Sub
  27.  
  28. Function fillLastUpdated(ByRef frm As Form, ByRef uniqueCtl As TextBox, ByRef listCtl As Control, Optional undoChanges As Boolean = False) As Boolean
  29.     If frm.Dirty And Not frm.NewRecord Then
  30.         Dim rsExist As Recordset
  31.         Dim ctl As Control
  32.  
  33.         Set rsExist = CurrentDb.OpenRecordset(frm.RecordSource, dbReadOnly)
  34.         ...
  35.         For i = 0 To listCtl.ListCount - 1
  36.             ...
  37.             Set ctl = frm.Controls(listCtl(i))
  38.             If rsExist.Fields(ctrlSource) = ctl.Value Then
  39.                 ...Do Stuff
  40.             End If
  41.             ...
  42.         Next i
  43.         ...
  44.     Else
  45.         fillLastUpdated = True
  46.     End If
  47.     ...
  48. End Function
  49.  
So essentially, how do I populate control indexes into a listbox instead of control names? Please let me know what to change
Expand|Select|Wrap|Line Numbers
  1. Call listCtl.addItem(ctl.Name)
  2.  
into.

Thanks in advance.
Nov 17 '10 #1

✓ answered by Stewart Ross

Hi. The form's controls collection, in common with all other collection objects, does not have a method which returns the index for a given object. In part this is because the position of an item within a collection may vary over time, depending on whether or not new objects are added to the collection or deleted from it.

That being said, I think you can modify your sub PopulateControlList to do what you want fairly easily, by substituting a counted-FOR loop instead of a FOR EACH loop, then adding the FOR loop index value to the list control in place of the control's name:

Expand|Select|Wrap|Line Numbers
  1. Sub populateControlList(ByRef frm As Form, ByRef listCtl As Control) 
  2.     Dim ctl As Control 
  3.     Dim intCtlCount as Integer
  4.     Dim intCurrentCtl as Integer
  5.     intCtlCount = frm.Controls.Count
  6.     listCtl.RowSource = "" 
  7.     For intCurrentCtl = 0 to intCtlCount-1 'zero-based count
  8.         set ctl = frm.controls(intCurrentCtl)
  9.         If LenB(ctl.Tag) > 0 Then 
  10.             Call listCtl.addItem(intCurrentCtl) 
  11.         End If 
  12.     Next intCurrentCtl 
  13. End Sub
Because the value stored in the listbox will be treated as text (not an integer number) you will probably need to modify line 37 to ensure you are passing an integer value as the index to the controls collection:

Expand|Select|Wrap|Line Numbers
  1. Set ctl = frm.Controls(CInt(listCtl(i)))
-Stewart

3 12438
Stewart Ross
2,545 Expert Mod 2GB
Hi. The form's controls collection, in common with all other collection objects, does not have a method which returns the index for a given object. In part this is because the position of an item within a collection may vary over time, depending on whether or not new objects are added to the collection or deleted from it.

That being said, I think you can modify your sub PopulateControlList to do what you want fairly easily, by substituting a counted-FOR loop instead of a FOR EACH loop, then adding the FOR loop index value to the list control in place of the control's name:

Expand|Select|Wrap|Line Numbers
  1. Sub populateControlList(ByRef frm As Form, ByRef listCtl As Control) 
  2.     Dim ctl As Control 
  3.     Dim intCtlCount as Integer
  4.     Dim intCurrentCtl as Integer
  5.     intCtlCount = frm.Controls.Count
  6.     listCtl.RowSource = "" 
  7.     For intCurrentCtl = 0 to intCtlCount-1 'zero-based count
  8.         set ctl = frm.controls(intCurrentCtl)
  9.         If LenB(ctl.Tag) > 0 Then 
  10.             Call listCtl.addItem(intCurrentCtl) 
  11.         End If 
  12.     Next intCurrentCtl 
  13. End Sub
Because the value stored in the listbox will be treated as text (not an integer number) you will probably need to modify line 37 to ensure you are passing an integer value as the index to the controls collection:

Expand|Select|Wrap|Line Numbers
  1. Set ctl = frm.Controls(CInt(listCtl(i)))
-Stewart
Nov 18 '10 #2
Hey, that is a good idea to use the For loop index to get the control index. I'm going to test it out and let you know how it works.

Thanks!
Nov 18 '10 #3
Awesome it works! I then call it from the listCtl with the following line of code:
Expand|Select|Wrap|Line Numbers
  1. frm.Controls(CInt(listCtl.ItemData(i))).Value
  2.  
Thanks a bunch, you're the man!
Nov 18 '10 #4

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

Similar topics

7
by: vindaloo1 | last post by:
I'm using Access 2000 and I have a main form and a subform. When a control on the subform is updated I am trying to update a control on the mainform. But instead of updating the control on the...
1
by: Frank | last post by:
I am trying to manipulate a textbox display value of subreport from main report's detail print() event. The goal is this, if there is no record from database, replace the blank space from textbox...
1
by: Brian | last post by:
I have created two forms Selector and Circuit. Selector is an unbound form where fields are populated and there is a command button which opens Circuit and displays information based on the input...
1
by: Sam | last post by:
Hi, I write my code behind in C# and everytime I need a new functionality, I build a class for it and save it in a folder I call ClassLibrary. My question is I don't know how to send my web...
1
by: Jon Stranger | last post by:
I am trying to build a custom web control based on DataGrid using VS.NET Standard Edition and have a problem referencing the Columns collection. The control has been built in a separate VS.NET...
2
by: Do | last post by:
Hi: I am using an MS Office Automation Active X control in my ASP.NET web application. I've added the reference and noticed that the .dll file gets copied into the \bin folder. All my code...
1
by: Jie | last post by:
Hi, All I am use Web User Control for State/Province drop down list. Public Class TEQContactCreate Inherits TEQBasePage Protected ddlProvinceFilter As ProvinceDropDownList Protected...
2
by: Mike L | last post by:
When the max length of a text box has been reached, I want the focus to go to the next control based on tab index. How do I code or what property do I set?
2
by: Alex Maghen | last post by:
I need to be able to get a reference to a Control on an ASPX Page by the exact ID that I gave that control at Design-Time. Let's say I created an ASPX that had a control: <asp:TextBox...
0
by: JasenC | last post by:
I've just upgraded from VS 2003 to VS 2005 and I've downloaded the latest service pack. However when I try to add a button to my form in design mode I keep getting this error. "A circular...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.