如何手动将新记录正确添加到空记录集?

时间:2010-02-19 03:46:59

标签: vb6 ado

如何将新记录添加到新的&手动清空ADODB.Recordset?

现在,这就是我正在做的不起作用的事情:

Dim rs as ADODB.Recordset
rs.Open
Dim Fields() as String
Fields(0) = "SomeFieldName"

Dim Values() as String
Value(0) = "SomeValue"

rs.AddNew Fields, Values

3 个答案:

答案 0 :(得分:15)

在就地:

rs.AddNew "SomeFieldName", "SomeValue"

或就地多个字段

rs.AddNew Array("SomeFieldName", "AnotherFieldName"), Array("SomeValue", 1234)

或使用单独的vars

Dim Fields As Variant
Dim Values As Variant

Fields = Array("SomeFieldName")
Values = Array("SomeValue")
rs.AddNew Fields, Values

编辑:这是如何为上面的AddNew示例合成记录集

Set rs = new Recordset
rs.Fields.Append "SomeFieldName", adVarChar, 1000, adFldIsNullable
rs.Fields.Append "AnotherFieldName", adInteger, , adFldIsNullable
rs.Open

我通常使用帮助函数CreateRecordset,如this answer所示。

更新2018-11-12

您还可以使用字段索引作为序号而不是字段名称作为字段数组的字符串,如此

rs.AddNew Array(0, 1), Array("SomeValue", 1234)

答案 1 :(得分:5)

set rs = new ADODB.Recordset
rs.Open "Select SomeFieldName, AnotherFieldName FROM MyTable", myConnection, adOpenDynamic, adLockOptimistic

rs.AddNew
rs("SomeFieldName").Value = "SomeValue"
rs("AnotherFieldName").Value = 1
rs.Update

rs.AddNew
rs("SomeFieldName").Value = "AnotherValue"
rs("AnotherFieldName").Value = 2
rs.Update

rs.Close

答案 2 :(得分:0)

打开连接Conn:

sSql="INSERT into mytable (somefieldname, anotherfieldname) values  ('Somevalue','Anothervalue')"
      Conn.Execute sSql