我有一个按钮,可以在点击事件中将数据插入到访问2013数据库中。
这是从Access 2013表单文本框调用数据到insert语句的正确方法吗?
Private Sub EnterDataFrom_Click()
CurrentDb.Execute "insert into Employee_info" _
& "(First_Name,Father_Name,Last_Name,Mother_Name,birthda0te,Birthplace,Gender,Military_Service,Marital_status,Nationality,National_ID,PhoneNum,EM_phoneNum,Mobile,E-Mail)" _
& "values (me.First_Name,me.Father_Name,me.Last_Name,me.Mother_Name,me.birthdate,me.Birthplace,me.Gender,me.Military_Service,me.Marital_status,me.Nationality,me.National_ID,me.PhoneNum,me.EM_phoneNum,me.Mobile,me.E-Mail);"
End Sub
答案 0 :(得分:1)
Xipooo是对的。 通常,可以随时将值条目包装在撇号之间。
您的按钮可能无法正常工作的原因是您在换行符之前忘记了空格(在Employee_info后面和字段后面的结束括号):
CurrentDb.Execute "insert into Employee_info " _ & "(First_Name,Father_Name,Last_Name,Mother_Name,birthda0te,Birthplace,Gender,Military_Service,Marital_status,Nationality,National_ID,PhoneNum,EM_phoneNum,Mobile,E-Mail) " _
& "values('" & me.First_Name & "', '" & me.Father_Name & "', '" & me.Last_Name & "', '" & me.Mother_Name & "', '" & me.birthdate & "', '" & me.Birthplace & "', '" & me.Gender & "', " & me.Military_Service &", '" & me.Marital_status & "', '" & me.Nationality & "', " & me.National_ID & "', '" & me.PhoneNum & "', '" & me.EM_phoneNum & "', '" & me.Mobile & "', '" & me.E-Mail & "');"
(基于Xipooo之前的回答)
答案 1 :(得分:0)
您的字符串格式不正确,无法从表单中收集值。您需要获取字符串之外的值并将它们连接起来。另外,插入到文本列中的字符串值如果在字符串变量中,则应包含在撇号中。
CurrentDb.Execute "insert into Employee_info" _
& "(First_Name,Father_Name,Last_Name,Mother_Name,birthda0te,Birthplace,Gender,Military_Service,Marital_status,Nationality,National_ID,PhoneNum,EM_phoneNum,Mobile,E-Mail)" _
& "values('" & me.First_Name & "', '" & me.Father_Name & "', '" & me.Last_Name & "', '" & me.Mother_Name & "', '" & me.birthdate & "', '" & me.Birthplace & "', '" & me.Gender & "', " & me.Military_Service &", '" & me.Marital_status & "', '" & me.Nationality & "', " & me.National_ID & "', '" & me.PhoneNum & "', '" & me.EM_phoneNum & "', '" & me.Mobile & "', '" & me.E-Mail & "');"
我在这里假设Military_Service和National_ID是数字列。