Results 1 to 8 of 8
  1. #1
    bidbud68 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    19

    Correct Syntax For "Not Like"

    Hi I would like to take the SQL from my Access Query as follows however the syntax is not correct for the Not Like conditions, is there any other format I can use ?



    Code:
    SELECT DISTINCT Accounts.ReservationID, Reservations.ReservationID, Reservations.[SalesPerson Email], Customers.EmailAddress, Accounts.Date, Accounts.Item
    FROM Customers INNER JOIN (Accounts INNER JOIN Reservations ON Accounts.ReservationID = Reservations.ReservationID) ON Customers.CompanyName = Reservations.Customer
    WHERE (((Accounts.Item) Not Like "*Payment*" And (Accounts.Item) Not Like "*Trf*" And (Accounts.Item) Not Like "*Deposit Received*" And (Accounts.Item) Not Like "*Deposit Reversed*"));

  2. #2
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,536
    Try changing your Where condition to Or instead of And. Your statement now says that all the conditions need to be met to get a result. Is this what you meant or did you mean that any one of the conditions need to be met? If the latter, then change the ands to ors.

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Have you considered using <>
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    I don't see anything wrong with that syntax. Where is it that it does not work - in Access or somewhere else?

    John

  5. #5
    bidbud68 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    19
    It works in an Access Query but when I copy the SQL to VBA it doesnt work

  6. #6
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    It will work fine in VBA, but you have to use it a bit differently. Since it is a select query, about all you can do with it is create a recordset that you will manipulate further. The VBA would look something like this:

    Dim db as Database, rst as RecordSet, SQL as string
    set db=currentdb


    SQL = "SELECT DISTINCT Accounts.ReservationID, Reservations.ReservationID, Reservations.[SalesPerson Email], Customers.EmailAddress, Accounts.Date, Accounts.Item FROM Customers INNER JOIN (Accounts INNER JOIN Reservations ON Accounts.ReservationID = Reservations.ReservationID) ON Customers.CompanyName = Reservations.Customer WHERE (((Accounts.Item) Not Like '*Payment*' And (Accounts.Item) Not Like '*Trf*' And (Accounts.Item) Not Like '*Deposit Received*' And (Accounts.Item) Not Like '*Deposit Reversed*'))
    Set rst = db.OpenRecordSet(SQL)
    While not rst.EOF
    .
    . Code to manipulate the data
    '
    rst.MoveNext
    Wend

    Note that I changed the double-quotes within the SQL to single quotes, to make it easier to read; SQL allows that.

    HTH

    John
    Last edited by John_G; 10-20-2012 at 12:11 PM. Reason: Add missing line break

  7. #7
    bidbud68 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    19
    Thank you I tried copying it again from the access query SQL and it works.

  8. #8
    RonL is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Location
    NJ
    Posts
    114
    Quote Originally Posted by John_G View Post
    It will work fine in VBA, but you have to use it a bit differently. Since it is a select query, about all you can do with it is create a recordset that you will manipulate further. ..............
    I have an ancillary (and basic) question, since I'm about to deal with something similar. It relates to my lack of fundamental understanding of the object model, in particular, recordsets.

    When you say "...create a recordset..." is "create" - ie. instantiation - meant literally? Or is it really just an *assignment* of a variable to the query's result?

    The reason I'm confused is, from my reading, one must adhere to a similar syntax even when the query has already been defined and is stored in the database - eg. can be seen in the project navigator. So in the VBA statements, why are we "creating" the recordset when it already exists??

    Is not the "set rst..." statement simply an assignment of an existing object to an object variable,and not an instantiation? If it is an instantiation, then how does the manipulation of the new object get reflected back to the original object, eg. the recordset we see on the form whose control source is the query, for example?

    Would appreciate any appropriate link to documentation in this area.

    Thanks, -Ron
    Last edited by RonL; 10-22-2012 at 12:05 PM. Reason: clarity

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2013, 12:59 PM
  2. Export "Query or Report" to a "Delimited Text File"
    By hawzmolly in forum Import/Export Data
    Replies: 3
    Last Post: 08-31-2012, 08:00 AM
  3. Replies: 11
    Last Post: 03-29-2012, 02:32 PM
  4. Replies: 13
    Last Post: 12-05-2011, 05:10 AM
  5. Replies: 16
    Last Post: 07-22-2011, 09:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums