Change Query Property using VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brilstern
    New Member
    • Dec 2011
    • 208

    Change Query Property using VBA

    For simplicities sake I don't have my SQL in my VBA. I am just using public functions to filter pre-built queries when needed. I have a new need to change the value of the Top Values property depending on the usage of the query.

    So my question: Can you change the properties of a query in VBA?

    I have tried this and similar methods:
    Code:
        Set rst = db.OpenRecordset("queryName")
        rst.Properties("Top Values") = strTopValues
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What you're looking for is the QueryDef object.

    Comment

    • jforbes
      Recognized Expert Top Contributor
      • Aug 2014
      • 1107

      #3
      I don't think Top can be a Parameter. You might be able to edit the QueryDef to get this done. This is probably not the best method, but it may work for you. Some Sample code to update a QueryDef:
      Code:
      Dim oQD As QueryDef
      Set oQD = CurrentDb.QueryDefs("qryTestFilter_DoNotDelete")
      oQD.SQL = "SELECT * FROM " & sTable & " WHERE " & sWhere

      Comment

      • Brilstern
        New Member
        • Dec 2011
        • 208

        #4
        @Rabbit
        I checked out QueryDef a little but I'll jump more into it then.

        @jForbes
        I thought about editing the SQL string but the issue is it is a variable value. I guess I could reset it to a specific value every time at the end of that specify query script.

        When I get home I'll see where I come out. Thanks gents.

        Comment

        • Brilstern
          New Member
          • Dec 2011
          • 208

          #5
          So... I looked at using .MaxRecords and it doesn't seem to effect the query when I do it manually or with VBA.

          Another weird thing I came across. If I set the Top Values to 10, or 20 it works fine. If I set it to 50 it is returning 53 records... not sure what is up with it.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            I'm guessing there are ties so it returns all the ties.

            Instead of the maxrecords property, you could try changing the SQL.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32645

              #7
              Look in the Help for the TOP predicate of the SELECT clause. It will explain why the number doesn't always match the one specified.

              Comment

              • Brilstern
                New Member
                • Dec 2011
                • 208

                #8
                I just ended up setting up a variable loop to end the query after it hits the required amount. Thanks everyone! NeoPa, I see what you were talking about with the TOP predicate properties now. Good to know for future use.

                Comment

                Working...