开发者

VBA Recordsets: Why does the way I declare my array, in the Fieldvalue parameters of .addnew, matter?

I've been trying to dynamically add to a Recordset but I am unable to do so. The .addnew function of a adodb.recordset seems to not allow just any type of array to be passed to it's fieldvalues, values parameters. For example if after setting up your recordset with the appropriate field values you say:

 FieldsArray = array("field1", "field2")
 ValuesArray = array("val1","val2")

 rs.AddNew FieldsArray, ValuesArray

This works!! But if you instead write

 Dim fieldsarray(0 To 1) As String
 FieldsArray(0) = "field1"
 FieldsArray(1) = "field2"
 ValuesArray = array("val1","val2")

 rs.AddNew FieldsArray, ValuesArray

It Fails?!?! (More specifically [Run-Time error '3001': Arguments are of the wrong type, are ou开发者_开发技巧t of acceptable range, or are in conflict with one another])

Why does the way one declares the field array in the .addnew parameters matter? Is there a way I can get the latter to work?


This is because in the first example you are declaring two arrays of VARIANT data type, while in the second example you are declaring STRING arrays.

The AddNew function is expecting a variant array.

In you second example, change your first line to:

Dim fieldsarray(0 To 1) As Variant

and it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜