How to create a recordset, not connected to database, but I can add new item to it?

Hi,

i would like to create  a new recordset, but not connecting to any database. I have a list of items which I can only add into the recordset during runtime.

I wrote the below, but was told by the system that "Operation is not allowed when the object is closed" . I tried to open it, but I receive error "The connection cannot be used to perform this operation. It is either closed or invalid in this context"

   Dim country() as string
    country = Split(gCountryFromDB, "|", , vbTextCompare)
    Dim rsCountryM As New ADODB.Recordset
    rsCountryM.Open
    For i = 0 To UBound(country)
        rsCountryM.AddNew "sCountryCode", country(i)
    Next i

Please help! Thanks!
LVL 1
sataAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Stephen MandersonSoftware EngineerCommented:
Hi there you can use this connection.

Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim i As Long

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database.mdb;"
conn.Open

sqlstr = "Table"

rs.Open sqlstr, conn, adOpenKeyset, adLockOptimistic

    For i = 0 To UBound(country)
        rs.AddNew
        rs.Fields("sCountryCode")  = UBound(i) .txt
        rs.Update

    Next i

rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
0
vinnyd79Commented:
You have to add fields to the recordset.This simple example will create a recordset and add three items to it,without a database.


' add reference to Microsoft ActiveX Data Objects Library

Dim rstADO As ADODB.Recordset

Private Sub Command1_Click()
Set rstADO = New ADODB.Recordset
With rstADO.Fields
  .Append "MyTextField", adVarChar, 50 'Character field, varchar type max 50 chars
  .Append "MyNumField", adInteger
End With

rstADO.Open
rstADO.AddNew
rstADO.Fields("MyTextField") = "My Text Item 1"
rstADO.Fields("MyNumField") = 1
rstADO.Update

rstADO.AddNew
rstADO.Fields("MyTextField") = "My Text Item 2"
rstADO.Fields("MyNumField") = 2
rstADO.Update

rstADO.AddNew
rstADO.Fields("MyTextField") = "My Text Item 3"
rstADO.Fields("MyNumField") = 3
rstADO.Update


rstADO.MoveFirst
While Not rstADO.EOF = True
    MsgBox rstADO.Fields("MyTextField")
    MsgBox rstADO.Fields("MyNumField")
    rstADO.MoveNext
Wend

rstADO.Close
Set rstADO = Nothing
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
You need to add fields. See this sample:

Private Sub Command1_Click()
Dim i As Integer
Dim rsCountryM As New ADODB.Recordset

    With rsCountryM
        With .Fields
            .Append "Field1", adVarChar, 50
            .Append "Field2", adVarChar, 50
        End With
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .LockType = adLockBatchOptimistic
        .Open
    End With
   
    For i = 0 To 10
        With rsCountryM
            .AddNew
            .Fields("Field1").Value = "Line " & CStr(i)
            .Fields("Field2").Value = "Value " & CStr(i)
            .Update
        End With
    Next
   
    MsgBox rsCountryM.RecordCount
End Sub


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.