I have an Access database Module that draws random records from a 3 table query and inserts them into a new table
I get an SQL error regarding operator syntax when the module is run (error details to follow). The SQL code worked fine when drawing records from one table but when I modified the code to draw from 3 tables I get the message "Syntax Error:missing operator in Query expression tblTemp.CorrectSelection INTO tblRandom_June10...."
Below is a portion of the module code where the error was identified:
-------------------------------------------------------------------------------------------------------------------------------------------------------
strTableName = "tblRandom_" & Format(Date, "ddmmmyyyy")
strSQL = "SELECT Top " & TopValue & " tblTemp.SubjectID, tblTemp.SubjectName, tblTemp.QuestionID, " & _
"tblTemp.QuestionAndOrInstructions, tblTemp.MultipleAnswerID, tblTemp.MultipleAnswerDescription, " & _
"tblTemp.UserSelection, tblTemp.CorrectSelection" & _
"INTO " & strTableName & " " & _
"FROM tblTemp " & _
"ORDER BY tblTemp.RandomNumber;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Any help would be greatly appreciated-----Below is the complete module code in case it is needed to isolate the problem------Thanks SCOTT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Option Compare Database
Option Explicit
Sub PickRandom()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strTopRecords As String
Dim TopValue As Integer
TopValue = 0
strTopRecords = InputBox("Enter the desired number of Records", _
"Number of Questions", _
"1-100")
TopValue = Val(strTopRecords)
Dim strTableName As String
strSQL = "SELECT CInt(tblSubjects.SubjectID) AS SubjectID, tblSubjects.SubjectName, " & _
" CInt(tblQuestions.QuestionID) AS QuestionID, tblQuestions.QuestionAndOrInstructions, " & _
" tbleMultipleAnswers.MultipleAnswerID, tbleMultipleAnswers.MultipleAnswerDescription, " & _
" tbleMultipleAnswers.UserSelection, tbleMultipleAnswers.CorrectSelection " & _
"INTO tblTemp " & _
"FROM (tblSubjects INNER JOIN tblQuestions ON tblSubjects.SubjectID = tblQuestions.fk_SubjectID)" & _
"INNER JOIN tbleMultipleAnswers ON tblQuestions.QuestionID = tbleMultipleAnswers.fk_QuestionID;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Set db = CurrentDb()
Set tdf = db.TableDefs("tblTemp")
Set fld = tdf.CreateField("RandomNumber", dbDouble)
tdf.Fields.Append fld
Set rst = db.OpenRecordset("tblTemp", dbOpenTable)
rst.MoveFirst
Do
Randomize
rst.Edit
rst![RandomNumber] = Rnd()
rst.Update
rst.MoveNext
Loop Until rst.EOF
rst.Close
Set rst = Nothing
strTableName = "tblRandom_" & Format(Date, "ddmmmyyyy")
strSQL = "SELECT Top " & TopValue & " tblTemp.SubjectID, tblTemp.SubjectName, tblTemp.QuestionID, " & _
"tblTemp.QuestionAndOrInstructions, tblTemp.MultipleAnswerID, tblTemp.MultipleAnswerDescription, " & _
"tblTemp.UserSelection, tblTemp.CorrectSelection" & _
"INTO " & strTableName & " " & _
"FROM tblTemp " & _
"ORDER BY tblTemp.RandomNumber;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
db.TableDefs.Delete ("tblTemp")
End Sub
