Looping thru controls in a Form

  • Thread starter Thread starter Farook
  • Start date Start date
F

Farook

Dear Group,

I'm trying to clear the text box in a form after doing some operations
since I have quite a lot of text boxes in my form I tried to loop thru
the controls to clear the text boxes. Unfortunately I'm getting an
error which I'm unable to solve, it says "You can't reference a
property or method for a control unless the control has te focus".

Appreciate if anybody could help me in this regard.


Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
ctl.Text = ""
End If
Next

Thanks in Advance

Farook
 
Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
ctl.SetFocus
ctl.Text = ""
End If
Next
 
Farook,

Just change ctl.Text = "" to ctl = "". Reference to a control without
specifying a property addresses its value by default (which is what you want
here). The .Text property, on the other hand, can be referenced on a control
only when it has the focus, thus your error message.

HTH,
Nikos
 
Guys, Thanks a lot for the immediate reply, I tried both the
possisbilites but still I'm not able to get the results.

Firstly I tried by setting the focus to the control. (ctl.setfocus) I
got an error saying "OrderForm can't move the control to OrderNumber".

Later I tried Nikos methods without mentioning the text property like
ctl = "" (without setting the focus). This time the error is like
"Field 'OrderForm.OrderNumber' cannot be Zero Length" as the field
Ordernumber is the primary key.

I guess there should be some way to get rid of this error.

Please advice.

Thanks in Advance,

Farook
 
Farook, Nikos' reply was correct. It showed you how to do what you
originally asked for. That was, how to clear all textboxes on the current
form.

But if some of those textboxes are bound to mandatory fields, then clearly,
you will not be able to clear them by setting their values to empty. I bet
that you are >not< clearing those particular textboxes with your current
manual code! So you must omit those controls when you run the general
purpose, looping code. You could do that by checking their names or
controlsources, for example:

if ctl.controltype = actextbox then
select case ctl.name
case "txtThis", "txtThat", "txtTheOther"
' not those!
case else
' these.
ctl = "" ' or = Null
end select
endif

HTH,
TC
 
Guys, thank you so murch for all the responses. I agree with what TC
and Nikos says on clearing the text. I shouldn't have included the
primary field in it, but i have no choice.

what I did was , i created a new record using docmd.goto acNewRec and
then I locked the fields.

Public Sub LockFields(bDoLock As Boolean)
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Or (ctl.ControlType =
acComboBox) Then
If ctl.Tag = "1" Then
If bDoLock = True Then
ctl.Locked = True
ctl.BackColor = vbWhite
Else
ctl.Locked = False
ctl.BackColor = vbYellow

End If
End If
End If
Next


End Sub

This seems to solve my problem.

Once again thanks guys for your valuable time.

Farook
 
Back
Top