Clear fields on forms (1 Viewer)

akika

Registered User.
Local time
Today, 11:53
Joined
Aug 7, 2018
Messages
102
hI,
I have a form which contain StudentId field with a search & clear btn.
how can i:
1. clear the form with all its fields (apprx.35 textbox, combo, date picker, listbox etc) when clear button is pressed

2. when i put the focus on StudentId to type the Id in the form header prior to click on clear btn..the fields e.g name , address, phone etc in the form should get cleared.

Is there any way other than:
Private Sub Command2_Click()
Me.Text4 = ""
Me.Combo6 = ""
Form.Form.Requery
End Sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:53
Joined
Feb 28, 2001
Messages
27,163
There are ways to loop through all controls to clear them, but the problem is that not all controls are the same. For instance, you can't clear the value of a multi-select list box because it isn't single-valued; you have to deselect everything that was selected. You can't clear the value of a label because it doesn't have a value. You don't clear the value of a combo-box, even if it is single-select. Instead, you reset the .ListIndex property to show that no selection has been made. So it isn't actually something you can do simply. The date-picker case IS one of the simple ones because that is usually just a regular text box with a special setting.

If this is a bound form, then creating a new record for a new student should have the effect of resetting all bound fields on the form (since there is no underlying value for the bound controls on the new record). There, your button would be superfluous.

If this is an unbound form, then I have to ask why it isn't bound because in that case you seem to be trying to re-invent the Access wheel. I.e. making more work for yourself. Get Access to work FOR you, not against you.

Therefore I would ask that you try to better describe what you really wanted to do once you cleared everything on the form. Because some of your comments seem to be contrary to the way Access normally works.
 

June7

AWF VIP
Local time
Today, 10:53
Joined
Mar 9, 2014
Messages
5,469
If controls are bound to fields, you don't 'clear', you move to a New Record row because if you 'clear' you delete data.

Agree with Doc_Man - using UNBOUND form/controls for data entry/edit doesn't take advantage of Access functionality.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 14:53
Joined
May 21, 2018
Messages
8,527
Not sure what you are doing but in general if you need to do something on a group of controls you normally select all in design view. Then add some text to the Tag property like "?". Then you loop all controls and check the ones with the tag property. You may also need to check the controltype of the control, because as said you may want to do different things for a label than a textbox or combobox.


Code:
Public Sub LoopControls()
  Dim ctrl As Access.Control
  For Each ctrl In Me.Controls
    If ctrl.Tag = "?" Then
       select case ctrl.controType
         case AcTextbox
           'do something if it is a textbox
         case acComboBox
            'do something if it is a combobox
         case acLabel
            'do something if it is a label
         ...... 
       end select
    End If
  Next ctrl
End Sub
 

Users who are viewing this thread

Top Bottom