运行时错误3134 Microsoft Access

时间:2017-06-20 20:14:08

标签: sql ms-access

我无法确定错误导致问题的根本原因。

下面是我的SQL语句

CurrentDb.Execute "INSERT INTO Kids(ID,First,Last,Age,Gender,Race,Org,School,Address,City,State,Zip,Name_of_Parent_Guardian,Parent_Guardian_Phone,Parent_Guardian_Email_Address,Child_s_Phone,Child_s_Email_Address,Siblings_Who_Participated_in_KIF,Years_Participated_in_KIF,On_Facebook_Y_or_N,On_Pic_Pals_Y_or_N,Notes_Comments,Year" & _
"Value ('" & "','" & Me.First.Value & "','" & Me.Last.Value & "','" & Me.Age.Value & "','" & Me.Gender.Value & "','" & Me.Race.Value & "','" & Me.Org.Value & "','" & Me.School.Value & "','" & Me.Address.Value & "','" & Me.City.Value & "','" & Me.State.Value & "','" & Me.Zip.Value & "','" & Me.NameofparentG.Value & "','" & Me.parentphone.Value & "','" & Me.parentemail.Value & "','" & Me.Child_sphone.Value & "','" & Me.Child_sEmailAddress.Value & "','" & Me.Sibling_s_WhoParticipatedinKIF.Value & "','" & Me.Year_ParticipatedinKIF.Value & "','" & Me.OnFacebook_Y_or_N_.Value & "','" & Me.NotesComments.Value & "','" & Me.Year.Value & "')"

2 个答案:

答案 0 :(得分:1)

检查你的语法......

它应该是这样的:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

答案 1 :(得分:1)

不能强调:考虑使用参数化查询,避免引用处理和变量连接,以获得更易读,可维护和更安全的代码。 Access SQL允许PARAMETERS子句指定具有已定义类型的命名参数,这些参数可以在VBA中使用Querydef进行调用。

SQL (在数据库中另存为存储查询,根据需要调整类型和大小)

PARAMETERS [IDParam] TEXT(255), [FirstParam] TEXT(255), [LastParam] TEXT(255), [AgeParam] TEXT(255), [GenderParam] TEXT(255), 
           [RaceParam] TEXT(255), [OrgParam] TEXT(255), [SchoolParam] TEXT(255), [AddressParam] TEXT(255), [CityParam] TEXT(255), 
           [StateParam] TEXT(255), [ZipParam] TEXT(255), [NameofparentGParam] TEXT(255), [parentphoneParam] TEXT(255), 
           [parentemailParam] TEXT(255), [Child_sphoneParam] TEXT(255), [Child_sEmailAddressParam] TEXT(255),
           [Sibling_s_WhoParticipatedinKIFParam] TEXT(255), [Year_ParticipatedinKIFParam] TEXT(255), 
           [OnFacebook_Y_or_N_Param] TEXT(255), [NotesCommentsParam] TEXT(255), [YearParam] TEXT(255);

INSERT INTO Kids ([ID], [First], [Last], Age, Gender, Race, Org, School, Address, City, State, Zip,
                  Name_of_Parent_Guardian, Parent_Guardian_Phone, Parent_Guardian_Email_Address,
                  Child_s_Phone, Child_s_Email_Address, Siblings_Who_Participated_in_KIF,
                  Years_Participated_in_KIF, On_Facebook_Y_or_N, On_Pic_Pals_Y_or_N,Notes_Comments, [Year])
VALUES ([IDParam], [FirstParam], [LastParam], [AgeParam], [GenderParam], [RaceParam], 
        [OrgParam], [SchoolParam], [AddressParam], [CityParam], [StateParam], [ZipParam], 
        [NameofparentGParam], [parentphoneParam], [parentemailParam], [Child_sphoneParam], 
        [Child_sEmailAddressParam], [Sibling_s_WhoParticipatedinKIFParam], [Year_ParticipatedinKIFParam], 
        [OnFacebook_Y_or_N_Param], [NotesCommentsParam], [YearParam]);

VBA (动态地将值传递给命名参数,然后执行)

Dim db As Database
Dim qdef As QueryDef

Set db = CurrentDb
Set qdef = db.QueryDefs("myQueryName")

qdef![IDParam] = Me.ID
qdef![FirstParam] = Me.First
qdef![LastParam] = Me.Last
qdef![AgeParam] = Me.Age
qdef![GenderParam] = Me.Gender 
qdef![RaceParam] = Me.Race
qdef![OrgParam] = Me.Org
qdef![SchoolParam] = Me.School 
qdef![AddressParam] = Me.Address
qdef![CityParam] = Me.City
qdef![StateParam] = Me.[State]
qdef![ZipParam] = Me.Zip
qdef![NameofparentGParam] = Me.NameofparentG
qdef![parentphoneParam] = Me.parentphone 
qdef![parentemailParam] = Me.parentemail 
qdef![Child_sphoneParam] = Me.Child_sphone
qdef![Child_sEmailAddressParam] = Me.Child_sEmailAddress
qdef![Sibling_s_WhoParticipatedinKIFParam] = Me.Sibling_s_WhoParticipatedinKIF
qdef![Year_ParticipatedinKIFParam] = Me.Year_ParticipatedinKIF
qdef![OnFacebook_Y_or_N_Param] = Me.OnFacebook_Y_or_N_ 
qdef![NotesCommentsParam] = Me.NotesCommentsParam 
qdef![YearParam] = Me.YearParam

qdef.Execute dbFailOnError

Set qdef = Nothing
Set db = Nothing