How to clear search text box

F

Frank Situmorang

Hello,

With the help of good friends in this forum, the following VBA has worked to
search the text.

My question how can we make it to automatically clear the text in the search
text box without backspace or clear manually.

Private Sub Form_Load()
Me.Filter = vbNullString
Me.FilterOn = False
End Sub

Private Sub TxtCari2_AfterUpdate()
'Dim strFilter As String
'Dim strSQL As String
Dim strfilter As String
'strSQL = "Select MOM.No_KEP, MOM.Subject, " _
'& "MOM.Nota From KepMajelisQry Where " & "MOM.Nota LIKE """ & "*" &
Me.Txtcari & "*" & """"

strfilter = "([Nota] LIKE """ & "*" & Me.TxtCari2 & "*" & """)"
'CurrentDb.Execute strSQL, dbFailOnError
'strSQL = strfilter
'Me.FilterOn = True
Me.Filter = strfilter
Me.FilterOn = True
End Sub


Thanks very much
 
A

Allen Browne

From the code, I assume you have 2 unbound text boxes where the user can
enter filter values, and their names are Txtcari and TxtCari2.

Unless you set something in the DefaultValue propery of these text boxes,
they will be blank when the form opens, so you won't need to clear them in
Form_Load. If you did want to clear them, assign Null to them:
Me.Txtcari = Null
Me.TxtCari2 = Null

I tend to use unbound controls in the form header section for the user to
enter the filter values, as in this example:
http://allenbrowne.com/ser-62.html
The remove filter button's On Click property gets set to exactly this:
=ClearFilterAndHeader([Form])
i.e. don't insert your form name.


Public Function ClearFilterAndHeader(frm As Form)
On Error GoTo Err_ClearFilterAndHeader
'Purpose: Remove the filter, and clear all the unbound contorls in the
form header.
Dim ctl As Control 'Each control in the form header.

'Save if necessary.
If HasProperty(frm, "Dirty") Then
If frm.Dirty Then
frm.Dirty = False
End If
End If

'Remove the filter.
frm.FilterOn = False
frm.Filter = vbNullString

'Clear all the unbound controls in the form header.
For Each ctl In frm.Section(acHeader).Controls
If HasProperty(ctl, "ControlSource") Then
If Len(Nz(ctl.ControlSource, vbNullString)) = 0& Then
If Not IsNull(ctl.Value) Then
ctl.Value = Null
End If
End If
End If
Next

Exit_ClearFilterAndHeader:
Set ctl = Nothing
Exit Function

Err_ClearFilterAndHeader:
Call LogError(Err.Number, Err.Description, conMod &
".ClearFilterAndHeader")
Resume Exit_ClearFilterAndHeader
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top