Searching a field via code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Buineugenio
    New Member
    • Mar 2008
    • 10

    #1

    Searching a field via code

    Mr Experts
    I am a new user using Access and I want to know how is possible search a field using wildcard (*) to find anything begining with a certain char and display in a form showing a list of records.
    By example; the name begin with e and there are many records with names beginning with e but they are similars.
    Thanks a lot
    Buineugenio
    Maputo, Mozambique, Africa
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    When you talk about searching, I presume you are talking about SQL.

    The way to find matching records in SQL is in the WHERE clause.
    [CODE=SQL]SELECT *
    FROM [YourTable]
    WHERE [YourField] Like 'E*'[/CODE]

    Comment

    • Buineugenio
      New Member
      • Mar 2008
      • 10

      #3
      Originally posted by NeoPa
      When you talk about searching, I presume you are talking about SQL.

      The way to find matching records in SQL is in the WHERE clause.
      [CODE=SQL]SELECT *
      FROM [YourTable]
      WHERE [YourField] Like 'E*'[/CODE]
      Thanks, is that I want, only I need to insert in the code, the searching string from a text box in a form.
      Thanks a lot
      Buineugenio
      Maputo, Mozambique, Africa

      Comment

      • WiscCard
        New Member
        • Mar 2008
        • 6

        #4
        What if you create a query with something like this in Criteria area of the column you want to search:
        Like "*" & [Which Customer Name are you looking for?] & "*"

        Then, create a command button that opens this query and then set your form data source to the query.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          The easiest way is to include a reference to the form control in your query (SQL).
          An example might be :
          [CODE=SQL]SELECT *
          FROM [YourTable]
          WHERE [YourField] Like Forms!frmYourFo rm.YourControl & '*'[/CODE]

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            If creating the SQL in a VBA string you can do it more directly and avoid a control reference in the SQL (Assumes code is run from within the form's module) :
            Code:
            Dim strSQL As String
            
            strSQL = "SELECT * " & _
                     "FROM [YourTable] " & _
                     "WHERE [YourField] Like '" & Me.YourControl & "*'

            Comment

            Working...