Help with qdf.OpenRecordset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dcozad999
    New Member
    • Feb 2012
    • 3

    Help with qdf.OpenRecordset

    Hello,

    Access VBA newbie here and need a little help getting this to work. I have a select query set up with two parameters on one field so I can use a date range:

    Between [Start Date] And [End Date]

    This works just fine when running the query all by itself.

    I have created a form which allows the user to enter the Start Date and End Date and then runs the query when selecting a button. The problem I'm having is no results show up. The code runs through but doesn't display even an empty results page.

    My code is below. The message box fires at the end but nothing displays on the screen prior to that. I've tried using ("Start Date") and ("End Date") in place of (0) and (1) for the parameters and that doesn't work either.

    I've also checked the me.StartDateTB and me.EndDateTB values and they are correct.

    Any help you could give me would be appreciated.


    Code:
    Private Sub RunBtn_Click()
        
        Dim db As DAO.Database
        Dim qdf As DAO.QueryDef
        Dim rst As DAO.Recordset
                    
            Set db = CurrentDb
            Set qdf = db.QueryDefs("TestStep1")
                qdf.Parameters(0) = Me.StartDateTB
                qdf.Parameters(1) = Me.EndDateTB
            Set rst = qdf.OpenRecordset
    
            MsgBox "finished"
            
    End Sub
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    What would you expect to disply on Screen? Is is a Recordset Object you are trying to create, not a Querydef Object. A recordset has no visible Interface.

    Comment

    • dcozad999
      New Member
      • Feb 2012
      • 3

      #3
      All I'm really trying to do is use entries from two textboxes in a form as the parameters for a query that is already set up in Access. Then display the results on the screen.

      thanks for any and all help.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32634

        #4
        As ADezii said, opening a Recordset does Not cause the data to display on the screen.

        Comment

        • dcozad999
          New Member
          • Feb 2012
          • 3

          #5
          Originally posted by NeoPa
          As ADezii said, opening a Recordset does Not cause the data to display on the screen.

          NeoPa,

          I understand that now. I am still trying to find a way to accomplish my goal. After ADezii's response, I googled around some more and tried the following but it still displays nothing.

          All I would really like to know is how to display the results of TestStep1 with the parameters from the form.

          Thanks in advance for the help.


          Code:
                  Dim db As DAO.Database
                  Dim qdf As DAO.QueryDef
                  Dim rst As DAO.Recordset
                  
                  Set db = CurrentDb
                  Set qdf = db.QueryDefs("TestStep1")
                      qdf.Parameters(0) = Me.StartDateTB
                      qdf.Parameters(1) = Me.EndDateTB
                  Set rst = qdf.OpenRecordset
                  
                  Do Until rst.EOF
                      Debug.Print rst!VENDOR_CARRIER_ID
                      rst.MoveNext
                  Loop
                  
                  MsgBox "finished"

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32634

            #6
            I'm not aware that you can pass parameter values to a QueryDef object when you open it for display. What you can do though, is to design the QueryDef to get it's values from a form instead.

            Comment

            Working...