Referring to a label

aziz rasul

Active member
Local time
Today, 11:47
Joined
Jun 26, 2000
Messages
1,935
If I have a label on a form, how do I check, using VBA, that the label exists before carrying out an action?
 
Are you creating the label at run time?
 
No. I wouldn't know how to do that anyway.
 
Why would you need to check if a label exists on a form? Are you performing this from a public function to a similarly named label over a number of forms.

The easiest way I would try (were this the case) would be to create a quick function. Something like this:

Code:
Public Function IsLabel(frm As Form, lbl As Label) As Boolean

    On Error Goto Err_IsLabel

    Dim strTemp As String

    strTemp = Forms(frm.Name).Controls(lbl.Name).Caption

    IsLabel = True

Exit_IsLabel:
    Exit Function

Err_IsLabel:
    IsLabel = False
    Resume Exit_IsLabel

End Function
 
Last edited:
Easy, just use a little function like this.

Code:
Function ChKControl(ControlName As String, Optional ControlType As String = "") As Boolean
Dim ChkTest As Control
On Local Error GoTo ErrHdl
Set ChkTest = Form_Form2.Controls(ControlName)

ChKControl = True
If ControlType <> "" Then
    Select Case ControlType
        Case "TextBox"
            If ChkTest.ControlType <> acTextBox Then
                ChKControl = False
            End If
        Case "Label"
            If ChkTest.ControlType <> acLabel Then
                ChKControl = False
            End If
        Case "Combo"
            If ChkTest.ControlType <> acComboBox Then
                ChKControl = False
            End If
        Case "List"
            If ChkTest.ControlType <> acListBox Then
                ChKControl = False
            End If
    End Select
End If
'control exists

On Local Error GoTo 0
Exit Function
ErrHdl:
'control doesnt exist
ChKControl = False
End Function

There are 2 ways you can use this,

1) just check a control exists,

2) check the type of control, this is optional.

have fun


:cool:ShadeZ:cool:
 
Mile-O-Phile said:
Why would you need to check if a label exists on a form? Are you performing this from a public function to a similarly named label over a number of forms.

The easiest way I would try (were this the case) would be to create a quick function. Something like this:

Public Function IsLabel(frm As Form, lbl As Label) As Boolean

On Error Goto Err_IsLabel

Dim strTemp As String

strTemp = Forms(frm.Name).Controls(lbl.Name).Caption

IsLabel = True

Exit_IsLabel:
Exit Function

Err_IsLabel:
IsLabel = False
Resume Exit_IsLabel

End Function[/code]

Grrr Beaten by you again,



[edit]
hmmm do you always leave your error handeling on?
[/edit]
 
Last edited:
Shadez said:
hmmm do you always leave your error handeling on?

The error handling there is to capture the Fact that an error will be generated if the control referenced does not exist. On that error the code jumps down and states that the label does not exist (FALSE) otherwise the code is quite happy to concede that the label does exist (TRUE).
 
The reason for using a label was that if you move the mouse cursor over the label the mouse cursor doesn't change as is the case with an unbound text box. Does that make sense?

I tried your code Mile-O-Phile. However, if I delete the label and run your routine, I get a run-time error 2465 i.e. 'Application defined or object defined error' before it get's to your code.

This is what I used behind the command button to check the availability of the label before proceeding to the next bit of code.

LabelCheckOutput = IsLabel(Forms!frmMain, Forms!frmMain.Output)

If the label is deleted by someone (I know that is unlikely but it's crucial for what I'm doing) how do I recreate the same label and in the same position again?
 
Last edited:
If the form isn't open you may have problems as my code won't explicitly generate an error message.



Maybe the function combined with this:

Code:
Public Function IsFormOpen(strFormName As String) As Boolean

    IsFormOpen = (SysCmd(acSysCmdGetObjectState, acForm, strFormName) = acObjStateOpen)
       
End Function


i.e.


Code:
If IsFormOpen("NameOfForm") Then
    If IsLabel(Form, Label) Then
        ' okay to perform
    Else
        ' label is not available
    End If
Else
    ' form is not open to perform label check
End If


As for users deleting a label - are you really letting people into the design aspects of your database's forms?
 
The form was open when I tried your code.

As for access to the design view of the form, I'm not very ofay with security. Hence the reason for this post.

If you can make a suggestion that will resolve this, that would be equally good, providing I can get into the design myself.
 
Mile-O-Phile said:
The easiest way I would try (were this the case) would be to create a quick function. Something like this

I knew my function wasn't perfect - I just wrote it off the top of my head. Rather than pass a label parameter I'm sure a string with the expected label's name is what should be done.
 
I know what you r saying. How would I write code that counted the number of labels on the form so that I could then check whether any of their captions met my string criteria.
 
Code:
Dim cntr As Control

For Each cntr In Me.Controls
    If cntr.ControlType = acLabel Then
        Debug.Print cntr.Name
    End If
Next cntr
 
Thanks for that Travis. How would I obtain the caption string?

I tried cntr.Name.Caption, but it didn't work.
 

Users who are viewing this thread

Back
Top Bottom