aziz rasul
Active member
- Local time
- Today, 06:31
- 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?
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
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
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]
Shadez said:hmmm do you always leave your error handeling on?
Public Function IsFormOpen(strFormName As String) As Boolean
IsFormOpen = (SysCmd(acSysCmdGetObjectState, acForm, strFormName) = acObjStateOpen)
End Function
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
Mile-O-Phile said:The easiest way I would try (were this the case) would be to create a quick function. Something like this