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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For each......

Status
Not open for further replies.

primerov

Technical User
Aug 16, 2002
160
BG

Can somebody help me with the for each code? On leaving the form, i want to set all the values for all the controls in the form to be value = 0.
The purpose is to clear the from from the previous filters anc choices and make it ready for a new choice
 
Something like this?
Whenever you leave the form call this function as below

Code:
clear_form(me)
      
Function clear_form(f As Form)
    Dim c As Control
    Dim f As Form
    For Each c In f.Controls
        c.Value = 0
    Next
End Function
 
Hi!

One word of warning about the code above there are some types of controls that do not have a value property, such as labels, and will cause the this code to crash. One way around it is to check the control type like this:

clear_form(me)

Function clear_form(f As Form)
Dim c As Control
Dim f As Form
For Each c In f.Controls
If c.ControlType <> acLabel then
c.Value = 0
End If
Next
End Function

Or you can add an error check:

clear_form(me)

Function clear_form(f As Form)
Dim c As Control
Dim f As Form

On Error GoTo ErrCheck
For Each c In f.Controls
c.Value = 0
Next

Exit Sub
ErrCheck
If Err.Number = ? (you will need to find the correct number) Then
Continue
End If
End Function

hth


Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top