简体   繁体   English

VB6 ADO INSERT 将 TEXT 字段插入.MDB

[英]VB6 ADO INSERT of TEXT field into .MDB

My example VB6 program:我的示例 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" contains one table "atable" with one field "afield" of type "Text" length 255. Access 2002, Access file format 2000. VB6 references include "Microsoft ActiveX Data Objects 2.8 Library".) (“adatabase.mdb”包含一个表“atable”,其中一个字段“afield”的类型为“Text”,长度为 255。Access 2002,Access 文件格式 2000。VB6 引用包括“Microsoft ActiveX Data Objects 2.8 Library”。)

conn.Execute gets: conn.Execute 得到:

Runtime error '-2147217904 (80040e10' [Microsoft][ODBC Access Driver] Too few parameters. Expected 1.运行时错误 '-2147217904 (80040e10' [Microsoft][ODBC Access Driver] 参数太少。预期为 1。

The query查询

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

runs directly in Access without any problem.直接在Access中运行没有任何问题。

Shouldn't that be:那不应该是:

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

ie you need to quote the double quotes?即你需要引用双引号吗?

Update: or as the poster solved, use single quotes to quote text.更新:或作为海报解决,使用单引号引用文本。

Or even better, use a parameterised query:或者更好的是,使用参数化查询:

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()

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

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