Results 1 to 12 of 12
  1. #1
    CraigInOz is offline Novice
    Windows 11 Access 2021
    Join Date
    Mar 2025
    Location
    Brisbane Australia
    Posts
    4

    Cannot pass parameter to query from VBA

    Using the Immediate Window I can see that the variables are set in VBA, but nothing is being passed to the Query. Every time the query is opened, a parameter pop-up window is displayed. Here is my code...


    Code:
        Dim dbs As Database
        Dim qdf As QueryDef
        Dim code As Integer
        Set dbs = CurrentDb()
        Set qdf = dbs.QueryDefs![qRptActivityWorksheet_All]
          
        code = 22
        qdf.Parameters![ActivityCode] = code
        DoCmd.OpenQuery ("qRptActivityWorksheet_All")
    
    
    ' tested with these variants on syntax
    '    Dim dbs As DAO.Database
    '    Dim qdf As DAO.QueryDef
    '    Set qdf = dbs.QueryDefs("qRptActivityWorksheet_All")
    '    qdf.Parameters("ActivityCode").Value = 44
    '    qdf.Parameters![ActivityCode].Value = 44
    '    qdf.Parameters("ActivityCode").Value = code
    '    qdf!ActivityCode = code
    Here is the SQL for the query:
    PARAMETERS ActivityCode Long;SELECT qfrmCon_DetailMemNewest_S2.MemNameForCard, etc.


    I would be very gratefully for some advice.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    I would suggest to edit the SQL property of the QueryDef object instead:
    Code:
    Dim dbs As Database
    Dim qdf As QueryDef
    Dim code As Integer
    
    
    Set dbs = CurrentDb()
    Set qdf = dbs.QueryDefs![qRptActivityWorksheet_All]
    qdf.SQL="SELECT .... FROM... WHERE.. [ActivityCode] = " & code & " AND .....;"
    .....
    https://learn.microsoft.com/en-us/of...l-property-dao
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    you can try this:
    Code:
        Dim code As Integer
          
        code = 22
        DoCmd.SetParameter "ActivityCode", code
        DoCmd.OpenQuery ("qRptActivityWorksheet_All")

  5. #5
    CraigInOz is offline Novice
    Windows 11 Access 2021
    Join Date
    Mar 2025
    Location
    Brisbane Australia
    Posts
    4
    Thank you everyone, I shall give these a try. One more wrinkle, this query is used by a report, it is the data source for the report. So running the SQL inside VBA is not going to work. The DoCmd.SetParameter I hope will update the query and then the report will call it. Thanks again.

  6. #6
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    you can use the query in your report:
    Code:
        Dim code As Integer
          
        code = 22
        DoCmd.SetParameter "ActivityCode", code
        'DoCmd.OpenQuery ("qRptActivityWorksheet_All")
        DoCmd.OpenReport "RptActivityWorksheet_All", acViewPreview

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    If query is RecordSource of report, why would you even need to open query? Purpose of parameter is to filter query? Why use parameterized query? Why not apply filter to report with WHERE CONDITION argument of OpenReport?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    CraigInOz is offline Novice
    Windows 11 Access 2021
    Join Date
    Mar 2025
    Location
    Brisbane Australia
    Posts
    4
    The DoCmd.SetParameter worked perfectly. I am going to hit the VBA/Access websites. I have dozens of queries in this database. How does the DoCmd know which query had THAT parameter? This code seemed intuitive.

    Code:
        Dim qdf As QueryDef
        Set qdf = dbs.QueryDefs![qRptActivityWorksheet_All]
        qdf.Parameters![ActivityCode] = code
    Point to the query, and then to the parameter in the query. The DoCmd just points to the parameter. What if I had used the same parameter name in another query? I gotta research how Access handles this. Again, Thank you. It is working as I need it to.

  9. #9
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    here is an explanation from ms:
    DoCmd.SetParameter method (Access) | Microsoft Learn

    if your Form/Report is using Query that has parameter,
    you SetParameters first for all the query parameters then
    open the Form/Report.

    once the Form/Report opens, the Parameter collection is cleared.
    so if you are going to open another form/report that uses query
    that has parameters, you again set up the parameters using
    DoCmd.SetParameters before opening the Form/Report.

  10. #10
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    June's answer is on the money. That whole passing a parameter to a query was a huge red herring, because when you open a report, you can pass parameter values to the underlying query that the report is based on. So remove the parameters from the query and maybe the entire WHERE clause. Then pass the filter when you open the report. It's a whole lot more flexible that way. If there are filters you always apply, you can leave them in the query the report is based on, but not a great idea, because you want the report to be super flexible that you can apply any filter to when you open the report.

  11. #11
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Often, what works to eliminate code failing over parameters is to use a variable as a go-between between the parameter in the vba generated sql and form controls. Code without defined parameters may not be able to resolve Me.txtCustomerID but it should resolve
    Dim lngCustID As Long

    lngCustID = Me.txtCustID

    " ... WHERE tblCustomer.CustID = " & lngCustID & " AND ... "
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    CraigInOz is offline Novice
    Windows 11 Access 2021
    Join Date
    Mar 2025
    Location
    Brisbane Australia
    Posts
    4
    I took the advice of removing the parameter from the query and building the necessary "where" statement for the OpenReport. Works like a charm. Thank you everyone.

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

Similar Threads

  1. Pass a variable to a Pass Through Query
    By violeta_barajas in forum Access
    Replies: 2
    Last Post: 01-26-2017, 07:59 PM
  2. Replies: 5
    Last Post: 04-27-2015, 02:40 PM
  3. Replies: 7
    Last Post: 03-11-2015, 12:48 PM
  4. Pass parameter to a VBA Module
    By testing2Three in forum Modules
    Replies: 4
    Last Post: 11-13-2014, 02:33 PM
  5. Replies: 2
    Last Post: 10-20-2014, 08:11 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