Reset the form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a search form with a search button and a close button. Each button has
an Event Procedure

I need to add a form reset button so that the form can be cleared of any of
the choices typed in or selected in the the combo is cleared. What is the
code to make this happen?
 
The basic idea is to loop through all controls on your form, and set the
Value to Null.

Of course, that applies only to controls that have a Value and are unbound.
There's an example below. Replace the call to the error logger with your own
handler, or use the one at:
http://allenbrowne.com/ser-23a.html

Note that toggle buttons, check boxes, and option buttons do have a
ControlSource and Value, but not if they are part of an option group.

-----------------------code starts--------------------------
Private Sub cmdReset_Click()
On Error GoTo Err_cmdReset_Click
'Purpose: Restore to the state when first loaded.
Dim ctl As Control

For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox 'Reset text boxes to blank.
If Not IsNull(ctl.Value) Then
If Left(ctl.ControlSource, 1) = "=" Then
'do nothing
Else
ctl.Value = Null
End If
End If

Case acComboBox, acOptionGroup, acCheckBox 'Reset to default
value.
If Nz(ctl.DefaultValue, "") <> vbNullString Then
ctl.Value = ctl.DefaultValue
ElseIf Not IsNull(ctl.Value) Then
ctl.Value = Null
End If

Case acListBox 'Unselect everything in
listboxes.
Call ClearList(ctl)

Case acLabel, acCommandButton, acOptionButton, acTabCtl, acPage,
acRectangle, acLine, acImage, acBoundObjectFrame, acSubform, acObjectFrame,
acPageBreak, acCustomControl
'Do nothing
Case Else
Debug.Print ctl.Name & " not handled"
End Select
Next

Exit_cmdReset_Click:
Exit Sub

Err_cmdReset_Click:
Call LogError(Err.Number, Err.Description, conMod & ".cmdReset_Click")
Resume Exit_cmdReset_Click
End Sub

Function ClearList(lst As ListBox) As Boolean
On Error GoTo Err_ClearList
'Purpose: Unselect all items in the listbox.
'Return: True if successful
Dim varItem As Variant

If lst.MultiSelect = 0 Then
lst = Null
Else
For Each varItem In lst.ItemsSelected
lst.Selected(varItem) = False
Next

End If

ClearList = True

Exit_ClearList:
Exit Function

Err_ClearList:
Call LogError(Err.Number, Err.Description, conMod & ".ClearList")
Resume Exit_ClearList
End Function
-----------------------code ends--------------------------
 
Ok. I guess I should said that I am not a programmer and I just wanted some
basic code for reseting the form. Thus, I am not sure about the error logger
info. you have referenced.
 
Regarding the error handler, you could replace this line:
Call LogError(Err.Number, Err.Description, conMod & ".ClearList")
with
MsgBox "Error " & Err.Number & ": " & Err.Description

The code was designed so that it works with any form, regardless of the
names of the controls. You could just do this kind of thing:
Me.[NameOfYourFirstTextboxHere] = Null
Me.[NameOfYourNextTextboxHere] = Null
and so on.
 
Back
Top