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!

Clear Sub-Form

Status
Not open for further replies.

Quan9r

Technical User
May 18, 2000
36
US
I have looked through various FAQ in search of a method of clearing 5 fields in a sub-form. I would like to push a button then clear those fields. They are simple fields [Name, Date, a yes/no box, and 2 combo boxes].
Is this too difficult? I do not have much VB experience.
Any help would be appreciated
 
There are a number of ways. You can use the tag property for each of the fields in question, for example enter a T in the tag property of the fileds you want to be able to clear.

Then use the code below in the click event of the button:

Dim ctl As Control

For Each ctl In yoursubformname.controls
If ctl.Tag = "T" Then ctl.Value = Null
Next

Alternatively you could do this:

with subformname
.txtname=null
.txtdate=null
.combo1=null
.combo2=null
.checkbox=null
end with

Hope this helps.

Mark..

[worm]


 
Thanks Mark, Does this look right? I get numerous error messages

Private Sub Command42_Click()
Dim ctl As Control

PRP_Status.Controls
If [Date].Tag = "T" Then [Date].Value = Null
If [Arm].Tag - "T" Then [Arm].Value = Null
Next
End Sub

 
No.. you are doing it wrong!
Set the tag property "T" to those controls that you need to clear then use the code. You don't need to name any control as it checks the tag property of each control.
Code:
Private Sub cmdClearFileds_Click()
    Dim ctl As Control
    For Each ctl In Me.SubFormName.Controls
        If ctl.Tag = "T" Then ctl.Value = Null
    Next
End Sub

________________________________________________________________________
Zameer Abdulla
Visit Me
A person who misses a chance and the monkey who misses its branch can't be saved.
 
I will try and be more precise. View the form in design mode and select one of the controls (e.g. Text Box, Combo Box) that you want to clear. Then view the properies for this control, you will then need to find the property named 'Tag'. Against this property enter a 'T'. Repeat this for all the controls you want to clear on the form. (All you can do a multiple select and do this in one go -whatever you prefer)

This Tag is purely used to indicate which controls you want to clear when the button is clicked.

Next you need to enter some code in the 'On Click' event of the 'Clear' Button, if the button is on the same form use the code below:
Code:
Private Sub buttonName_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.Tag = "T" Then ctl.Value = Null
    Next
End Sub
If on a different form replace the Me. with the reference to the form e.g. Form_formName.Controls

HTH

Mark...

[worm]
 
Thank you! Mark....It worked. I appreciate your help and patience.

I do have another question...on one of those combo boxes I want the value to change...from "SUSPENDED"...to "ACTIVE". I would like to change it with the same button push as the clear fields.

I appreciate you help...it is obvious that I need to take a VB course.

thanks again
 
I solved it ...I did the following....I like that Tag ...very versitile

rivate Sub cmdClearFields_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "T" Then ctl.Value = Null
If ctl.Tag = "R" Then ctl.Value = "ACTIVE"
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top