How do I pass a parameter in VB from a form to a query used in OpenReport?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AJ Dreh

    #1

    How do I pass a parameter in VB from a form to a query used in OpenReport?

    I have the login name on my form, Ulogin. The Report I call with OpenReport (from VB) uses a parameter, ulog, for the query it based upon. I pass Ulogin in a DOcmd.OpenRepor t using the where clause Ulog = ULogin. My report still prompts me for Ulog. I want to pass the login value to the parameter in the report such that no dialog box is displayed and the report runs automatically. What is the correct syntax for this or do I need to do this differently?
    Thank you
  • AJ Dreh

    #2
    I tried loading the parameter value in the OpenArgs portion of the OpenReport. I checked it in Debug and it was correct. I loaded the correct value into the parameter value in the Open event for the report. Why does the parameter dialog box continue to prompt me for the login name - even though I successfully loaded it in the open event? My VB code said: ulog = Me.OpenArgs. Do I need to load it differently if it is a parameter?
    Thank you for your help

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      You can specify the Criteria in the Where Argument of the OpenReport Method, as in:
      Code:
      Dim strWhere As String
      
      strWhere = "[LastName] = '" & Me![txtLastName] & "'"
      
      DoCmd.OpenReport "Employees", acViewPreview, , strWhere, acWindowNormal

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        AJ, if the query the report is run on has a parameter to be entered there is no way to pass that value to it in code.

        To use code to provide the filtering the query itself must be redesigned. The query would be designed to view all records, then the opening would include a filter specification (actually called the WhereCondition parameter of the OpenReport() method) that ensured only the selected record was shown. Does that make it clearer?
        Last edited by NeoPa; Oct 22 '10, 01:17 PM.

        Comment

        • AJ Dreh

          #5
          Thank you. I figured out how to do it. The report was based on a query which contained the parameter. Instead of a parameter, I put where the query should find the value to use which is in a form. I used [Forms]![table]![field] and it worked great.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            That's certainly another way to redesign the query. Not my preference but Ibelieve it's popular amongst many.

            Comment

            Working...