Caption in message

  • Thread starter MrH via AccessMonster.com
  • Start date
M

MrH via AccessMonster.com

I was looking at some code for validation.... like this....

For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Trim(ctl & "") = "" Then
msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbCritical + vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next

Now the problem is this displays the field name.... which to alot of users
might be confusing. For example...... The "First Name" field or "Last Name"
field on a form might display.... "txtFName" or txtLName" ........ If the
user sees this in a message box they might not really know what field is
required!
What I want to do is display the Caption.. somethin like "ctl.Caption" not
"ctl.name" but can't seem to find any syntax that works.
Anyone help?

Thanks
 
D

Douglas J. Steele

Each control has a Tag property that you can use however you want. Stick
whatever description you want in the Tag property, and then use

msg = "Data Required for '" & ctl.Tag & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
 
G

GH

Textbox controls do not have a Caption property. You can try using
the StatusBarText or ControlTipText properties to provide more user-
friendly descriptions of the textbox fields.

- GH
 
M

Marshall Barton

MrH said:
I was looking at some code for validation.... like this....

For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Trim(ctl & "") = "" Then
msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbCritical + vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next

Now the problem is this displays the field name.... which to alot of users
might be confusing. For example...... The "First Name" field or "Last Name"
field on a form might display.... "txtFName" or txtLName" ........ If the
user sees this in a message box they might not really know what field is
required!
What I want to do is display the Caption.. somethin like "ctl.Caption" not
"ctl.name" but can't seem to find any syntax that works.


Captions are in labels, not text boxes. **IF** the text
box has an attached label, then you can retrive the label's
caption using:

ctl.Controls(0).Caption
 
M

MrH via AccessMonster.com

Douglas said:
Each control has a Tag property that you can use however you want. Stick
whatever description you want in the Tag property, and then use

msg = "Data Required for '" & ctl.Tag & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _


That won't work......... The "Tag" is being used to identify which fields to
check.....
"If ctl.Tag = "*" And Trim(ctl & "") = "" Then"
So....... It would work if you changed the tag on every field and had
multiple IF...Then statements.
But..... Not what I want to do.......
Thats what has been frustrating trying to get an answer on this one! OR
simply why it wont work. The "caption" can be set in the table. THAT is what
I am really trying to reference.
 
D

Douglas J. Steele

MrH via AccessMonster.com said:
That won't work......... The "Tag" is being used to identify which fields
to
check.....
"If ctl.Tag = "*" And Trim(ctl & "") = "" Then"
So....... It would work if you changed the tag on every field and had
multiple IF...Then statements.
But..... Not what I want to do.......
Thats what has been frustrating trying to get an answer on this one! OR
simply why it wont work. The "caption" can be set in the table. THAT is
what
I am really trying to reference.

While it's a pain, you can store multiple pieces of information in the Tag
property.

Put, say, a semicolon after the asterisk, followed by the caption you wish
to use:

*;Caption

You'd then use:

If Left(ctl.Tag, 1) = "*" And Trim(ctl & "") = "" Then

and

msg = "Data Required for '" & Mid(ctl.Tag, 3) & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _

Heck, you don't even really need the semicolon in this case: just put an
asterisk in front of the caption (and use Mid(ctl.Tag, 2) in the assignment
statement instead)
 
M

marius

... The "caption" can be set in the table. THAT is what
I am really trying to reference.

You can try something like:
msg= ...... & Me.Recordset.Fields(ctr.ControlSource).Properties("Caption")
& ....

(if property isn't set you get an error, trap it)
 

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