Microsoft Visual Basic - 运行时错误“3075:查询表达式“1st”、“A”、1-Jan-15'、“1”中的语法错误(缺少运算符))
Posted
技术标签:
【中文标题】Microsoft Visual Basic - 运行时错误“3075:查询表达式“1st”、“A”、1-Jan-15\'、“1”中的语法错误(缺少运算符))【英文标题】:Microsoft Visual Basic - Run-time error '3075: Syntax error (missing operator) in query expression "1st", 'A', 1-Jan-15', '1')'Microsoft Visual Basic - 运行时错误“3075:查询表达式“1st”、“A”、1-Jan-15'、“1”中的语法错误(缺少运算符)) 【发布时间】:2016-11-02 03:01:19 【问题描述】:我正在尝试将 50,000 条记录添加到 Microsoft Access 中的 Tournament_Result 表中,但我遇到了以下错误:运行时错误 '3075:查询表达式“1st”、“A”、1 中的语法错误(缺少运算符) -Jan-15', '1')'。
Option Compare Database
Option Explicit
Sub arrayData()
Dim TournamentResult() As Variant
Dim DivisionEntered() As Variant
Dim DateOfTournament() As Variant
Dim num As Long, TournamentResultNo As Long, MembershipNo As Long, dbs As Database, InsertRecord As String
Dim num1 As Long 'we need to declare num1 as an Integer to create a loop
Dim TournamentResultDescription As String, DivisionEnteredDescription As String, DateOfTournamentDescription As String
Set dbs = CurrentDb()
TournamentResultNo = 0
MembershipNo = 0
TournamentResultNo = TournamentResultNo + 1
TournamentResult = Array("1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th")
DivisionEntered = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
DateOfTournament = Array("1-Jan-15", "2-Feb-15", "3-Mar-15", "4-Apr-15", "5-May-15", "6-Jun-15", "7-July-15", "8-Aug-15", "9-Sep-15", "10-Oct-15", "11-Nov-15", "12-Dec-15")
MembershipNo = MembershipNo + 1
For num1 = 0 To 50000
num = Int((50000 - 0 + 1) * Rnd + 0)
TournamentResultDescription = TournamentResult(num)
DivisionEnteredDescription = DivisionEntered(num)
DateOfTournamentDescription = DateOfTournament(num)
InsertRecord = "insert into TOURNAMENT_RESULT(TournamentResultNo, TournamentResult, DivisionEntered, DateOfTournament, MembershipNo) values (" & "'" & TournamentResultNo & "'" & "," & "'" & TournamentResultDescription & "'" & "'" & "," & "'" & DivisionEnteredDescription & "'" & "," & "'" & DateOfTournamentDescription & "'" & "," & "'" & MembershipNo & "'" & ")"
dbs.Execute InsertRecord
Debug.Print TournamentResultNo; TournamentResultDescription; DivisionEnteredDescription; DateOfTournamentDescription; MembershipNo
Next
End Sub
编辑:dbs.Execute InsertRecord
突出显示为问题
【问题讨论】:
请使用参数化查询。 参数化查询是什么意思? 阅读here 删除其中之一:& "'" & "'"
。您的查询没有平衡引号:values ('',''','','','')
.
您的引号字符严重不匹配,缺少单引号以及不应在您发布的错误消息中使用的引号。它不是实际的消息,或者您发布的代码不是您的实际代码。如果您学会使用参数化查询,您将不必担心正确配对和转义引号字符。您还可以保护自己免受 SQL 注入。
【参考方案1】:
评论者的建议是正确的。
由于串联字符串,这些类型的动态语句可能难以调试 - 额外串联分隔符和逗号会使情况变得更糟。
在我看来,您的 SQL 中好像多了一个单引号 ("'")。
试试:
InsertRecord = _
"insert into TOURNAMENT_RESULT(TournamentResultNo, TournamentResult, DivisionEntered, DateOfTournament, MembershipNo) values ('" & _
TournamentResultNo & "','" & TournamentResultDescription & "','" & _
DivisionEnteredDescription & "','" & DateOfTournamentDescription & _
"','" & MembershipNo & "')"
【讨论】:
【参考方案2】:这可能就是你所追求的:
Public Function InsertRange()
Const Results As Long = 50000
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim Num101 As Integer
Dim Num102 As Integer
Dim Num12 As Integer
Dim ResultNo As Long
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("Select Top 1 * From TOURNAMENT_RESULT")
Randomize
For ResultNo = 1 To Results
Num101 = 1 + Int(10 * Rnd)
Num102 = 1 + Int(10 * Rnd)
Num12 = 1 + Int(12 * Rnd)
rst.AddNew
rst!TournamentResultNo.Value = ResultNo
rst!TournamentResult.Value = CStr(Num101) & "st"
rst!DivisionEntered.Value = Chr(64 + Num102)
rst!DateOfTournament.Value = DateSerial(2015, Num12, Num12)
rst!MembershipNo.Value = ResultNo
rst.Update
Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
End Function
【讨论】:
以上是关于Microsoft Visual Basic - 运行时错误“3075:查询表达式“1st”、“A”、1-Jan-15'、“1”中的语法错误(缺少运算符))的主要内容,如果未能解决你的问题,请参考以下文章
辉煌不再!Microsoft计划结束Visual Basic编程语言
如何通过 Winsock 从外部设备检索数据到 Microsoft Visual Basic (VB6)
如何将 Visual Basic 6 组合框与 Microsoft Access 2007 表中的字段链接
Microsoft Visual Basic - 运行时错误“3075:查询表达式“1st”、“A”、1-Jan-15'、“1”中的语法错误(缺少运算符))
我将如何防止用户进入数据库中不存在的记录(使用 Microsoft DAO 2.5/3.51 的 Visual basic 6.0)