Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862

    Open Dynamic Parameterized Query using DAO

    Is there a way to open a parameterized query using DAO without passing the parameters (via DAO) to the named query object? I have some criteria in the query object that references controls on an open form. I want to open that query object with DAO using OpenRecordset but keeping getting "Parameters Expected" error.



    So the form is open and the query works fine when I open it using the Nav Pane but if I try to execute the VBA that uses DAO it don't like it. I guess I should expect this but wondering if there is a way to avoid writing VBA to pass the criteria that is already in the query object as SQL.

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Can you show us the code you are trying to use.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Just a basic OpenRecordset. I tried dbOpenDynaset too...


    Code:
    'Calculate the total number of units
    Dim rsSum As DAO.Recordset
    Dim lngSum As Long
    lngSum = 0
    Set rsSum = CurrentDb.OpenRecordset("qryInvView", dbOpenSnapshot)
        With rsSum
            If .EOF = False Then
                .MoveFirst
                    While .EOF = False
                        lngSum = lngSum + ![CountUnits]
                        .MoveNext
                    Wend
            End If
        Me.txtUnitCount.Value = lngSum
        End With
    rsSum.Close
    Set rsSum = Nothing
    In case you are curious, here is the SQL from the query object named in the DAO. THis SQL works fine when I double click the query in the Nav Pane. Within this SQL is another query object that is a Totals query.

    Code:
    SELECT tblPurchOrd.PurchOrdPK, tblPurchOrd.CustFK, tblPurchOrd.PO, qryProdDescrInParam.Product, qryProdDescrInParam.CountUnits, qryProdDescrInParam.SpeciesPK, qryProdDescrInParam.GradePK, qryProdDescrInParam.DimensionPK, qryProdDescrInParam.CurePK
    FROM tblPurchOrd INNER JOIN qryProdDescrInParam ON tblPurchOrd.PurchOrdPK = qryProdDescrInParam.PurchOrdFK
    WHERE (((tblPurchOrd.PurchOrdPK)=IIf([Forms]![frmInventory]![cmbPO]<>"",[Forms]![frmInventory]![cmbPO],[tblPurchOrd].[PurchOrdPK])) AND ((tblPurchOrd.CustFK)=[Forms]![frmInventory]![cmbCust]) AND ((qryProdDescrInParam.SpeciesPK)=IIf([Forms]![frmInventory]![cmbSpecies]<>"",[Forms]![frmInventory]![cmbSpecies],[qryProdDescrInParam].[SpeciesPK])) AND ((qryProdDescrInParam.GradePK)=IIf([Forms]![frmInventory]![cmbGrade]<>"",[Forms]![frmInventory]![cmbGrade],[qryProdDescrInParam].[GradePK])) AND ((qryProdDescrInParam.DimensionPK)=IIf([Forms]![frmInventory]![cmbDim]<>"",[Forms]![frmInventory]![cmbDim],[qryProdDescrInParam].[DimensionPK])) AND ((qryProdDescrInParam.CurePK)=IIf([Forms]![frmInventory]![cmbCure]<>"",[Forms]![frmInventory]![cmbCure],[qryProdDescrInParam].[CurePK])));
    Last edited by June7; 04-04-2014 at 10:22 PM.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    So far, the only solution I am finding is to use QueryDefs and pass the variable through the query def. Kinda defeats the purpose of the query object.

    I preach to avoid parameterized queries for most solutions. I thought I would try one for the first third of this Module I am constructing. It does not seem to be panning out. I am going to spend my time bringing the SQL from the object into the class and then I will revisit why I could not get the parameterized query to work. Might be able to look at the reasons why by Monday. I need to get this Inventory Interface completed first.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can usually wrap each form reference in the Eval() function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I was able to get the Query Object to work when I included the Eval() function but I still get an exception when I try to open the recordset via DAO.

    The following is the only code I am executing. I moved it to a seperate click event for testing.

    Code:
    'Calculate the total number of units
    Dim rsSum As DAO.Recordset
    Dim lngSum As Long
    lngSum = 0
    Set rsSum = CurrentDb.OpenRecordset("qryInvView", dbOpenSnapshot)
       With rsSum
            If .EOF = False Then
                .MoveFirst
                    While .EOF = False
                        lngSum = lngSum + ![CountUnits]
                        .MoveNext
                    Wend
            End If
        Me.txtUnitCount.Value = lngSum
        End With
    rsSum.Close
    Set rsSum = Nothing

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Can you post the db here? It worked for me; the query was against a linked SQL Server table, but I can't imagine that would matter.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Let me see if I can put something together that I can upload.

  9. #9
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    ItsMe,

    See if this link helps.

    Good luck.

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I tried my best to clean up a version so the form would be intuitive. The back story is I am revising a form to offer more features. The form will allow the user to apply various criteria to eventually create reports for Front Operations regarding warehouse location of filtered inventory.

    The section I am currently focusing on is the ListBox RowSource. The Customer ID will be mandatory and the other criteria will be optional. Behind the form is legacy code as well as some ideas for the future that is commented out. I cleaned up (removed) a lot of controls and code to focus on the listbox Rowsource but, there is still some code in there that is commented out.

    Orange, I will take a look at that link now.
    Attached Files Attached Files

  11. #11
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Orange, that worked. Did you know it was going to work?

    This is the QueryDef solution I was referring to in post #5
    http://support.microsoft.com/kb/142938

    It seems the function you linked to does all the heavy lifting for you. All I had to do was pass the query object name along with the snapshot argument and whamo. It seemed to work quickly too. Performance is a major consideration as I refactor my code and try new things.

    I know I have a soltion that I can roll out and there is a big light at the end of the tunnel. I am not going to mark this solved yet though. I am still interested in understanding if, indeed, the only solution is QueryDefs.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Cool function. Regarding Eval(), the argument needs to be a string. This doesn't throw the error:

    SELECT tblPurchOrd.PurchOrdPK, tblPurchOrd.CustFK, tblPurchOrd.PO, qryProdDescrInParam.Product, qryProdDescrInParam.CountUnits, qryProdDescrInParam.SpeciesPK, qryProdDescrInParam.GradePK, qryProdDescrInParam.DimensionPK, qryProdDescrInParam.CurePK
    FROM tblPurchOrd INNER JOIN qryProdDescrInParam ON tblPurchOrd.PurchOrdPK = qryProdDescrInParam.PurchOrdFK
    WHERE (((tblPurchOrd.PurchOrdPK)=IIf(Eval('[Forms]![frmInventory]![cmbPO]')<>"",Eval('[Forms]![frmInventory]![cmbPO]'),tblPurchOrd.PurchOrdPK)) And ((tblPurchOrd.CustFK)=Eval('[Forms]![frmInventory]![cmbCust]')) And ((qryProdDescrInParam.SpeciesPK)=IIf(Eval('[Forms]![frmInventory]![cmbSpecies]')<>"",Eval('[Forms]![frmInventory]![cmbSpecies]'),qryProdDescrInParam.SpeciesPK)) And ((qryProdDescrInParam.GradePK)=IIf(Eval('[Forms]![frmInventory]![cmbGrade]')<>"",Eval('[Forms]![frmInventory]![cmbGrade]'),qryProdDescrInParam.GradePK)) And ((qryProdDescrInParam.DimensionPK)=IIf(Eval('[Forms]![frmInventory]![cmbDim]')<>"",Eval('[Forms]![frmInventory]![cmbDim]'),qryProdDescrInParam.DimensionPK)) And ((qryProdDescrInParam.CurePK)=IIf(Eval('[Forms]![frmInventory]![cmbCure]')<>"",Eval('[Forms]![frmInventory]![cmbCure]'),qryProdDescrInParam.CurePK)));
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I should have looked into it some more and passed the correct argument to the Eval() function. I glossed over it when I added the Eval function to the SQL and it retrieved the correct records. Thanks Paul. This may be the best approach of all the options.

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Itsme,

    I didn't know for sure, but I saw that function recently in a response to someone who had trouble getting the form value.
    When I saw Paul's comment about Eval(),I recalled it, searched it down again, and suggested it.
    Glad it worked for you.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Filter form with parameterized query
    By wnicole in forum Access
    Replies: 3
    Last Post: 10-03-2013, 12:33 PM
  2. Parameterized Queries in Access 2010
    By Raptor_45 in forum Queries
    Replies: 1
    Last Post: 02-05-2013, 07:36 PM
  3. open a report using VBA from dynamic query?
    By haggisns in forum Programming
    Replies: 3
    Last Post: 10-15-2010, 08:45 AM
  4. Form with parameterized query
    By ngruson in forum Forms
    Replies: 3
    Last Post: 08-11-2010, 11:08 AM
  5. Count distinct records in parameterized query
    By SilverSN95 in forum Access
    Replies: 5
    Last Post: 07-27-2010, 09:31 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