Test for existance of control on form

M

mcnews

how to test to see if a particular control such as a command button
exists on a form.

tia,
mcnewsxp
 
K

Klatuu

Put these functions in a standard module so you can call them from anywhere.
The firrst function checks to see if the form is loaded. If it is not, it
opens it hidden in design mode and looks for the control name. If it finds
the control, it returns the control type as a string. If in can't find the
form, it returns "Form Not Found". If it can't find the control, it returns
"Control Not Found". The second function is used by the first to convert the
numeric control type to a string.

Public Function FindCtl(strFormName As String, strCtlName As String) As String
Dim ctls As Controls
Dim ctl As Control
Dim frm As Form
Dim blnCloseTheForm As Boolean

On Error GoTo FindCtl_Error

'Set the defaults
FindCtl = "Control Not Found"
blnCloseTheForm = False

'First see if the form is loaded
'Open it if it is not
If Not Application.CurrentProject.AllForms(strFormName).IsLoaded Then
DoCmd.OpenForm strFormName, acDesign, , , , acHidden
blnCloseTheForm = True
End If

Set frm = Forms(strFormName)
Set ctls = frm.Controls
For Each ctl In ctls
If ctl.Name = strCtlName Then
FindCtl = ControlTypeName(ctl.ControlType)
Exit For
End If
Next

FindCtl_Exit:

On Error Resume Next

If blnCloseTheForm Then
DoCmd.Close acForm, strFormName
End If

Exit Function

FindCtl_Error:

'Err 2467 means the form does not exist
If Err.Number = 2467 Then
FindCtl = "Form Not Found"
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure FindCtl of Module modUtilities"
End If
GoTo FindCtl_Exit
End Function
---------------------------------------------------------
Function ControlTypeName(n As Long) As String
'Purpose: Return the name of the ControlType.
'Note: The ControlType returns a Byte, but the constants are Long.
Dim strReturn As String

Select Case n
Case acBoundObjectFrame: strReturn = "Bound Object Frame"
Case acCheckBox: strReturn = "Check Box"
Case acComboBox: strReturn = "Combo Box"
Case acCommandButton: strReturn = "Command Button"
Case acCustomControl: strReturn = "Custom Control"
Case acImage: strReturn = "Image"
Case acLabel: strReturn = "Label"
Case acLine: strReturn = "Line"
Case acListBox: strReturn = "List Box"
Case acObjectFrame: strReturn = "Object Frame"
Case acOptionButton: strReturn = "Object Button"
Case acOptionGroup: strReturn = "Option Group"
Case acPage: strReturn = "Page (of Tab)"
Case acPageBreak: strReturn = "Page Break"
Case acRectangle: strReturn = "Rectangle"
Case acSubform: strReturn = "Subform/Subrport"
Case acTabCtl: strReturn = "Tab Control"
Case acTextBox: strReturn = "Text Box"
Case acToggleButton: strReturn = "Toggle Button"
Case Else: strReturn = "Unknown: type" & n
End Select

ControlTypeName = strReturn
End Function
 
M

mcnewsxp

Put these functions in a standard module so you can call them from
anywhere.
The firrst function checks to see if the form is loaded. If it is not, it
opens it hidden in design mode and looks for the control name. If it
finds
the control, it returns the control type as a string. If in can't find
the
form, it returns "Form Not Found". If it can't find the control, it
returns
"Control Not Found". The second function is used by the first to convert
the
numeric control type to a string.

Public Function FindCtl(strFormName As String, strCtlName As String) As
String

wow!
that's great.
thanks.
 
Top