A family of Microsoft relational database management systems designed for ease of use.
What are you attempting to do here with the SQL statement? If you want to open it as a datasheet at each iteration of the loop, the following function will do this:
Public Function OpenTempQuery(strSQL As String)
Dim qdf As DAO.QueryDef
Static n As Integer
n = n + 1
' delete temporary querydef object if exists
On Error Resume Next
CurrentDb.QueryDefs.Delete "Temp" & n
Select Case Err.Number
Case 0
' no error
Case 3265
' temporary querydef does not exist
' ignore error
Case Else
' unknown error
MsgBox Err.Number, vbExclamation, "Error"
End Select
' create temporary querydef object
Set qdf = CurrentDb.CreateQueryDef("Temp" & n)
CurrentDb.QueryDefs("Temp" & n).SQL = strSQL
' open query and then delete temporary querydef object
DoCmd.OpenQuery "Temp" & n
' delete temporary querydef object
CurrentDb.QueryDefs.Delete "Temp" & n
End Function
Note that the variable n is declared as Static, which means the function will open multiple datasheets if run more than once without closing the previous datasheet. They'll be named Temp1, Temp2 etc.
So, having added the function to a standard module, instead of:
DoCmd.RunSQL SQLStr1 & SQLStr2 & SQLStr3
you'd use:
OpenTempQuery SQLStr1 & SQLStr2 & SQLStr3