Like query works in query editor but not in VBA code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nates
    New Member
    • Apr 2008
    • 16

    Like query works in query editor but not in VBA code

    If i use the following in the query editor it returns rows from my database, however when i run the very same query in VBA the recordset recordcount returns -1 every time:
    Code:
    sql = "SELECT uniqueid, Notes FROM TaskRecords WHERE Notes Like '*" & SearchBox.Value & "*'"
    UserRS.Open sql, UserConn
    The [Notes] field is type Memo. The database contains around 5500 entries at present with up to 1000 chars in each memo field. I am using the above query to speed up the search form i put together (which had previously used InStr record by record to find matches - extremely slow).

    Any ideas?

    Thanks
    Last edited by NeoPa; Apr 6 '08, 10:06 PM. Reason: Please use [CODE] tags
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Originally posted by Nates
    If i use the following in the query editor it returns rows from my database, however when i run the very same query in VBA the recordset recordcount returns -1 every time:

    sql = "SELECT uniqueid, Notes FROM TaskRecords WHERE Notes Like '*" & SearchBox.Value & "*'"
    UserRS.Open sql, UserConn

    The [Notes] field is type Memo. The database contains around 5500 entries at present with up to 1000 chars in each memo field. I am using the above query to speed up the search form i put together (which had previously used InStr record by record to find matches - extremely slow).

    Any ideas?

    Thanks
    Hi

    If you are sure there are records to be return for the criteria used, then perhaps this will yield a RecordCount > 1

    UserRS.Open sql, UserConn, adOpenStatic, adLockReadOnly

    ???

    With the parameters you have (not) used or with a dymamic recordsets you should use the recordset .EOF property to test that records have been returned.

    MTB

    Comment

    • Nates
      New Member
      • Apr 2008
      • 16

      #3
      Unfortunately the above did not find any records either.

      Ive uploaded a couple of screenshots that highlight the problem:






      The above SQL in the query editor is slightly adapated but produced the same problem when used in the code.

      Im baffled. Seems like a bug.

      Comment

      • MikeTheBike
        Recognized Expert Contributor
        • Jun 2007
        • 640

        #4
        Hi again

        Unfortunately, I only gave you half the answer last time. Failed to note this is an ADO recordset, so the wild card characture is % NOT *.

        So you need my previous mod plus the above change, ie.
        Code:
        sql = "SELECT uniqueid, Notes FROM TaskRecords WHERE Notes Like '%" & SearchBox.Value & "%'"
        UserRS.Open sql, UserConn, adOpenStatic, adLockReadOnly
        ??

        MTB
        Last edited by NeoPa; Apr 6 '08, 10:06 PM. Reason: Please use [CODE] tags

        Comment

        • Nates
          New Member
          • Apr 2008
          • 16

          #5
          Thanks Mike that did the trick :)

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32636

            #6
            You may find ANSI Standards in String Comparisons interesting.

            Comment

            Working...