How to properly add new records to empty recordset manually?

后端 未结 3 1508
自闭症患者
自闭症患者 2021-01-02 01:15

How to add new records to a new & empty ADODB.Recordset manually?

Right now, here\'s what I\'m doing that isn\'t working:

Dim rs as ADODB.Records         


        
3条回答
  •  自闭症患者
    2021-01-02 01:42

    In-place:

    rs.AddNew "SomeFieldName", "SomeValue"
    

    Or in-place multiple fields

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

    Or using separate vars

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

    Edit: This is how to synthesize a recordset for the AddNew sample above

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

    I'm usually using a helper function CreateRecordset as seen this answer.

    Update 2018-11-12

    You can also use field indexes as ordinals instead of field names as strings for the fields array like this

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

提交回复
热议问题