title: SQL instruction in MS Access VBA
body: <p>The Chr(34) idea gives</p>
  <pre><code>MsgBox1 SELECT * FROM Emp WHERE JobNumber = BFLA
  MsgBox2 &quot;SELECT * FROM Emp WHERE JobNumber = BFLA&quot;
  </code></pre>
  <p>which looks as though it should work in OpenRecordset but gives the &quot;can't find table&quot; message for &quot;'SELECT....BFLA'&quot;</p>
  <p>The '&quot; &amp; xx &amp; &quot;'&quot; idea gives</p>
  <pre><code>MsgBox1 SELECT * FROM Emp WHERE JobNumber = 'BFLA'
  MsgBox2 &quot;SELECT * FROM Emp WHERE JobNumber = 'BFLA'&quot;
  </code></pre>
  <p>which also gives the &quot;can't find table&quot; message for &quot;'SELECT....'BFLA'&quot; (extra single quote before BFLA)</p>
  <p>The original code which worked was:</p>
  <pre><code>xx = Me![JobID]
  
  Set r1 = db.OpenRecordset(&quot;Select * from Emp where EmployeeID = &quot; &amp; Nz(Me![JobID]))
  </code></pre>
  <p>In this code EmployeeID was a numerical value</p>
  <p>The latest code is:</p>
  <pre><code>xx = Me![JobNumber] ' the JobNumber from the Combo box which is tested in MsgBox 1 and is correct
  
  xxSQL = &quot;SELECT * FROM Emp WHERE JobNumber = '&quot; &amp; xx &amp; &quot;'&quot;
  
  MsgBox xxSQL, vbOKOnly
  
  xxSQL = Chr(34) &amp; xxSQL &amp; Chr(34)
  
  MsgBox xxSQL, vbOKOnly ' which shows what will go into the OpenRecordset
  
  Set r1 = CurrentDb.OpenRecordset(xxSQL)
  </code></pre>
  <p>and sorry about the poor formatting last time, hopefully I've got the hang of the message box...</p>
  <p>Chris</p>
  <p><em><strong><strong>Original message below</strong></strong></em></p>
  <p>I am trying to fix some existing Access code because a source application has been changed and I'm really struggling to get a variable into a Select statement into SQL.  I've read many solved problems that are similar to this and tried many permutations of &quot;&quot;&quot; before, after and in the middle of this Select, and tried splitting it up.</p>
  <pre><code>xx = Me![JobNumber]
  xxSQL = &quot;SELECT * FROM Emp WHERE JobNumber = &quot; &amp; xx
  MsgBox xxSQL, vbOKOnly
  xxSQL = &quot;&quot;&quot; &amp; xxSQL &amp; &quot;&quot;&quot;
  MsgBox xxSQL, vbOKOnly
  Set r1 = CurrentDb.OpenRecordset(xxSQL)
  </code></pre>
  <p>The JobNumber is now a 4char string that cycles through various jobs from a Combo Box and produces a report page for each job.  The first MsgBox generates SELECT * FROM Emp WHERE JobNumber = BFLA (a valid code)</p>
  <p>Table Emp exists and has a field JobNumber which also contains 4char strings (including BFLA) which match the Me! Combo box.</p>
  <p>The second MsgBox produces &quot;&amp; xxSQL &amp;&quot; and then an Access error message - can't find the input table or query &quot;'&amp; xxSQL &amp;&quot;' because I can't get the number and placement of the double quotes right.</p>
  <p>The report then generates, repeating the 2 MsgBoxes as it cycles through the Combo box list, and the reports are fine except for missing the content from the Emp table.  There have been no changes to the Emp table other than the introduction of JobNumber, and no changes to the code following the Set r1.</p>
  <p>I fear that this is a 2-sec fix for someone with more experience than I, and it will be embarrassing, but I just can't fix it !</p>
  <p>Thanks</p>
  <p>Chris</p>