Determining Whether a Field is on the ActiveForm

  • Thread starter Thread starter L.A. Lawyer
  • Start date Start date
L

L.A. Lawyer

I am trying to do code that will only run if a field is present on the
screen.activeform and will be ignored if the field is not on the form. How
is this done?
 
Try something like:

For each ctl in Forms(ActiveForm).Controls
If ctl.Name = "TheOneIWasLookingFor" Then
'code to do what you want
Exit For
End If
Next

HTH,
Nikos
 
The problem with this, to my understanding, is that it is very time
consuming and will slow down the process. Although there ought to be a way
to locate the field directly because we know the name, this solution, which
I assume will be slow, should work though.
 
The problem with this, to my understanding, is that it is very time
consuming and will slow down the process.
You would have to have several hundred controls on your form before you
notice a delay.

Although there ought to be a way
to locate the field directly because we know the name...
There is. Try this:

Private Sub Whatever()
On Error GoTo Error_Handler
vCheck = Me.Controls("TheOneIWasLookingFor").Name
MsgBox "Control exists."
'Code to do what you want
Exit Sub

Error_Handler:
If Err = 2465 Then
MsgBox "Control does not exist."
Exit Sub
Else
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
End If
End Sub

I would suggest you try both and see if you notice any difference in
performance. How many controls on your form?
 
Back
Top