FIX: Parameter Mismatch with Subqueries When Using Access ODBC Driver

Article translations Article translations
Article ID: 244719 - View products that this article applies to.
This article was previously published under Q244719
Expand all | Collapse all

On This Page

SYMPTOMS

When executing a parameterized query that contains a subquery, when using the Microsoft Open Database Connectivity (ODBC) Driver for Access, various unexpected problems occur.

Reported problems include unexpected results (no records returned or the wrong records returned) or other errors indicating a type mismatch or a numeric value out of range.

CAUSE

This problem is caused by a bug in the underlying Microsoft Jet 4.0 engine that is included with the Microsoft Data Access Components (MDAC) versions noted above.

Parameters markers in ODBC are typically mapped from left to right with no regard as to whether they are in the main query or a subquery. When you have a main query and a subquery, Jet maps the subquery parameters first and maps the main query parameters next. Therefore, in most cases, the parameters are mapped in a different order than expected. This parameter mapping order means that unexpected parameter values are passed in to one or more of the parameters and can result in unexpected query results or type mismatch errors (for example, if a text value is passed into a numeric parameter).

For example, the following SQL query has four parameters for fields A, B, C, and D:
SELECT * FROM MainTable 
WHERE A=? AND B=? AND 
E IN ( SELECT E FROM SubQueryTable WHERE C=? AND D=?) 
				
Normally in ODBC the programmer expects the parameters to be bound in the order A, B, C, D; however, because of this particular bug in Microsoft Jet, the parameters are bound in the order C, D, A, B.

RESOLUTION

A supported fix that corrects this problem is now available from Microsoft, but it has not been fully regression tested and should be applied only to systems experiencing this specific problem. If you are not severely affected by this specific problem, Microsoft recommends that you wait for the next service pack for Microsoft Data Access Components that contains this fix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the fix. For a complete list of Microsoft Product Support Services phone numbers and information on support costs, please go to the following address on the World Wide Web:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Microsoft Jet 4.0 Service Pack 4.

For additional information about how to download the latest Jet 4.0 service pack, click the following article number to view the article in the Microsoft Knowledge Base:
239114 How To: Obtain the Latest Service Pack for the Microsoft Jet 4.0 Database Engine

MORE INFORMATION

Steps to Reproduce Behavior

The following VBA code reproduces the parameter mismatch problem. Note that your VBA project requires a reference to the "Microsoft ActiveX Data Objects 2.1 Library."
Sub ParameterMisMatchDemo()
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.recordset
Dim strSQL As String

    ' This test requires a copy of the Microsoft Access sample database
    ' NorthWind.mdb in the C:\ folder.
    conn.Open "Driver=Microsoft Access Driver (*.mdb);" & _
              "DBQ=C:\NorthWind.mdb;"
    
    ' Build SQL statement. Note that there is a parameter marker
    ' in the main query (Country=?) as well as a parameter marker
    ' in the subquery (OrderID=?).
    strSQL = "Select CustomerID From Customers " & _
             "Where Country=? And CustomerID In " & _
             "(Select CustomerID From Orders Where OrderID=?)"

    cmd.ActiveConnection = conn
    cmd.CommandText = strSQL
    cmd.CommandType = adCmdText

    ' Manually add parameters in the normal "left to right" order.
    cmd.parameters.Append cmd.CreateParameter("Country", _
           adVarChar, adParamInput, 15)
    cmd.parameters.Append cmd.CreateParameter("OrderID", _
           adInteger, adParamInput)

    ' Set parameter values. These values should filter down the
    ' resultset to one record with a CustomerID of "HUNGC".
    cmd.parameters("Country").Value = "USA"
    cmd.parameters("OrderID").Value = 10375
    Set rs = cmd.Execute

    ' This should return CustomerID "HUNGC", but instead the following
    ' error is returned: "Either BOF or EOF is True, or the current record 
    ' has been deleted; the operation requested by the application requires         
    ' a current record."
    MsgBox rs.fields("CustomerID").Value

End Sub
				

Properties

Article ID: 244719 - Last Review: April 7, 2006 - Revision: 4.1
APPLIES TO
  • Microsoft Open Database Connectivity Driver for Access 4.0
  • Microsoft Data Access Components 2.1
  • Microsoft Data Access Components 2.5
  • Microsoft Data Access Components 2.6
Keywords: 
kbqfe kbhotfixserver kbbug kbdatabase kbfix kbjet kbmdac260fix kbmdacnosweep KB244719

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com