SQl中的INSERT语句

时间:2011-10-11 20:41:15

标签: sql vba ms-access-2007

我试图在多个表中保存记录我使用的是insert语句但是它给了我“INSERT INTO语句中的语法错误” 这是我的代码

strSQL = " INSERT INTO [2011 Nurse Patient Index]
                (File Number, Patient name ,Gender , AgeRTU , Nationality , 
                 Diagnosis , Other , Appointment , Date)
         VALUES('" & File_NumberRTU.Value & "','" & Patient_nameRTU.Value & "' , 
                '" & GenderRTU.Value & "', '" & AgeRTU.Value & "' , 
                '" & NationalityRTU.Value & "' , '" & DiagnosisRTU.Value & "' ,
                '" & OtherRTU.Value & "' , '" & AppointmentRTU.Value & "' ,
                '" & DateRTU.Value & "')"
        CurrentDb.Execute strSQL

此行中出现错误

CurrentDb.Execute strSQL

谁能告诉我哪里出错了? 谢谢

2 个答案:

答案 0 :(得分:3)

如果数据库中的日期列类型为DateDatetime,则可能必须使用#来包围DateRTU.Value,即

INSERT INTO 
    [2011 Nurse Patient Index]([File Number], [Patient name], Gender, AgeRTU, Nationality, Diagnosis, Other, Appointment, Date) 
VALUES 
    ('" & File_NumberRTU.Value & "', 
     '" & Patient_nameRTU.Value & "', 
     '" & GenderRTU.Value & "', 
     '" & AgeRTU.Value & "', 
     '" & NationalityRTU.Value & "', 
     '" & DiagnosisRTU.Value & "', 
     '" & OtherRTU.Value & "', 
     '" & AppointmentRTU.Value & "', 
     '#" & DateRTU.Value & "#')"

答案 1 :(得分:1)

尝试围绕[File Number][Patient name]的方括号。

strSQL = " INSERT INTO [2011 Nurse Patient Index]([File Number], [Patient name] ,Gender , AgeRTU , Nationality , Diagnosis , Other , Appointment , Date)VALUES('" & File_NumberRTU.Value & "','" & Patient_nameRTU.Value & "' , '" & GenderRTU.Value & "', '" & AgeRTU.Value & "' , '" & NationalityRTU.Value & "' , '" & DiagnosisRTU.Value & "' , '" & OtherRTU.Value & "' , '" & AppointmentRTU.Value & "' , '" & DateRTU.Value & "')"
    CurrentDb.Execute strSQL