插入语法问题

时间:2014-03-24 20:11:34

标签: vba ms-access access-vba

我想我可能已将这个插入到sql表中的问题解决了。我一直在遇到SYNTAX ERROR问题,它突出了整个QDF.SQL =“INSERT INTO .....部分。我正在尝试解决我做错的问题。那里有哪些专家可以发现我的问题是什么?” / p>

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

3 个答案:

答案 0 :(得分:1)

我无法评论提出问题,但根据您的陈述,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"')"

否则,错误说的是什么?

答案 1 :(得分:1)

构建字符串时出错...

"'" & rs.JobGroup"')"

将其更改为此...

"'" & rs.JobGroup & "')"

通过使用字符串变量来保存SQL语句,通常更容易发现问题。

Dim strInsert As String
strInsert = "INSERT INTO ..."

然后你可以检查字符串......

Debug.Print strInsert

最后将字符串分配给querydef ...

qdf.SQL = strInsert

答案 2 :(得分:0)

您尝试使用rs.fieldName引用记录集中的字段,这不是正确的方法。正确的方法是:

rs![EmployeeId]

(如果您的字段名称没有空格,则方括号是可选的)

另外,正如HansUp在他的回答中指出的那样,你错过了最后一个&符号...你忘了把最后一个&放在你的字符串的最后一行:"'" & rs.JobGroup"')"应该是“'”& ; rs.JobGroup& “')”