Enumerating through controls to extract values using tag property

  • Thread starter Thread starter tcb
  • Start date Start date
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?
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top