We help IT Professionals succeed at work.

MS Access 2010 VBA sql string

Medium Priority
576 Views
Last Modified: 2012-06-27
Dear Experts,

  I'm composing a Where clause of an SQL Statement for OpenReport event in a MS Access 2010 Form.  The records showing go the report is different from the returning records when I issue the query directly on SQL Server 2005.  How do I make the SQL string in VBA be equivalent to the WHERE clause in the SQL Server 2005's sql statement?

The WHERE clause In SQL Server 2005:

WHERE
OrderDate = '10-10-2011' and OrderCountry ='USA'
And
( OrderCategories = 'A' or
  OrderSpecial =1          and
  OrderExpedite =0    
 )

Result Set

Date                Country     Category     SpecialOrder    ExpediteOrder
---------------------------------------------------------------------------
10-10-2011     USA            NULL             1                     0
10-10-2011     USA            A                   1                     0
10-10-2011     USA            A                   0                    0



The SQL string in OpenReport event of a MS Access Form:

sqlStr=
"[Order.OrderDate]=#" & Me.[txt_OrderDate] & "# and [Order.OrderCountry]='" & Me.[txt_Country] & "' and ([Order.OrderCategories]='" & Me.[txt_Category] & "' or [Order.OrderSpecial]=" & Me.[chk_OrderSpecial] & " and [Order.OrderExpedite]=" & Me.[chk_OrderExpedite] & ")"

Result Set

Date                Country     Category     SpecialOrder    ExpediteOrder
---------------------------------------------------------------------------
10-10-2011     USA            A                   1                     0
10-10-2011     USA            A                   0                    0



My Question

The result set from SQL Server 2005 is what I want.  How come the MS Access Report does not include the first record (with Category as NULL).  Is there anything wrong with my SQL string in VBA ?


Thank you!!!
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Access uses -1 for true.

Test against  
abs(Me.[chk_OrderSpecial])

Commented:
Put a brak on this string
sqlStr=
"[Order.OrderDate]=#" & Me.[txt_OrderDate] & "# and [Order.OrderCountry]='" & Me.[txt_Country] & "' and ([Order.OrderCategories]='" & Me.[txt_Category] & "' or [Order.OrderSpecial]=" & Me.[chk_OrderSpecial] & " and [Order.OrderExpedite]=" & Me.[chk_OrderExpedite] & ")"

using F9.

Then run the code and when it stops on the above line copy the entire code and past it in the immediate window (CTRL+G) preceding with a quetion mark and press enter. It will reveal the entire string with parameters passed. Let me know if you need more help.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Also try using some ()

WHERE
OrderDate = '10-10-2011' and OrderCountry ='USA'
And
( (OrderCategories = 'A' or
  OrderSpecial =1)          and
  OrderExpedite =0    
 )


Put parenthesis in your Access query.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.