Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a Label Name to a Public Function

Status
Not open for further replies.

Lightning

Technical User
Jun 24, 2000
1,140
AU
I have a db with many forms which each have a number of mandatory fields. I need a generic function to display an error message if the user either tabs through the field(s) without entering any data, or tries to close the form without entering the data. I am using the following code to call the MandatoryField function from a module. This works exactly as expected.

However, what I really need to display in the message box is the Caption of the field’s associated label. So I need to pass to the Function the field’s Labelname. All my attempts to do so have ended in abject failure.

I tried
Code:
Call MandatoryField(Me.ActiveControl & “Label”)
and get an “Object Required” error. I tried declaring a control variable and passing that, but got the error message “Object variable or With Block variable not set”.

Can anybody tell me how to amend this code to send the control’s labelname to the function? All the fields’ labels are named using the convention FieldNameLabel.

TIA
__________________________________________________________
Code:
Private Sub SupportNames_Exit(Cancel As Integer)
If IsNull(SupportNames) Then
        Call MandatoryField(Me.ActiveControl)
        Cancel = True
        Call ChangeColor
        LocalSupport.SetFocus
    End If
End Sub
___________________________________________________________

Code:
Public Function MandatoryField(ctl As Control)
Dim strField As Variant


    strField = MsgBox(ctl.Name & "@This field MUST have information entered into it.@" & _
            "Enter the required information or press the [ESCAPE] key Twice to delete " & _
            "all data on this screen and start again.", vbOKOnly + vbInformation, _
            "Required Information Missing!!")
End Function

This function really needs to be:

Code:
Public Function MandatoryField(ctl As Control)
Dim strField As Variant


    strField = MsgBox(ctl.Caption & "@This field MUST have information entered into it.@" & _
            "Enter the required information or press the [ESCAPE] key Twice to delete " & _
            "all data on this screen and start again.", vbOKOnly + vbInformation, _
            "Required Information Missing!!")
End Function
__________________________________________________________

Lightning
 
Hi,

try:
Call MandatoryField(Me.ActiveControl.Controls(0))

Cheers
 
sdk

Thank you. That was exactly what I needed!

Lightning
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top