Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>Eval()</code> takes the string you give it and uses the <em>"expression service"</em> to process it. The problem you're facing is the expression service doesn't know anything about VBA variables. If you're really determined, you may be able to figure out a workaround which builds the variable's <em>value</em> rather than the variable's <em>name</em> into the string you give <code>Eval()</code> to ... um ... evaluate.</p> <p>But for what you're doing, I suggest you ditch <code>Eval()</code>. Instead give the function a data structure such as a <code>Scripting.Dictionary</code> or VBA <code>Collection</code> which contains the parameter values with your former variable names as keys. </p> <p>Here is a VBA <code>Collection</code> example ...</p> <pre class="lang-vb prettyprint-override"><code>Dim MyCol As Collection Set MyCol = New Collection MyCol.Add CLng(10), "R_Yr" MyCol.Add "foo", "MyString" Debug.Print MyCol("R_Yr"), TypeName(MyCol("R_Yr")) Debug.Print MyCol("MyString"), TypeName(MyCol("MyString")) </code></pre> <p>That code gives me this output in the Immediate window ...</p> <pre class="lang-vb prettyprint-override"><code> 10 Long foo String </code></pre> <p>So consider building a similar collection in the calling code and passing that collection to a modified <code>Execute_query</code> function.</p> <pre class="lang-vb prettyprint-override"><code>Public Function Execute_query(ByVal pQdf As String, _ ByRef pCol As Collection) As Recordset Dim qdf As QueryDef Set qdf = CurrentDb.QueryDefs(pQdf) For Each prm In qdf.Parameters prm.Value = pCol(prm.Name) Next prm If (qdf.Type = 80) Then qdf.Execute Else Set Execute_query = qdf.OpenRecordset End If End Function </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload