Results 1 to 6 of 6

Thread: [RESOLVED] Queries using the "LIKE" operator, Access works/VB isn't

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Resolved [RESOLVED] Queries using the "LIKE" operator, Access works/VB isn't

    Alright, I have a program querying a database to create a list of unique company names. It works fine, however, I'm adding a "filter" addition to it and so I'll be using the LIKE operator to make it work, obviously. The code works fine when using a normal query without LIKE, and otherwise returns empty, even if it -should- be able to find the records. When using the same query in Access, I returned 6 records, but in VB nothing seems to work. Can anyone see something I'm doing wrong, or something I could try to see if it would work..?

    VB Code:
    1. Dim qry As ADODB.Recordset, query As String
    2.     Set qry = New ADODB.Recordset
    3.  
    4.     query = "SELECT DISTINCT companyname FROM companyrecords WHERE companyname LIKE '*a*'"
    5.     ' this query works fine: "SELECT DISTINCT companyname FROM companyrecords"
    6.  
    7.     qry.Open query, dbConn, adOpenDynamic, adLockReadOnly, adCmdText
    8.    
    9.     If qry.EOF And qry.BOF Then
    10.       MsgBox "no records"
    11.       Exit Sub
    12.     End If
    13.    
    14.     Do Until qry.EOF
    15.       MsgBox qry!CompanyName
    16.       qry.MoveNext
    17.     Loop
    18.     qry.Close
    19.     Set qry = Nothing

    I'm clueless at this point, I've never worked with Access and VB before doing this project, and my experience with databases comes mostly from MySQL, but the syntax is nearly exactly the same, which is why I have no idea what's going wrong.
    Like Archer? Check out some Sterling Archer quotes.

  2. #2

  3. #3

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Queries using the "LIKE" operator, Access works/VB isn't

    Quote Originally Posted by MartinLiss
    I believe that in VB you need to use the % character rather than the * with Like.
    It's not VB but rather ADO that doesn't recognize the asterisc as a wild card.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Queries using the "LIKE" operator, Access works/VB isn't

    Quote Originally Posted by MartinLiss
    I believe that in VB you need to use the % character rather than the * with Like.
    oh, jeez. I was reading through these forums the other day, and someone said it was * for Access (while % is for MySQL, which is why I was doing it like this). But, anyway.. thanks! it works fine now.. I knew I couldn't have been doing much wrong.

    edit: thanks to you too, Rhino. I'll give you a rating as well, just because I'm glad I can finish my project now @_@
    Like Archer? Check out some Sterling Archer quotes.

  6. #6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width