简体   繁体   中英

Cannot insert records to ms access 2007 with vb6

I have been searching around for the proper way of connecting into the database(MS ACCESS 2007) using VB6.0... The problem is it says an error that "SYNTAX ERROR IN INSERT INTO STATEMENT"

DECLARATION CODE:

Dim adoConn As New ADODB.Connection
Dim adoRS As New ADODB.Recordset
Dim conStr, sqlStr As String

CONNECTION CODE:

conStr = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source= " & App.Path &        "\curriculum.mdb;Persist Security Info=False"
Set adoConn = New ADODB.Connection
adoConn.ConnectionString = conStr
adoConn.Open

Here is the BUTTON code:

sqlStr = "INSERT INTO cur(CourseCode, Units, Days, Time, RoomNumber, Instructor, Course, YearLevel, Term) VALUES ("

sqlStr = sqlStr & "'" & txtCurCourseCode.Text & "',"
sqlStr = sqlStr & "'" & txtCurUnits.Text & "',"
sqlStr = sqlStr & "'" & txtCurDays.Text & "',"
sqlStr = sqlStr & "'" & txtCurTime.Text & "',"
sqlStr = sqlStr & "'" & txtCurDays.Text & "',"
sqlStr = sqlStr & "'" & txtCurRoom.Text & "',"
sqlStr = sqlStr & "'" & txtCurInstructor.Text & "',"
sqlStr = sqlStr & "'" & cboCurCourse.Text & "',"
sqlStr = sqlStr & "'" & txtCurYearLevel.Text & "',"
sqlStr = sqlStr & "'" & txtCurTerm.Text & "')"
adoConn.Execute sqlStr

THE ERROR IS FOUND IN THIS LINE OF CODE WHEN I CLICK DEBUG: adoConn.Execute sqlStr

YOUr help would be greatly appreciated as this school project is needed by tomorrow. Been sleepless for many nights. thansk

Unfortunately, you are using duplicate value..

I mean yor are trying to INSERT INTO to 9 columns(CourseCode, Units, Days, Time, RoomNumber, Instructor, Course, YearLevel, Term), however, you are putting 10 values(). txtCurDays is duplicated.

That error indicates the generated SQL statement has an error in it.

Set a breakpoint on the line:

adoConn.Execute sqlStr

Then view the SQL query (print it to the immediate window or just examine it in the locals window). Check for any syntax errors.

One likely errore in your SQL would be a apastrophe (') in one of your text fields. You need to make sure you "escape" any apostrophes in your SQL statement. You can do this easily by tweaking your code a bit like so:

sqlStr = sqlStr & "'" & Replace(cboCurCourse.Text, "'", "''") & "',"

Escape the column names that match reserved words: TIME by enclosing in [] :

 sqlStr = "INSERT INTO cur(CourseCode, Units, Days, [Time], RoomNumber, Instructor, Course, YearLevel, Term) VALUES ("

You should also use paramaterized queries as what you have in vulnerable to SQL Injection. (Run with a ' in one of the textboxes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM