简体   繁体   English

无法使用vb6将记录插入ms access 2007

[英]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" 我一直在寻找使用VB6.0连接到数据库(MS ACCESS 2007)的正确方法...问题是它出现了“语法错误插入声明”的错误

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: 这是BUTTON代码:

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 当我点击调试时,在这行代码中发现了错误:adoConn.Execute sqlStr

YOUr help would be greatly appreciated as this school project is needed by tomorrow. 非常感谢您的帮助,因为明天需要这个学校项目。 Been sleepless for many nights. 多晚都没睡了。 thansk 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(). 我的意思是你正在尝试INSTO INSERT INTO到9列(CourseCode,Units,Days,Time,RoomNumber,Instructor,Course,YearLevel,Term),但是,你要放10个值()。 txtCurDays is duplicated. txtCurDays是重复的。

That error indicates the generated SQL statement has an error in it. 该错误表示生成的SQL语句中包含错误。

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). 然后查看SQL查询(将其打印到即时窗口或在本地窗口中检查它)。 Check for any syntax errors. 检查是否存在语法错误。

One likely errore in your SQL would be a apastrophe (') in one of your text fields. 你的SQL中可能错误的是你的一个文本字段中的apastrophe(')。 You need to make sure you "escape" any apostrophes in your SQL statement. 您需要确保在SQL语句中“转义”任何撇号。 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 [] : 通过括在[]转义与保留字: TIME匹配的列名:

 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. 您还应该使用参数化查询作为易受SQL注入攻击的查询。 (Run with a ' in one of the textboxes) (在其中一个文本框中运行')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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