VB6 ADO INSERT将TEXT字段转换为.MDB

时间:2011-08-14 01:29:41

标签: ms-access vb6 odbc ado

我的示例VB6程序:

Dim conn As New connection
conn.Open "Driver={Microsoft Access Driver (*.mdb)};Dbq=" & App.Path & "\adatabase.mdb;"
conn.Execute "INSERT INTO atable (afield) VALUES (""some text"")"
End Sub

(“adatabase.mdb”包含一个表“atable”,其中一个字段“afield”类型为“Text”,长度为255. Access 2002,Access file format 2000. VB6引用包括“Microsoft ActiveX Data Objects 2.8 Library”。)

conn.Execute得到:

  

运行时错误'-2147217904(80040e10'[Microsoft] [ODBC访问驱动程序]参数太少。预计1。

查询

INSERT INTO atable (afield) VALUES ("some text")

直接在Access中运行,没有任何问题。

1 个答案:

答案 0 :(得分:0)

不应该是:

conn.Execute "INSERT INTO atable (afield) VALUES ("""some text""")"

即。你需要引用双引号吗?

更新:或者当海报解决后,使用单引号引用文字。

甚至更好,使用参数化查询:

Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim Param1 As ADODB.Parameter
Dim Rs1 As ADODB.Recordset

' Create and Open Connection Object.
Set Conn1 = New ADODB.Connection
Conn1.ConnectionString = "DSN=Biblio;UID=admin;PWD=;"
Conn1.Open

' Create Command Object.
Set Cmd1 = New ADODB.Command
Cmd1.ActiveConnection = Conn1
Cmd1.CommandText = "SELECT * FROM Authors WHERE AU_ID < ?"

' Create Parameter Object.
Set Param1 = Cmd1.CreateParameter(, adInteger, adParamInput, 5)
Param1.Value = 5
Cmd1.Parameters.Append Param1
Set Param1 = Nothing

' Open Recordset Object.
Set Rs1 = Cmd1.Execute()