title: Tracking down an invalid procedure call error in a SQL VBA execute command
body: <p>Thanks for opening this. I've been wracking my brain for the past couple of days trying to make heads of tails of this error (Run-time error '5': Invalid procedure call) error that has blocked me at every turn. </p>
  
  <p>Essentially, my goal is to run and execute a SQL command to find the largest number in a given category and store it into a variable for later use. I've tried different ways to go about it and no matter how I do it (either in VBA using a DMAX or through SQL) I run into the same error whenever I try to execute the command. I've gotten some help working through it but I think there's some deeper issue that I am not understanding with VBA.</p>
  
  <p>Here is the code:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Public Function GetMaxValue(Child As String)
  
      Dim RAC As DAO.RecordSet
      Dim Prefix As String
      Dim MaxVal As Long
      Dim SearchString As String
  
      NewPrefix = Child
      SearchString = "SELECT MAX([SalesValue]) FROM [SalesTable] WHERE [Prefix] = '" &amp; NewPrefix &amp; "';"
  
      Set RAC = CurrentDb.OpenRecordset(SearchString)
  
      If RAC.Fields.Count = 1 Then
            MaxVal = RAC.Fields(0)
      End If
  
      RAC.Close
      Set RAC = Nothing
  
  End Function
  </code></pre>
  
  <p>It breaks whenever I hit the line that reads <code>Set RAC = CurrentDb...</code> with:</p>
  
  <blockquote>
    <p>Invalid procedure call error </p>
  </blockquote>
  
  <p>Please let me know if anyone has any idea what produces this error. I've searched everywhere for a possible explanation and I can't find anything that would cause my code to break whenever I try to run a max function. I even made sure that the SalesValue was a Number field in the underlying Access table and that everything was spelled correctly.</p>
  
  <p>Thanks!</p>