字符串')'之前的未闭合引号。得到这个错误

时间:2015-01-23 20:45:09

标签: vb6

(   db.Execute "insert into Studentstemp(StudentNameA,FacultyNumber,DepartmentNumber" & _
",BatchDescription,programNumber, Studentstatus, Gender, AddmissionFormNo, StudentNumber)values('" & VBA.Trim(TxtStudentNameA.Text) & "','" & _
                                                              CmbFaculty.BoundText & "','" & _
                                                              CmbDepartMent.BoundText & "','" & _
                                                              CmbBatch.BoundText & "','" & _
                                                              CmbProgram.BoundText & "','" & _
                                                              Cmbastatute.Text & "','" & _
                                                              CmbGnder.Text & "','" & _
                                                              TxtStudyFees.Text & ")")

2 个答案:

答案 0 :(得分:2)

你错过了一个'最后。

(db.Execute "insert into Studentstemp(StudentNameA,FacultyNumber,DepartmentNumber" & _
            ",BatchDescription,programNumber, Studentstatus, Gender, AddmissionFormNo" & _
            ",StudentNumber)values('" & VBA.Trim(TxtStudentNameA.Text) & "','" & _
             CmbFaculty.BoundText & "','" & CmbDepartMent.BoundText & "','" & _
             CmbBatch.BoundText & "','" & CmbProgram.BoundText & "','" & _
             Cmbastatute.Text & "','" & CmbGnder.Text & "','" & TxtStudyFees.Text & "')")

答案 1 :(得分:1)

高级拱门告诉你的是,如果程序中输入的其中一个字符串中包含撇号,则会出现此错误。要自己证明这一点,请在txtStudentName中输入学生姓名“O'Malley”。现在,我同意你应该使用参数,但要解决这个特殊问题,你需要这样做:

...values('" & _
    Replace(Trim(txtStudentName.Text), "'", "''") & "','" & _
    Replace(CmbFaculty.BoundText, "'", "''") & "','" & _
    Replace(CmbDepartMent.BoundText, "'", "''") & "','" & _
...                                                          

等等。 SQL需要两个撇号来表示文字撇号,因此'O''Malley'将是在SQL中的字段中输入值O'Malley的方法。