Clearing Controls & Forms in Access (VBA)
Clearing Controls & Forms in Access (VBA)
praxis1949 commented: Very elegant item for clearing all controls. +0
---- start of revised code ----
Private Sub cmdClearCriteria_Click()
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl
End Sub
'---- end of revised code ----
'OR
'*********** CLEAR FORM FIELDS CODE ***********************
Sub Clear_Form()
Dim X As Control
For Each X In Me.Controls
If TypeOf X Is TextBox Then
X = Null
End If
Next X
End Sub
'************************************************************
OR
'***********************************************
Sub ClearFormText(frm As Form)
Dim ctl As Control
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Then
ctl.Value = ""
End If
Next ctl
End Sub
Call the code from any event with the following syntax:
ClearFormText Me
The 'Me' keyword will pass in a reference to the current form and the code will clear all text boxes.
'********************************************
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.