Code to refer to the caption of a label bound to a control

K

Kurt

I use controls’ tag property and BeforeUpdate code to validate missing data
on a form. How can I edit the MsgBox below to show the caption of the label
that’s bound to the control, instead the name of the control (ctl.Name)
that’s currently used? Thanks. - Kurt

###

Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim ctl As Control

For Each ctl In Me

If ctl.Tag = "*" Then
If IsNull(ctl) Or ctl = "" Then
MsgBox "You must complete the required field '" & " " &
ctl.Name & " " & "' before you can continue.", vbCritical, "Required Field"
ctl.SetFocus
Cancel = True
Exit Sub
End If
End If
Next
Set ctl = Nothing

End Sub
 
D

Dirk Goldgar

Kurt said:
I use controls' tag property and BeforeUpdate code to validate
missing data on a form. How can I edit the MsgBox below to show the
caption of the label that's bound to the control, instead the name of
the control (ctl.Name) that's currently used? Thanks. - Kurt

###

Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim ctl As Control

For Each ctl In Me

If ctl.Tag = "*" Then
If IsNull(ctl) Or ctl = "" Then
MsgBox "You must complete the required field '" & " "
& ctl.Name & " " & "' before you can continue.", vbCritical,
"Required Field" ctl.SetFocus
Cancel = True
Exit Sub
End If
End If
Next
Set ctl = Nothing

End Sub

Dim strFieldName As String

' ...

If ctl.Controls.Count > 0 Then
strFieldName = ctl.Controls(0).Caption
Else
strFieldName = ctl.Name
End If

MsgBox "You must complete the required field '" & _
& strFieldName & "' before you can continue."
 
K

Kurt

Works great. Thanks.

Dirk Goldgar said:
Dim strFieldName As String

' ...

If ctl.Controls.Count > 0 Then
strFieldName = ctl.Controls(0).Caption
Else
strFieldName = ctl.Name
End If

MsgBox "You must complete the required field '" & _
& strFieldName & "' before you can continue."

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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

Top