Consulting

Results 1 to 3 of 3

Thread: Opening a form based on a query with VBA-set parameters

  1. #1
    VBAX Regular
    Joined
    May 2012
    Posts
    30
    Location

    Opening a form based on a query with VBA-set parameters

    I have a form in a database that runs on a query. I would like to be able to limit what records appear on the form by setting query criteria based on parameters that I pass in code. But when I pass the parameter in VBA, the form still prompts for the query parameter. How can I make the parameter "stick"?

    The code I use currently to set the parameter:

    Private Sub Command0_Click()
        'Set query parameter values
        Dim qdf As DAO.QueryDef
        
        'Set query parameter(s)
        Set qdf = CurrentDb.QueryDefs("qryBricks")
        With qdf
            .Parameters("parColor") = "*" 'Usually this would be "Green" but I'm just trying to get the darn thing to run right now.
        End With
        
        'Launch form
        DoCmd.OpenForm "frmBricks" 'At this point it asks me for the parameter even though I've already set it.
    End Sub
    Any help would be appreciated.

    Thank you,

    Dan

    Attachment 11609
    Attached Files Attached Files

  2. #2
    VBAX Regular GP George's Avatar
    Joined
    Jul 2004
    Location
    Puget Sound, WA
    Posts
    26
    Location
    The problem is that setting the parameter for the query in this procedure has no effect on the query as the recordsource for the called form. It is not persistent between the two instances of the query. There is more than one way to filter the recordset for the called form. You can either use a combo or list box on your calling form to select the value you want to use for the filter and reference that form control in the query instead of the parameter currently in it. Or you can pass that value to the called form via OpenArgs and then, in the open event of the called form, rewrite the SQL in the query dynamically to use that OpenArgs value.
    "We're all in this together."
    -Red Green

  3. #3
    VBAX Regular
    Joined
    May 2012
    Posts
    30
    Location
    Quote Originally Posted by GP George View Post
    The problem is that setting the parameter for the query in this procedure has no effect on the query as the recordsource for the called form. It is not persistent between the two instances of the query. There is more than one way to filter the recordset for the called form. You can either use a combo or list box on your calling form to select the value you want to use for the filter and reference that form control in the query instead of the parameter currently in it. Or you can pass that value to the called form via OpenArgs and then, in the open event of the called form, rewrite the SQL in the query dynamically to use that OpenArgs value.
    Thanks, I think I can use one of these methods to solve the problem. What you said made sense--that they are different instances of the query.

    Much appreciated!

    Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •