Hi Colin,
I "skimmed" the text of that article in the link, and may adopt that policy of deleting temporary objects.
=====================================
My main purpose, and the reason for finding this old thread is that I am trying to recreate the functionality of an old utility called SQL2VAR.EXE that I used in the Access 2.0/97 days.
I had found a newer utility included with IDBE tools, that did this too ... but have discovered that it no longer works with Access 2016.
I combined a couple of ideas and code snippets to design a form containing a list-box that is populated by the code above. Allen Brown had instructions for creating a tab control with 2 text-boxes. One for the SQL string which he was copy/pasting from the SQL view of a query, and the other tab/textbox would contain another textbox to contain the generated VBA string. Again, this was intended to be copy/pasted into the code window. Ugh.
So I worked on my idea of combining the two, and came up with this: (Still far from the functionality of the good old SQL2VAR utility.)
==== Code ==========
Private Sub cmdGenereateSQL_Click()
Dim frm As Form, ctl As Control
Dim varItm As Variant
Dim strQuery As String
Dim strSQL As String
Set frm = Me
Set ctl = frm!lbxQueries
For Each varItm In ctl.ItemsSelected
strQuery = ctl.ItemData(varItm)
'Debug.Print strQuery
Next varItm
Me.txtSQL = CurrentDb.QueryDefs(strQuery).SQL
'Now run Allen Browne's code.
'Purpose: Convert a SQL statement into a string to paste into VBA code.
Const strcLineEnd = " "" & vbCrLf & _" & vbCrLf & """"
If IsNull(Me.txtSQL) Then
Beep
Else
strSQL = Me.txtSQL
strSQL = Replace(strSQL, """", """""") 'Double up any quotes.
strSQL = Replace(strSQL, vbCrLf, strcLineEnd)
strSQL = "strSql = """ & strSQL & """"
Me.txtVBA = strSQL
Me.txtVBA.SetFocus
RunCommand acCmdCopy
End If
End Sub
'=========end code =============
The VBA generated is not NEAR as neat, and I haven't yet tested it.
BTW, my "post count" is still below the threshold of 10, so I was not able to post links or use code tags.
(This policy seems unnecessary to me, but hey ...)