SQL LIKE statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wietmie
    New Member
    • Aug 2008
    • 9

    SQL LIKE statements

    Hai, i'm new here, i would like to ask about SQL statements, i'm using VB6.0 and my systems if more on searching record, so the SQL statements is very important to search a record, so back to the question, i do this to search the "company name"
    Code:
    Private Sub cmdNameView_Click()
    Dim strSQL As String
    inputName = UCase(txtName.Text)
        Set rs = New ADODB.Recordset
        If inputName <> "" Then
            strSQL = "SELECT * FROM SUPPLIER WHERE sName LIKE '*" & inputName & "*'"
            MsgBox strSQL
            rs.Open strSQL, conn, adOpenKeyset, adLockPessimistic, adCmdText
            fillfields
        End If
    
    End Sub
    And here the method of fillfields()
    Code:
    Public Sub fillfields()
        
                    NotEditable
                    frameSDetail.Visible = True
                    If Not (rs.BOF = True Or rs.EOF = True) Then
                        txtsName.Text = rs.Fields("sName")
                        txtsAddress.Text = rs!sAddress
                        txtPost_Code.Text = rs!Post_Code
                        txtCity.Text = rs!City
                        txtPhone.Text = rs!Phone
                        txtEmail.Text = rs!Email
                        txtFax.Text = rs!Fax
                        txtService.Text = rs!Service
                    Else
                        MsgBox "No Record Match", vbExclamation, "Cannot Move"
                    End If
                    Exit Sub
                
    End Sub
    I got the msg "No Record Match" when i try the LIKE statements in the Microsoft Office access same as my code above, it return correct output. then i thought it is because of the logic error in fillfields method, then i try change the LIKE statements to
    Code:
    SELECT * FROM SUPPLIER WHERE sName = '" & inputName & "'
    it return a correct output. For your information sName field cannot be the same and i already create a code to validate it. Can anyone can help me to trace the logic error on it?

    thanks in advance
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    Code:
    strSQL = "SELECT * FROM SUPPLIER WHERE sName LIKE '% & inputName & %'"
    SQL syntax for LIKE uses % in most cases. Not *

    Comment

    • edwardrsmith
      New Member
      • Feb 2008
      • 62

      #3
      I have always used astrik's but I don't think that is the problem. I would change adLockPessimist ic to adLockOptimisti c.

      Code:
      Private Sub cmdNameView_Click()
      Dim strSQL As String
      inputName = UCase(txtName.Text)
          Set rs = New ADODB.Recordset
          If inputName <> "" Then
              strSQL = "SELECT * FROM SUPPLIER WHERE sName LIKE '*" & inputName & "*'"
              MsgBox strSQL
              rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic, adCmdText
              fillfields
          End If
       
      End Sub
      Let me know if that doesn't work.

      Edward

      Comment

      • jeffstl
        Recognized Expert Contributor
        • Feb 2008
        • 432

        #4
        Originally posted by edwardrsmith
        I have always used astrik's but I don't think that is the problem. I would change adLockPessimist ic to adLockOptimisti c.

        Edward
        * Isn't a universal syntax for SQL like.

        % as far as I know..is more universal across multiple database platforms.

        Why not to use * in SQL Like

        I also do not think the problem is the locking method of opening the recordset.

        That only relates to the contraints on the records of your database in terms of updating and inserting.

        It has nothing to do with whether or not records will be returned in a SELECT.

        Comment

        • wietmie
          New Member
          • Aug 2008
          • 9

          #5
          Originally posted by jeffstl
          * Isn't a universal syntax for SQL like.

          % as far as I know..is more universal across multiple database platforms.

          Why not to use * in SQL Like

          I also do not think the problem is the locking method of opening the recordset.

          That only relates to the contraints on the records of your database in terms of updating and inserting.

          It has nothing to do with whether or not records will be returned in a SELECT.
          Yes!! It works!, thank u everyone, my problem solved, must use the "%" symbol to make it works

          Code:
           strSQL = "SELECT * FROM SUPPLIER WHERE sName LIKE '%" & inputName & "%'"

          Comment

          • wietmie
            New Member
            • Aug 2008
            • 9

            #6
            I think my problem solve, today, my Access 2003 and VB6.0 act weird. Here what i face today:

            Code:
            strSQL = "SELECT * FROM SUPPLIER WHERE sName LIKE '%" & inputByName & "%'"
            i enter word "AM" and my VB return correct result.

            Code:
            strSQL = "SELECT * FROM SUPPLIER WHERE City LIKE '%" & inputByName & "%'"
            i enter word "W" my program say Cannot find Record

            i feel weird, then i try in Microsoft office 2003
            Code:
            SELECT * FROM SUPPLIER WHERE sName LIKE '%AM%'
            Microsoft office return no record found

            then i try in Microsoft office 2003
            Code:
            SELECT * FROM SUPPLIER WHERE sName LIKE '*AM*'
            Microsoft office return correct result

            then i try in VB6.0
            Code:
            SELECT * FROM SUPPLIER WHERE sName LIKE '*AM*'
            VB6.0 return cannot find record

            Can anyone explain to me what happen

            Comment

            • QVeen72
              Recognized Expert Top Contributor
              • Oct 2006
              • 1445

              #7
              Hi,

              For VB6 + ADO + Access Combination, you need to query with %:

              SELECT * FROM SUPPLIER WHERE sName LIKE '%AM%'

              Regards
              Veena

              Comment

              Working...