Clear all controls in a form. (1 Viewer)

Han_S_84

Registered User.
Local time
Today, 23:05
Joined
May 5, 2010
Messages
10
I have a form with a mixtue of controls on, Combo Boxes looking up different tables, text boxes controlled by what is selected in the combo box and plain text boxes. All controls are unbound.
I used a button to run code which inserts the values entered/selected into a table. I would like the button to then clear the form ready for the user to enter new values.
I can do this by closing and reopening the form - but this isn't very neat.

I have tried using me. with refresh or undo neither of which work, but give no error messages.

I have also tried the code

Dim ctl As Control
For Each ctl In Me.Controls
ctl = vbNull String

Next

This gives me a runtime error 438.
Please help!!
 

boblarson

Smeghead
Local time
Today, 15:05
Joined
Jan 12, 2001
Messages
32,059
Here's a function you can put in a standard module and then call it whenever you want any form cleared.

Code:
Function ClearAll(frm As Form)
Dim ctl As Control

For Each ctl In frm.Controls
   Select Case ctl.ControlType
      Case acTextBox
           ctl.Value = ""
      Case acOptionGroup, acComboBox, acListBox
          ctl.Value = Null
      Case acCheckbox
         ctl.Value = False
   End Select
Next
End Function

And then you call it by using

ClearAll Me

to clear the current form.
 

Han_S_84

Registered User.
Local time
Today, 23:05
Joined
May 5, 2010
Messages
10
Hi thank-you for your reply. I find this works until I get to a text box which is dependant upon the value in another text box, therefore it is locked. Is it possible to skip just this text box.
I have tried using If statements but get myself in a muddle with the For and Next statements lining up.
 

vbaInet

AWF VIP
Local time
Today, 23:05
Joined
Jan 22, 2010
Messages
26,374
Code:
      Case acTextBox
       If ctl.Name <> "[COLOR=Red][B]TextboxName[/B][/COLOR]" then
               ctl.Value = ""
           end if
 

rabuayya

Registered User.
Local time
Today, 18:05
Joined
Aug 29, 2012
Messages
26
I don't know if I can post on this thread considering it is ~8 yrs old. I just stumbled on it and it really helped me clear all controls in my form except for a listbox. I am trying to deselect the items in the listbox.

My form contains text boxes and one list box. I created a btnClearAll button to run the above function when clicked.

All the text boxes do get cleared but the items in the listbox remain selected. Why would the items in the list box not get deselected?

Can someone help me with that?
Thanks
 
Last edited:

missinglinq

AWF VIP
Local time
Today, 18:05
Joined
Jun 20, 2003
Messages
6,423
Is the Multi-Select Property of the Listbox set to anything other than None? If so, it no longer has a Value Property...hence

ctl.Value = Null

won't work. You'd have to have a separate clause for Listboxes and iterate thru them, unselecting each item...with something along the order of
Code:
Case acListBox

 For intLB = 0 to ctl.Name.ListCount - 1
  ctl.Name.Selected(intLB) = False
 Next intLB

At the top of your Sub you should Dim the Variable intLB with

Dim intLB As Integer

Not my original code, but sadly, I don't have the source.

Linq ;0)>
 

N.C.Barrett

New member
Local time
Today, 15:05
Joined
Sep 19, 2019
Messages
4
I tried that code in Access 2010, and

Code:
ClearAll Me

doesn't even get past the compilation stage. VBA throws "Compile Error: Expected variable or procedure, not module". I'm calling this from a private subroutine bound to a button, with

Code:
Option Compare Database
Option Explicit

at the very top.

Since I'm still learning how to build databases in Access, I'm not sure how to solve that. Thanks in advance for help.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:05
Joined
Sep 21, 2011
Messages
14,238
Sounds like you called the module name ClearAll.?

Name the module something else.
The actual function name should be as Bob posted.

HTH
 

Users who are viewing this thread

Top Bottom