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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if control exists 1

Status
Not open for further replies.

Page410

Technical User
Mar 9, 2001
106
US
Here's a quick little function that will check if a control exists on a form or subform and return a boolean value.

I've used it to check to see if recordset field names are used as form control names, updating the recordset with the form value when I have a match (let me know if anyone is interested in that).

Here are the calls
No sub:
ControlExists(control.Name, form.Name)

With sub:
ControlExists(control.Name, subform.Name, True, subform.Parent.Name)

Code:
Function ControlExists(ControlName As String, FormName As String, _
Optional blnSubform As Boolean, Optional strParentform As String) As Boolean
Dim strTest As String
On Error Resume Next

Select Case blnSubform
    Case True
        strTest = Forms(strParentform)(FormName).Form(ControlName).Name
        ControlExists = (Err.Number = 0)
    Case Else
        strTest = Forms(FormName)(ControlName).Name
        ControlExists = (Err.Number = 0)
End Select
End Function
 
Looks good, but I'd pass a reference to the form, rather than the form name:
Code:
Function ControlExists(ControlName As String, FormCheck As Form) As Boolean
Dim strTest As String
On Error Resume Next
        strTest = FormCheck(ControlName).Name
        ControlExists = (Err.Number = 0)
End Function

Code:
ControlExists("ctlBob", Me)

Code:
ControlExists("ctlBob", Me.subFormBob.Form)




----------------------------------------------
Ben O'Hara "Where are all the stupid people from...
...And how'd they get so dumb?"
rockband.gif
NoFX-The Decline
----------------------------------------------
Want to get great answers to your Tek-Tips questions? Have a look at F
 
For my information, what would the advantage of passing the reference be?
 
The advantage of ByRef is you're passing a pointer to the actual form so you can check its control collection directly, which by the way guarantees that the form exists. Yours does return false if the control doesn't exist, but it also returns false if the form doesn't exist.

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
Also, many developers frown on deliberately triggering errors when there are other methods to get the job done. In this case, it is a simple matter to loop through a very small collection to find the control (how many forms have hundreds of controls?):
Code:
Function ControlExists(ByRef ctlName As String, ByRef frm As Form) As Boolean
On Error GoTo ErrHandler
  Dim ctl As Control
  
  For Each ctl In frm.Controls
    If ctl.Name = ctlName Then
      ControlExists = True
      Exit For
    End If
  Next ctl
  
ExitHere:
  Exit Function
ErrHandler:
  Debug.Print Err, Err.Description
  Resume ExitHere
End Function

I'm guessing the time it takes my box (P4 1.7Ghz) to execute this function on any form would be almost unmeasurable - looping is what computers do well, especially for collections that inlcude the IUnknown iterator (For-Each).

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
VbSlammer said:
The advantage of ByRef is you're passing a pointer to the actual form.............
I like to thank you for that beautiful explantion.
BTW, What could be a situation where we need to check if a control exists or not? I can't find any reason.
Thank you


Zameer Abdulla
Visit Me (New Look & style)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top