Enumerating through controls to extract values using tag property

T

tcb

I want to add up the values of several textboxes on a form using the
tag property. In this case, tag = "grade"

Is this possible? I know you can set properties of controls but can
you extract the values of those controls also?
 
D

Douglas J Steele

Dim ctlCurr As Control
Dim lngTotal As Long

For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "grade" Then
lngTotal = lngTotal + ctlCurr
End If
Next ctlCurr
 
T

tcb

Thanks, I like that better than my solution. Before your message was
posted I tried something similar but my code was failing due to null
values in the text boxes (took me a while to figure that out).
Meanwhile I tried something a bit different which took more typing than
yours.

Dim frmCurrentForm As Form
Set frmCurrentForm = Screen.ActiveForm

Dim ctl As Control
Dim strFieldName As String
Dim intAgeGroupValue As Long
Dim intTotalAgeGroupValue As Long

For Each ctl In frmCurrentForm.Controls

If ctl.Tag = "AgeGroup" Then

strFieldName = ctl.ControlSource
intAgeGroupValue = NTZ(frmCurrentForm.Recordset.Fields(strFieldName))
intTotalAgeGroupValue = intTotalAgeGroupValue + intAgeGroupValue

End If

Next ctl

If intTotalAgeGroupValue <> Me.TotalInds Then

MsgBox ("Total by Grade Does Not Match Total Individuals"), ,
"Totals Do Not Match"

End If
 
Top