Making read-only recordset updatable

Posted on 2006-04-11
Last Modified: 2013-12-25

I'm looking for a way to change a read-only recordset into an updateable one.

The application queries several tables and, due to the links, returns a non-updateable result.

I then disconnect this (ActiveConnection=Null) and want to edit the results for a report, but I can't see a way of changing the results table.

I've tried saving it as a persisted recordset and reopening as editable, but it still remembers that it cannot be updated.

Anyone any clues?


Question by:StuckInTheMud
    LVL 11

    Expert Comment

    Are these updates temporary???

    If not, why not do the updates directly on the table that needs to be updated using the connection object
    yourconn.execute "update table set yourfield = NewData where tablekey = " & localrs.fields("key")

    then set the local recordsets connection back and refresh it.


    Author Comment

    Yes, they are temporary for the report. I specifically don't want to change underlying data.

    LVL 11

    Expert Comment

    Why would you like to only "temporarily" change the data??

    What db is this?  Access??  You could always look into using temporary tables turn the queries into insert statements and then modify the temp tables and run your reports off of these tables.  Although this isn't very "clean".

    Perhaps you could make the changes in the query itself??  If these are calculations, that would be quite easy to do.

    Author Comment

    DB is Oracle and SQL Server (I have a couple of apps I want to do this in).

    I'm using the recordset as a grid for a report that is displayed/exported to excel. The changes are things like removing specific text for display grouping.

    Anyway, this is irrelevant to the original query, how to make a read-only recordset into an editable one.
    LVL 11

    Expert Comment

    Try using a disconnected recordset:

    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    conn.Open ""Provider=sqloledb;Data Source=YourServer;Initial Catalog=YourDb;User Id=;Password=;"
    rs.CursorLocation = adUseClient
    rs.LockType = adLockBatchOptimistic
    rs.Open "SELECT Table1.Test, Table2.test1 FROM Table1 JOIN table2 ON Table1.Pkey = Table2.Pkey;", conn
    Set rs.ActiveConnection = Nothing
    rs.fields(1) = "Mike "

    I opened the above and it's originally not updatable, but once it was disconnected, I was able to make modifications.
    LVL 11

    Expert Comment

    Okay I just re-read your original question and you already tried a disconnected recordset?!?!

    Perhaps your locktype was wrong??
    LVL 29

    Accepted Solution

    >how to make a read-only recordset into an editable one.

    You can not.
    LVL 8

    Expert Comment

    if u wan to edit data,then do not open ur recordset as read-only...
    really simple logic and concept...
    have fun...
    LVL 1

    Expert Comment

    make a copy of the original recordset using

      Set rs2 = CopyRecordset(rs)

    rs2 will be updateable.

    Public Function CopyRecordset(ByRef rs As Recordset) As Recordset
        Dim fld As ADODB.Field
        Dim rsDummy As New Recordset ' the resulting recordset
        ' create fields in rsDummy
        For Each fld In rs.Fields
            rsDummy.Fields.Append fld.Name, fld.Type, fld.DefinedSize, fld.Attributes
        ' copy the recordset's content
        While Not rs.EOF
            For Each fld In rs.Fields
                rsDummy.Fields(fld.Name) = rs.Fields(fld.Name)
        Set CopyRecordset = rsDummy
        Exit Function
    End Function

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    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…
    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…

    754 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

    24 Experts available now in Live!

    Get 1:1 Help Now