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:
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_BeforeUpda te. 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:
So essentially, how do I populate control indexes into a listbox instead of control names? Please let me know what to change
into.
Thanks in advance.
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:
Code:
frm.Controls(15)
Code:
'---FORM CODE---
Private Sub Form_Load() 'Place in each form with a listbox of control sources
Call populateControlList(Me, Me!lstControlSources)
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not fillLastUpdated(Me, Me!txtItemKey, Me!lstControlSources, Me!cbbProcured.Value = True) Then
Cancel = True
Me.Undo
End If
End Sub
'---GLOBAL MODULE CODE---
Sub populateControlList(ByRef frm As Form, ByRef listCtl As Control)
Dim ctl As Control
listCtl.RowSource = ""
For Each ctl In frm.Controls
If LenB(ctl.Tag) > 0 Then
Call listCtl.addItem(ctl.Name)
End If
Next ctl
End Sub
Function fillLastUpdated(ByRef frm As Form, ByRef uniqueCtl As TextBox, ByRef listCtl As Control, Optional undoChanges As Boolean = False) As Boolean
If frm.Dirty And Not frm.NewRecord Then
Dim rsExist As Recordset
Dim ctl As Control
Set rsExist = CurrentDb.OpenRecordset(frm.RecordSource, dbReadOnly)
...
For i = 0 To listCtl.ListCount - 1
...
Set ctl = frm.Controls(listCtl(i))
If rsExist.Fields(ctrlSource) = ctl.Value Then
...Do Stuff
End If
...
Next i
...
Else
fillLastUpdated = True
End If
...
End Function
Code:
Call listCtl.addItem(ctl.Name)
Thanks in advance.
Comment