title: Opening Form From Report
body: <p>I have the following lines of code in 2 different VBA functions in a Module they both have the same aim of opening a form to a specific record;</p>
  
  <pre><code>stLinkCriteria = "[ID]=" &amp; Reports![Rpt_Manufacture].[ID]
  DoCmd.OpenForm stDocName, , , stLinkCriteria
  </code></pre>
  
  <p>and</p>
  
  <pre><code>stLinkCriteria = "[ID]=" &amp; Forms![frmManufactureList]![frm_Products].[ID]
  DoCmd.OpenForm stDocName, , , stLinkCriteria
  </code></pre>
  
  <p>How can I change this so I only have one function that I can call from reports or forms and it will open the form to a specific record. I have tried the me! version on reports but I get an 'Invalid use of Me keyword' which I guess is because I can not use it from a Module.</p>
  
  <p>UPDATE #1
  Based on the answer below by Thomas G I used this code;</p>
  
  <pre><code>Option Compare Database
  
  Public Function CmdOpenProductDetails(ByRef theObject As Object)
  On Error GoTo Err_CmdOpenProductDetails
  
      Dim stDocName As String
      Dim stLinkCriteria As String
  
      stDocName = "FrmProductDetails"
  
      stLinkCriteria = "[ProductID]=" &amp; theObject![ProductID]
      DoCmd.OpenForm stDocName, , , stLinkCriteria
  
  Exit_CmdOpenProductDetails:
      Exit Function
  
  Err_CmdOpenProductDetails:
      MsgBox Err.Description
      Resume Exit_CmdOpenProductDetails
  
  End Function
  </code></pre>
  
  <p>And from the Form and Report for the product names I have an onClick event that reads;</p>
  
  <pre><code>=CmdOpenProductDetails()
  </code></pre>
  
  <p>However, if I click a product name on a form I get the error message;</p>
  
  <blockquote>
    <p>The express On Click you entered as the event property setting
    produced the following error. The expression you entered has a
    function contain the wrong number of arguments. (The expression may
    not result in the name of a macro, UDF or Event Proc) (There may have
    been an error evaluating the function).</p>
  </blockquote>
  
  <p>If I click from a report I get the error message;</p>
  
  <blockquote>
    <p>MS Access cannot find the object 'CmdOpenProductDetails(). Make sure
    you have saved it and that you have typed it correctly.</p>
  </blockquote>