Solved

How do I populate ADO recordset with no connection.

Posted on 2001-09-16
2
154 Views
Last Modified: 2006-11-17
I am using VBasic v6, ADO, and Access.

I need to pass a data record to an existing subroutine, that
I did not write.

The subroutine expects the record in the form of a
recordset object, with a single record.

I have my field values that have been computed outside of
any database.  A database with the proper table does not
exist and no connection.  I need to get my values into a
Recordset object.  I want to do something like the following.

-----

    dim RecSet as ADODB.Recordset
    dim name_list(1) as String
    dim value_list(1) as String

    name_list(0) = "Field_0"
    name_list(1) = "Field_1"
    value_list(0) = "val_0"
    value_list(1) = "val_1"

    Set RecSet = new ADODB.Recordset
    RecSet.CursorLocation = adUseClient
    Call RecSet.Open(, , adOpenStatic, adLockBatchOptimistic)
    Call RecSet.AddNew(name_list, value_list)

Alternately, instead of the "AddNew" method I could do something
like:

    ' Declare and initialize an ADODB.Field object
    Dim field_0 as ADODB.Field
    Set field_0 = new ADODB.Field
    ' Initialize the name and value properties of field_0
    Call RecSet.Fields.Append field_0

-----

This does not work.  The following Open fails.

    Call RecSet.Open(, , adOpenStatic, adLockBatchOptimistic)

Instead of adOpenStatic, I have also tried adOpenForwardOnly.

The documentation for the Open method says that Source (1st parm)
and ActiveConnection (2nd parm) are optional.  But Open() still
throws an error.  The error message says that I need a connection.
If, experimentally, I give a valid connection to an irrelevant
database, then I get an error about the missing source (first parm).

And AddNew throws an error, if I try to execute AddNew without
the Open.


So the question is, "How do I populate a recordset with data when
I have no connection to a database?"

0
Comment
Question by:Jim_S
2 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 150 total points
ID: 6486235
This will work:

Private Sub Command1_Click()
Dim RecSet As ADODB.Recordset

   Set RecSet = New ADODB.Recordset
   RecSet.CursorLocation = adUseClient
   RecSet.CursorType = adOpenStatic
   RecSet.LockType = adLockBatchOptimistic
   RecSet.Fields.Append "Field_0", adVarChar, 50
   RecSet.Fields.Append "Field_1", adVarChar, 50
   RecSet.Open
   Call RecSet.AddNew(Array("Field_0", "Field_1"), Array("val_0", "val_1"))

    MsgBox RecSet.RecordCount
End Sub
0
 

Author Comment

by:Jim_S
ID: 6486454
This worked.

Thank you ever so much for your prompt and
accurate answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now