loop thru controls

  • Thread starter Thread starter sam
  • Start date Start date
S

sam

Hi,
I have an unbound form. Once it is populated with data I
want to be able to click 'new' and clear all the
controls. Can anyone tell me how to do this. So far I
have the following but can't seem to get it to work....
that's if I'm even on the right track.

Private Sub cmdRecNew_Click()
Dim ctl As Control
For Each ctl In Me.Controls
*** can't get this right ****
Next ctl
End Sub

Not too sure about me.controls either

Any ideas?
 
The problem with doing this is that you need to be able to
specify which type of controls to clear - do you want to
change your labels or lines?

If you use the standard naming conventions, it becomes far
easier:

Select Case Mid((ctrl.Name),1,3)
Case "txt"
ctrl.Text = ""
Case "lbl"
ctrl.Caption = ""
Case "cbo"
ctrl.Text = ""
Case etc...
End Select

Hope this helps!

Howard Brody
 
You need to know what type of control it is. You
say "Clear" the controls, most controls can't be cleared.
If the controls in question are text boxes, the following
does this. For other controls, insert code as appropriate
in the select

Private Sub Command14_Click()
Dim ctlLoop As Control
For Each ctlLoop In Me.Controls
Select Case ctlLoop.Properties("ControlType")
Case acCheckBox
Case acComboBox
Case acCommandButton
Case acCustomControl
Case acImage
Case acLabel
Case acLine
Case acListBox
Case acObjectFrame
Case acOptionButton
Case acOptionGroup
Case acPage
Case acPageBreak
Case acRectangle
Case acSubform
Case acTabCtl
Case acTextBox
ctlLoop = vbNullString
Case acToggleButton
End Select
Next
End Sub
 
Back
Top