Clearing Form Values the easy Way???
When you're attacking problems like this (when you don't know how
many controls there are) a good approach is to look for a solution that is
flexible and re-usable.
Here's one approach that I came up with using the tag property of the form
controls.
For each control that you want to refresh enter a value of Y (for Yes) as
the tag
property. You would then loop through each control, see if the tag has a Y
and
clear it. You might want to go one step further though. Sometime when you
clear
the fields on a form, you want some fields to have a default value like
today's
date in a date field or a zero in a number field.
To do this, in the tag property of each control you want to reset enter a Y
and
a new default value if you want one. For example: Y|0 (I've used | as a
separator).
Now your code simply has to loop through each control, see if the first
character
is Y and apply the default value if one exists otherwise use an empty string.
Here's some example code. Note that I've also included a parameter of the
form you wish to clear which means you can use the same code for many forms.
The function also has a return value which will tell you how many fields were
cleared.
Function ClearForm(ByRef frm As UserForm) As Long
Dim ctl As Control
' variant to store the default value which could be a
' string, number or date value.
Dim varDefault As Variant
' Loop through the collection of controls of the
' passed form parameter
For Each ctl In frm.Controls
' Determine if we want this control cleared
If UCase$(Left$(ctl.Tag, 1)) = "Y" Then
' retrieve default value (if any). This will be anything
' after the first two characters of the tag property
varDefault = Right(ctl.Tag, Len(ctl.Tag) - 2)
' Assess the default value
Select Case varDefault
' if the tag property has the word today, enter the date
Case "TODAY"
ctl.Value = Date
' Have as many special alternatives as you need
'case whatever
' Otherwise settle for the default value in the tag property.
' If the default was blank, this will enter an empty string
Case Else
ctl.Value = varDefault
End Select
' increment counter so we know how many fields were cleared
ClearForm = ClearForm + 1
End If
Next ctl
End Function
Good luck.
OfficeHacker
" wrote:
Herro!! Okay, I have been clearing forms upon exiting them by
individual fields, and it is a pain in my ass. Example:
Sub ClearRWts()
RWts.Hide
RWts.textbox1.Clear
RWts.Caption.Clear
RWts.Pain.Clear
RWts.InMy.Clear
RWts.colon.Clear
RWts.textbox2.Clear
End Sub
So...let us say that the above code includes like 18 lines to clear an
entire form so that the next time it will be fresh and clean like a new
tissue from the kleenex box...Please tell me there is an easy way to do
this. I have tried the following, but they did not work:
RWts.Clear
RWts.Close
RWts.JustDeleteEverythingDammit
RWts.Values.Clear
Apparently, these are not allowed as they either only exist in my head,
or are not correct statements or syntax.
Oh dear stranger reading this, will you not help this poor soul who
must endure countless hours of useless line-by-line code entry?
|