查询表达式中的语法错误(缺少运算符)“

Syntax Error (Missing Operator) In Query Expression "

尝试编写一个 SQL 查询,其中我使用内部联接从一个 table 获取数据并在另一个 table 中显示匹配的内容,但是我收到错误'Syntax error (missing operator) in query expression "'。不确定我哪里出了问题,但是我插入了我正在使用的代码,它看起来是正确的。

顺便说一句,代码是作为 VB 程序的一部分编写的,因此 SQL 代码旁边还有额外的代码。

Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [classes] INNER JOIN [students] ON classes.StudentForename = students.Forename AND ON classes.StudentSurname = students.Surname AND ON classes.TeacherName ='" & personloggedon.Text & "' AND ON classes.Day ='" & System.DateTime.Now.DayOfWeek.ToString & "' AND ON classes.Period ='" & attendance_reg_periodComboBox.Text & "'", con)

我也尝试过使用下面的代码,但还是不行。

Dim cmd as OleDbCommand = New OleDbCommand("SELECT [Forename], [Surname] FROM [classes] INNER JOIN [students] ON classes.StudentForename = students.Forename AND ON classes.StudentSurname = students.Surname AND ON classes.TeacherName ='" & personloggedon.Text & "' AND ON classes.Day ='" & System.DateTime.Now.DayOfWeek.ToString & "' AND ON classes.Period ='" & attendance_reg_periodComboBox.Text & "'", con)

我的所有 SQL 查询需要做的就是从 "classes" 数据库中获取名字和姓氏,只要它们符合给定的标准,然后从中获取给定的名字和姓氏所述数据库并匹配 "students" 数据库。然后,它会显示在我程序的数据网格视图中。

我对SQL编码比较陌生,所以如果错误特别明显,我提前道歉!但是,非常感谢所有帮助。

您的 ON 过多。将所有 AND ON 替换为 AND.

Dim cmd as OleDbCommand = New 
OleDbCommand("SELECT [Forename], [Surname] FROM 
[classes] INNER JOIN [students] ON 
classes.StudentForename = students.Forename AND 
classes.StudentSurname = students.Surname WHERE 
classes.TeacherName ='" & personloggedon.Text & "' 
AND  classes.Day ='" & 
System.DateTime.Now.DayOfWeek.ToString & "' AND 
classes.Period ='" & 
attendance_reg_periodComboBox.Text & "'", con)

您真的想看看使用参数化查询来避免注入攻击。

ON 每个连接仅使用一次。

Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [classes] INNER JOIN [students] ON classes.StudentForename = students.Forename AND classes.StudentSurname = students.Surname AND classes.TeacherName ='" & personloggedon.Text & "' AND classes.Day ='" & System.DateTime.Now.DayOfWeek.ToString & "' AND classes.Period ='" & attendance_reg_periodComboBox.Text & "'", con)

虽然调用 SQL 的方式非常不寻常(而且很乱)。很难确定您是否还有其他问题。如果它不起作用,请告诉我,我们会一起解决。


额外提示: 调试 VBA SQL 字符串

当您在包含(或引用)复杂 SQL 字符串的行上遇到 VBA run-time 错误时,可能很难准确确定问题出在哪里。

与对任何代码进行故障排除一样,该过程是简化,以消除可能的罪魁祸首。

When attempting to execute the following procedure, we will get a run-time error on the line with the OpenRecordSet method:

Public Function SQLTest()
    Dim strSQL As String
    strSQL = "SELECT * FROM tblMain" & _
                "ORDER BY [TableID]"
    Debug.Print strSQL
    CurrentDb.OpenRecordset strSQL
End Function

To help identify the problem, we can modify the code slightly by adding a single line that will print the value of the SQL string to the Immediate Window.

(Use Ctrl+G to view the immediate window.)

Public Function SQLTest()
    Dim strSQL As String
    strSQL = "SELECT * FROM tblMain" & _
                "ORDER BY [TableID]"
    Debug.Print strSQL
    CurrentDb.OpenRecordset strSQL
End Function

Now, take a look at the output (in the immediate window):

SELECT * FROM tblMainORDER BY [TableID]
                    ↗↖

It's easy to see that we are missing a space between the table name and the ORDER BY clause, which is simple enough to fix:

strSQL = "SELECT * FROM tblMain " & _
     "ORDER BY [TableID]"
  • This is the possibly the single most helpful debugging technique for troubleshooting SQL statements in VBA.
  • 有关更多调试技巧,请参阅 Source

更多信息:


编辑:

试试这个来解决问题:

Debug.Print "SELECT * FROM [classes] INNER JOIN [students] ON classes.StudentForename = students.Forename AND classes.StudentSurname = students.Surname AND classes.TeacherName ='" & personloggedon.Text & "' AND classes.Day ='" & System.DateTime.Now.DayOfWeek.ToString & "' AND classes.Period ='" & attendance_reg_periodComboBox.Text & "'"  
Stop

将该代码放在问题行之前。当代码 Stops 按下 Ctrl+G 查看即时 window.

将 SQL 复制并粘贴到新查询 Window(在 SQL 视图中)并尝试更改为设计模式。看看您是否可以找出那里的错误。