Results 1 to 6 of 6
  1. #1
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071

    SQL Statement gets different results between Query Analyzer and VBA

    I have a command button on an Access 2010 Form that populates a recordset and sets the form filter based on the results. The recordset is opened with the following statement

    rs.Open strsql, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly

    The recordset returns 0 Records.

    If I put in a breakpoint at the rs.open statement and then go into the immediate window and put in ?strsql I get the SQL Statement that is executed. I then copy that statement into Query analyzer and run it. I get the 1 record I expect to get there.



    Why would I get different results between running a query in the Analyzer and running the exact same query in Code?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  3. #3
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Select memb_keyid from dbo_current_membership where lastnm like "Milhon*"

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Different engines, different wildcards.

    Building sql with VBA and opening recordset with wildcard parameter doesn't work with * wildcard. Try %. Been there, been very frustrated. Had to modify code because couldn't get * to work. Several years later finally figured out the issue.

    EDIT: As Paul explains below, the conflict is due to DAO vs ADO recordset.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Thanks that was it.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    To clarify, use * with a DAO recordset and % with an ADO recordset. I suppose the left hand not knowing what the right hand is doing at MS. Don't shoot me, I'm just the messenger.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  2. Hiding Results of If Statement
    By DDEB in forum Queries
    Replies: 1
    Last Post: 06-14-2012, 04:48 PM
  3. Replies: 6
    Last Post: 05-14-2012, 07:24 AM
  4. Replies: 11
    Last Post: 12-14-2010, 01:25 PM
  5. Replies: 4
    Last Post: 10-12-2010, 02:11 PM

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