Check to see if a box exists on a form.

PuddinPie

Registered User.
Local time
Today, 11:29
Joined
Sep 15, 2010
Messages
149
Hello,


I was trying to figure out hoe to have a global module run and check the form that is open to see if a box existsed or not. I know how to do it for finding a table or query but not for something on a form itself.
Does anyone know how this would work for the VBA coding?

Thank's.
 
A function like this may do what you need.
Code:
Public Function checkIfBoxExists(ByRef pForm As Form) As Boolean
Dim ctl As Control
Rem set default return value
checkIfBoxExists = False
Rem step through all controls on the form
For Each ctl In pForm
  If ctl.Name = "xxx" Then ' 'xxx' = name of control you want to find
    checkIfBoxExists = True
    Exit Function
  End If
Next
End Function
You would call the function from somewhere with the target form name.
Code:
If checkIfBoxExists(me.Name) then <do something>
My example is not meaningful as you would not call the function from the form where you want to check the presence of the box (i.e. Me.Name), but it illustrates how the call is made.
What happens if more than one form is open? Do you want to check all open forms? There's code to do that as well!:)
If you want to make the code even more generic, you can pass the name of the box as a second parameter (pBoxName As String) and test that in the function instead of the literal name ("xxx" in my example).
Does it matter what type of box you're looking for (e.g. TextBox, ListBox, CheckBox ...)?
 
... did you spot my deliberate mistake? (I didn't!). :o The function call in my example should be
Code:
If checkIfBoxExists(Me) then <do something>
because the parameter passed is the form object and not its name.
 

Users who are viewing this thread

Back
Top Bottom