Search through textbox

azhar2006

Registered User.
Local time
Today, 14:15
Joined
Feb 8, 2012
Messages
289
Hello guys .
You have created a form based on a query. And put a text box in it for the purpose of conducting a search in the query records. I used this code that my dear brother (arnelgp) helped me write, but the code does not work. I don't know where the problem is
Thank you very much
Code:
Private Sub TypeDown(strSearch As String)
On Error Resume Next
    
    Dim StrSQL As String
        
    If strSearch = "" Then
        StrSQL = "QurMastr"
    Else
        StrSQL = "SELECT * FROM QurMastr " & _
                 " WHERE FullName Like '" & strSearch & "*' " & " OR bookNumber Like '" & strSearch & "*' " _
                 & " OR Result Like '" & strSearch & "*' " & " OR Success Like '" & strSearch & "*' "
    End If
    Me.frmMastr.RecordSource = StrSQL
    Me.frmMastr.Requery
    Me.txtSearch.SetFocus
    
End Sub

Private Sub txtSearch_AfterUpdate()
    On Error Resume Next

    Me.Requery
    TypeDown IIf(IsNull(Me.txtSearch.Text), "", Me.txtSearch.Text)

End Sub
 
Can you describe what happens when you enter text to search (strSearch)?
StrSQL - "QurMastr" is not a SQL statement. Is it a table or a Query itself?
 
upload your db so we can see where is wrong.
 
Can you describe what happens when you enter text to search (strSearch)?
StrSQL - "QurMastr" is not a SQL statement. Is it a table or a Query itself?
Thank you for replying
Nothing happens. Yes, it is a query based on a table
 
Thank you for replying
Nothing happens. Yes, it is a query based on a table
So the first step is: change StrSQL = QurMastr to StrSQL = "SELECT * FROM QurMastr; "

As ArnelGP suggests - upload a copy of the db
 
And put a debug.print strSQL into the code and look to see what is produced.

You know, simple debugging methods. :(

Also no need for a requery if you change the recordsource.
 
Code:
Private Sub TypeDown(strSearch As String)
On Error Resume Next
    
    Dim StrSQL As String
        
    If strSearch = "" Then
        StrSQL = "QurMastr"
    Else
        StrSQL = "SELECT * FROM QurMastr " & _
                 " WHERE FullName Like '" & strSearch & "*' " & " OR bookNumber Like '" & strSearch & "*' " _
                 & " OR Result Like '" & strSearch & "*' " & " OR Success Like '" & strSearch & "*' "
    End If
    Me.frmMastr.RecordSource = StrSQL
    Me.txtSearch.SetFocus
    
End Sub

Private Sub txtSearch_AfterUpdate()
    On Error Resume Next

    TypeDown Nz(Me.txtSearch, "")

End Sub
 
This is how I would do that search...
Code:
Private Const SQL_SEARCH As String = _
    "SELECT * FROM tbLinvestigation " & _
    "WHERE FullName Like p0 " & _
        "OR bookNumber Like p0 " & _
        "OR Result Like p0 " & _
        "OR Success Like p0 "

Private qdf_ As DAO.QueryDef

Private Property Get qdfSearch() As DAO.QueryDef
    If qdf_ Is Nothing Then Set qdf_ = CurrentDb.CreateQueryDef("", SQL_SEARCH)
    Set qdfSearch = qdf_
End Property

Private Sub txtSearch_Change()
    With qdfSearch
        .Parameters(0) = "*" & Me.txtSearch.Text & "*"
        Set Me.Recordset = .OpenRecordset
    End With
End Sub
 
This is how I would do that search...
Code:
Private Const SQL_SEARCH As String = _
    "SELECT * FROM tbLinvestigation " & _
    "WHERE FullName Like p0 " & _
        "OR bookNumber Like p0 " & _
        "OR Result Like p0 " & _
        "OR Success Like p0 "

Private qdf_ As DAO.QueryDef

Private Property Get qdfSearch() As DAO.QueryDef
    If qdf_ Is Nothing Then Set qdf_ = CurrentDb.CreateQueryDef("", SQL_SEARCH)
    Set qdfSearch = qdf_
End Property

Private Sub txtSearch_Change()
    With qdfSearch
        .Parameters(0) = "*" & Me.txtSearch.Text & "*"
        Set Me.Recordset = .OpenRecordset
    End With
End Sub
thank you

MarkK dear​

It works fine, but why did you put the event in on Private Sub txtSearch_Change() ?
Code:
Private Sub txtSearch_Change()

    With qdfSearch

        .Parameters(0) = "*" & Me.txtSearch.Text & "*"

        Set Me.Recordset = .OpenRecordset

    End With

End Sub
 
why did you put the event in on Private Sub txtSearch_Change()
Then it updates for every keystroke the user enters, but obviously, feel free to do whatever you want.
 

Users who are viewing this thread

Back
Top Bottom