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

Posted on 2004-11-22
Last Modified: 2010-05-02

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
    For i = 0 To UBound(country)
        rsCountryM.AddNew "sCountryCode", country(i)
    Next i

Please help! Thanks!
Question by:sata
    LVL 19

    Expert Comment

    by:Stephen Manderson
    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;"

    sqlstr = "Table"

    rs.Open sqlstr, conn, adOpenKeyset, adLockOptimistic

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

        Next i

    Set rs = Nothing
    Set conn = Nothing
    LVL 28

    Accepted Solution

    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.Fields("MyTextField") = "My Text Item 1"
    rstADO.Fields("MyNumField") = 1

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

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

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

    Set rstADO = Nothing
    End Sub
    LVL 69

    Expert Comment

    by:Éric Moreau
    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
        End With
        For i = 0 To 10
            With rsCountryM
                .Fields("Field1").Value = "Line " & CStr(i)
                .Fields("Field2").Value = "Value " & CStr(i)
            End With
        MsgBox rsCountryM.RecordCount
    End Sub


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    use like in If/Then vba 4 43
    MS Access "DoCmd.applyfilter" 16 70 Listview 9 66
    Put text in a picture ASP.NET C# 2 26
    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…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    760 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