Clearing form contents using ForEach Object

Talismanic

Registered User.
Local time
Today, 18:30
Joined
May 25, 2000
Messages
377
Isn't there a way to clear all the objects in a form using something like this:

For each object in the form
clear contents.

I haven't been able to find anything in Access help but I have seen this done with other scripting languages so I wonder if it is possible with Access.
 
Here's two that someone gave me...no warranties expressed or implied here:

Function BlankFormControls(f As Form)
'
' Copies Null to all fields on a form - ignores errors
'
Dim i As Integer, C As Control
On Error Resume Next
For i = 0 To f.Count - 1
f(i) = Null
Next i
End Function

Function BlankXXControls(f As Form)
'
' Copies Null to all fields on a form
' Assumes form control names to be blanked are prefixed by 'XX'
'
Dim i As Integer, C As Control
For i = 0 To f.Count - 1
Set C = f(i)
If Left$(C.Name, 2) = "XX" Then
C = Null
End If
Next i
End Function
 
I must be missing something because I could not get either of those to work. I did find this in Access help but I am not sure how to make it work in my case:

Dim Found, MyObject, MyCollection
Found = False ' Initialize variable.
For Each MyObject In MyCollection ' Iterate through each element.
If MyObject.Text = "Hello" Then ' If Text equals "Hello".
Found = True ' Set Found to True.
Exit For ' Exit loop.
End If
Next

Any ideas for getting either one of these to work?
 
if i want to clear the contents of all the controls in a form or any other objects that can hold control (tab pages, option group, etc.), i do this routine:

Dim ctl as Control
On Error Resume Next
For each ctl in Me.Controls
ctl.Value = Null
ctl.Caption = vbNullString
Next

i use on error resume next to ignore any errors when i happen to access the wrong property of any of the controls (eg. labels have no VALUE property) but if you want to clear only textbox or combo box, which are the usual entry controls it is better to use this.

Dim ctl as Control
For each ctl in Me.Controls
if TypeOf ctl is TextBox or TypeOf ctl is ComboBox Then ctl.Value = Null
Next
 

Users who are viewing this thread

Back
Top Bottom