问题
This is the code:
sqlStr = "INSERT INTO Students (ID, Last Name, First Name, E-mail Address, Student ID, Level, Date of Birth, Home Phone, Address, City, State/Province) VALUES ('" & ID.Value & "','" & lastName.Value & "','" & firstName.Value & "','" & email.Value & "','" & studentID.Value & "','" & birthday.Value & "','" & phone.Value & "','" & address.Value & "','" & city.Value & "','" & state.Value & "');"
DoCmd.RunSQL sqlStr
The .Value
variables are coming from text boxes from a userform.
回答1:
You could open the table and insert the record directly without building the sql statement.
Dim rs as dao.recordset
set rs = Currentdb.tabledefs("Students").openrecordset
Rs.addnew
' both rs(fieldname) and rs!fieldName would work
Rs("ID") = id
Rs![Last Name] = lastName
rs![First Name] =
rs![E-mail Address]=
rs![Student ID]=
rs!Level=
rs![Date of Birth]=
rs![Home Phone]=
rs!Address=
rs!City=
rs![State/Province]=
Rs.update
Rs.close
Set rs = nothing
Assign rest of the text box values for each field and try it. At least you can escape from syntax error
replying from mobile
回答2:
Try this:
sqlStr = "INSERT INTO Students (ID, [Last Name], [First Name], [E-mail Address], [Student ID], Level, [Date of Birth], [Home Phone], Address, City, [State/Province]) VALUES ('" & ID.Value & "','" & lastName.Value & "','" & firstName.Value & "','" & email.Value & "','" & studentID.Value & "','" & birthday.Value & "','" & phone.Value & "','" & address.Value & "','" & city.Value & "','" & state.Value & "');"
回答3:
You'll need square brackets surrounding fields with spaces or special characters, e.g.:
(ID, [Last Name], [First Name], [E-mail Address], [Student ID], Level, [Date of Birth], [Home Phone], Address, City, [State/Province])
Also, unless all of your fields are text fields, you'll need to correctly represent the values depending on the data type - for example, if Date of Birth
is a date field (as it should be), the date value should be supplied in the format:
#mm/dd/yyyy#
回答4:
The names of the fields with spaces need to be closed between sqaure brackets and check the data type of your values. Additionally you need to use a single quote (') in your varchar .Values.
sql_qry = "INSERT INTO STUDENTS(ID, [NAME ST], AGE) VALUES(" & v1 & ",'" & v2 & "'," & v3 & ")"
then
DoCmd.RunSQL (sql_qry)
回答5:
You set 11 fields to insert, but give only 10 values.
You lack the value of Level
sqlStr = "INSERT INTO Students "
sqlStr = sqlStr & "([ID], [Last Name], [First Name], [E-mail Address], [Student ID], [Level], [Date of Birth], [Home Phone], [Address], [City], [State/Province]) VALUES ("
sqlStr = sqlStr & "'" & ID.Value & "'"
sqlStr = sqlStr & ",'" & lastName.Value & "'"
sqlStr = sqlStr & ",'" & firstName.Value & "'"
sqlStr = sqlStr & ",'" & email.Value & "'"
sqlStr = sqlStr & ",'" & studentID.Value & "'"
sqlStr = sqlStr & "," & Level.Value & "" '<--lack. if number
sqlStr = sqlStr & ",#" & birthday.Value & "#" '<--if date, blocked with #
sqlStr = sqlStr & ",'" & phone.Value & "'"
sqlStr = sqlStr & ",'" & address.Value & "'"
sqlStr = sqlStr & ",'" & city.Value & "'"
sqlStr = sqlStr & ",'" & state.Value & "'"
sqlStr = sqlStr & ");"
来源:https://stackoverflow.com/questions/59079847/trying-to-execute-a-sql-statement-from-vba-to-an-access-database-getting-a-synt