Solved

How do I populate ADO recordset with no connection.

Posted on 2001-09-16
2
151 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
Comment Utility
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
Comment Utility
This worked.

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 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

10 Experts available now in Live!

Get 1:1 Help Now