Using .Caption instead of .Name in VBA

farmboy

New member
Local time
Today, 04:42
Joined
Jan 26, 2015
Messages
8
Hi Guys,

I'm trying to customise the error message for Input Mask Violations, so that it references the control where the error has occured, rather than just being a generic error message.

I can make this work using the code below:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const INPUTMASK_VIOLATION = 2279
If DataErr = INPUTMASK_VIOLATION Then
MsgBox "Please check that the information you have added in the " + Screen.ActiveControl.Name + " field is in the appropriate format."
Response = acDataErrContinue
End If
End Sub

BUT, all my field names are shortened e.g. Mobile Number = MobileNo, so I want to use the caption property instead. I assumed I could just change it to "Screen.ActiveControl.Caption". But this doesn't work at all!

I've checked elsewhere but google's telling me I'm doing the right thing!

Any help would be much appreciated,

Thanks
:)
 
Do not believe everything Google tells you. :)
Try the below, (not tested).
Code:
Screen.ActiveControl.Controls(0).Caption
 
I had come across that method, but it gives me an error message:

"Run-time error: '438'

Object doesn't support this property or method"

Which is the same one I get when I just use .Caption.

Thanks though!

EDIT!

I'd missed the "s" from "Controls"! It works now,

Thanks JHB!
 
Last edited:
JBH

I've learned something new. I never thought of a control having a control. Certainly helps in coding visibility of a text box and its label.
 
never thought of a control having a control
Note, only applies to controls that can have an associated label such as textbox, combo, subform, etc. You can't add one to a button for example
 

Users who are viewing this thread

Back
Top Bottom