2010-02-19 74 views
9

如何手動添加新記錄到新的&空的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 

回答

13

就地:

rs.AddNew "SomeFieldName", "SomeValue" 

或就地多個領域

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

或使用單獨的變量

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

+0

你可能包括的AddNew之前的申報和準備記錄的()調用?我想確保我正在做所有需要做的事情,不多也不少。 – bitcycle 2010-02-19 15:53:08

+0

添加了創建記錄集示例代碼 – wqw 2010-02-19 18:49:56

+0

您真的救了我的命! – 2012-09-12 16:31:51

3
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 
0

以開放的連接康恩:

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