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

    Query problem in VBA


    I have a query that when I run it in in the Query Editor it works perfectly. However, I need to be able to run the query in VBA. I have a report that needs to run this query using parameters supplied by the report. When I put it in VBA and set it to run I get an error message "Method 'Open' of object '_Recordset' failed" Any help would be appreciated.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It would help to see the code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Here it is.

    Code:
    Public Function StarsBenchmark(ByRef msr As String, msryear As Long) As Double
        Dim strsql As String, rs As New ADODB.Recordset
        
        strsql = "Select Beg, End from IStar_Matrix where submeasureCode = """ & _
        msr & """ and star = 5 and Measureyear = " & msryear
    
        rs.Open strsql, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
        If Not rs.EOF Then
            If Left(msr, 3) = "PCR" Then
                StarsBenchmark = rs!high
            Else
                StarsBenchmark = rs!Low
            End If
        Else
            StarsBenchmark = 0
        End If
        rs.Close
        Set rs = Nothing
    End Function

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I don't use ADO much, but presuming the basics are right, use this to see what the finished SQL looks like:

    BaldyWeb-Immediate window
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Paul that's how I determined that the query works in the Query analyzer copied it from the immediate window to Query Analyzer and it works as expected. It's only not working in VBA. The SQL is

    Code:
    Select Beg, End from IStar_Matrix where submeasureCode = "ABA - BMI within measure year or prior year" and star = 5 and Measureyear = 2016

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    What line does the error occur?

    In my testing, the SQL has fields "Beg" and "End" (note that "END" is a reserved word), but in the "IF...End IF" test, you have the lines:
    StarsBenchmark = rs!high and
    StarsBenchmark = rs!Low.

    But "High" and "Low" are not fields in the record set, so an error is raised.

    Code:
    Public Function StarsBenchmark(ByRef msr As String, msryear As Long) As Double
        Dim strsql As String, rs As New ADODB.Recordset
        
        strsql = "Select Beg, End from IStar_Matrix where submeasureCode = """ & _
        msr & """ and star = 5 and Measureyear = " & msryear
    
        rs.Open strsql, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
        If Not rs.EOF Then
            If Left(msr, 3) = "PCR" Then
                StarsBenchmark = rs!high
            Else
                StarsBenchmark = rs!Low
            End If
        Else
            StarsBenchmark = 0
        End If
        rs.Close
        Set rs = Nothing
    End Function

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

Similar Threads

  1. Update Query Problem (Not an Updateable Query)
    By McArthurGDM in forum Queries
    Replies: 6
    Last Post: 02-19-2015, 11:25 AM
  2. Problem with a Query please help
    By winterh in forum Queries
    Replies: 2
    Last Post: 04-12-2012, 07:00 AM
  3. Query problem
    By lamkee in forum Queries
    Replies: 13
    Last Post: 12-12-2010, 10:30 PM
  4. Query problem
    By phoebe in forum Access
    Replies: 1
    Last Post: 08-23-2010, 01:52 PM
  5. query problem i have a problem wi
    By maxx3 in forum Queries
    Replies: 0
    Last Post: 06-29-2009, 02:29 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