Ms access 插入到语法问题中
我想我可能已经解决了这个插入sql表的问题。我一直遇到一个语法错误问题,它突出显示了整个QDF.SQL=“INSERT-INTO…”部分。我正在尝试解决我做错的问题。有没有专家能发现我的问题Ms access 插入到语法问题中,ms-access,vba,Ms Access,Vba,我想我可能已经解决了这个插入sql表的问题。我一直遇到一个语法错误问题,它突出显示了整个QDF.SQL=“INSERT-INTO…”部分。我正在尝试解决我做错的问题。有没有专家能发现我的问题 Public Function Update() Dim cdb As DAO.Database, qdf As DAO.QueryDef Dim rs As Recordset Dim err As DAO.Error Const DestinationTableName = "AC_CDData"
Public Function Update()
Dim cdb As DAO.Database, qdf As DAO.QueryDef
Dim rs As Recordset
Dim err As DAO.Error
Const DestinationTableName = "AC_CDData"
Const ConnectionString = _
"ODBC;" & _
"Driver={SQL Server Native Client 10.0};" & _
"Server=GAALPSVR031B\P003,49503;" & _
"Database=DB;" & _
"UID=ID;" & _
"PWD=PW;"
Set cdb = CurrentDb
Set qdf = cdb.CreateQueryDef("")
Set rs = CurrentDb.OpenRecordset("CDData", dbOpenTable)
qdf.Connect = ConnectionString
Do While Not rs.EOF
qdf.SQL = "INSERT INTO ac_cddata_1(EmployeeID, EmployeeName, Region, District, Function1, Gender, EEOC, Division, Center, MeetingReadinessLevel, ManagerReadinessLevel, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3, DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, JobGroupCode, JobDesc, JobGroup) " & _
"Values (" & _
"'" & rs.EmployeeID & "', " & _
"'" & rs.EmployeeName & "', " & _
"'" & rs.Region & "', " & _
"'" & rs.District & "', " & _
"'" & rs.Function1 & "', " & _
"'" & rs.Gender & "', " & _
"'" & rs.EEOC & "', " & _
"'" & rs.Division & "', " & _
"'" & rs.Center & "', " & _
"'" & rs.MeetingReadinessLevel & "', " & _
"'" & rs.ManagerReadinessLevel & "', " & _
"'" & rs.EmployeeFeedback & "', " & _
"'" & rs.DevelopmentForEmployee1 & "', " & _
"'" & rs.DevelopmentForEmployee2 & "', " & _
"'" & rs.DevelopmentForEmployee3 & "', " & _
"'" & rs.DevelopmentForEmployee4 & "', " & _
"'" & rs.DevelopmentForEmployee5 & "', " & _
"'" & rs.Justification & "', " & _
"'" & rs.Changed & "', " & _
"'" & rs.JobGroupCode & "', " & _
"'" & rs.JobDesc & "', " & _
"'" & rs.JobGroup"')"
qdf.ReturnsRecords = False
On Error GoTo Update_qdfError
qdf.Execute dbFailOnError
On Error GoTo 0
rs.MoveNext
Loop
rs.Close
Set qdf = Nothing
Set cdb = Nothing
Set rs = Nothing
Exit Function
Update_qdfError:
For Each err In DAO.Errors
MsgBox err.Description, vbCritical, "Error " & err.Number
Next
End Function
您试图用
rs.fieldName
引用记录集中的字段,但这不是正确的方法。正确的方法是:
rs![EmployeeId]
(如果字段名没有空格,则方括号是可选的)
另外,正如HansUp在回答中指出的那样,您错过了最后一个符号,…您忘记将最后一个
&
放在字符串的最后一行:“'&rs.JobGroup””)“
应该是“'&rs.JobGroup&”)“我不能对提问发表评论,但从您的陈述来看,EmployeeId
我假设是整数类型。如果ID是整数,请尝试:
qdf.SQL = "INSERT INTO ac_cddata_1(EmployeeID, EmployeeName, Region, District, Function1, Gender, EEOC, Division, Center, MeetingReadinessLevel, ManagerReadinessLevel, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3, DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, JobGroupCode, JobDesc, JobGroup) " & _
"Values (" & _
& rs.EmployeeID & ", " & _
"'" & rs.EmployeeName & "', " & _
"'" & rs.Region & "', " & _
"'" & rs.District & "', " & _
"'" & rs.Function1 & "', " & _
"'" & rs.Gender & "', " & _
"'" & rs.EEOC & "', " & _
"'" & rs.Division & "', " & _
"'" & rs.Center & "', " & _
"'" & rs.MeetingReadinessLevel & "', " & _
"'" & rs.ManagerReadinessLevel & "', " & _
"'" & rs.EmployeeFeedback & "', " & _
"'" & rs.DevelopmentForEmployee1 & "', " & _
"'" & rs.DevelopmentForEmployee2 & "', " & _
"'" & rs.DevelopmentForEmployee3 & "', " & _
"'" & rs.DevelopmentForEmployee4 & "', " & _
"'" & rs.DevelopmentForEmployee5 & "', " & _
"'" & rs.Justification & "', " & _
"'" & rs.Changed & "', " & _
"'" & rs.JobGroupCode & "', " & _
"'" & rs.JobDesc & "', " & _
"'" & rs.JobGroup"')"
否则,错误说明了什么?您在构建字符串时出错
“'”&rs.JobGroup“)”
换成这个
“'”&rs.JobGroup&“)”
使用字符串变量保存SQL语句通常更容易发现问题
Dim strInsert作为字符串插入
strInsert=“插入到…”
然后你可以检查字符串
Debug.Print strInsert
最后,将字符串指定给您的querydef
qdf.SQL=strInsert
我刚刚做了更改(我的人都不需要[]),但我得到了相同的语法错误。它们都是文本类型,解决了这个问题。现在我得到了一个类型不匹配错误,它突出显示了集RS=CURRENTDB.OPENRECORDSET(“CDDATA”,“DBOPENTABLE”)然后尝试:集RS=CURRENTDB().OPENRECORDSET(“CDDATA”,dbOpenDynaset)
不要在DBOPENTABLE周围使用引号。它是一个常量的名称,因此不应该被引用。使用以下声明:Dim rs As DAO.Recordset
这是有效的,有没有办法在插入发生后从CDDATA表中删除数据,或者我需要将其作为追加查询?