PC Review


Reply
Thread Tools Rate Thread

ADO: Mistakenly updating actual database by updating a recordset

 
 
=?Utf-8?B?UmljaGFyZA==?=
Guest
Posts: n/a
 
      29th Apr 2004
Hello,

Using the code below based on a select query, I have been trying to loop through a recordset, while peforming the methods AddNew or Delete. However, in the AddNew case, it only goes through the loop once and adds one record before stoppiong. In the Delete case, I get a runtime error and cannot delete. This despite running Supports queries that verify that these methods are supported.

But then I find that my simple playing with the recordset has been updating the actual database!!! The SELECT query statement was taken from a query I actually saved in my Access database, and though I thought I was just playing with the recordset when I updated am actual field in the saved query!!! It also updated an actual field in one of the tables on which I based my SELECT query--that is, the query was based on joining existing tables, but I would have expected only the recordset, separate from the original tables, to be affected. Am I misunderstanding something here? Isn't the recordset independent of the tables on which it is based? (code follows below)

Richard

=====================

Dim myRecordSet, myRecordSetNew As ADODB.Recordset
Set myRecordSet = New ADODB.Recordset

Dim strSQL As String
strSQL = "SELECT chorizon.hzname, chorizon.hzdepb_r, " & _
"chorizon.sandtotal_r, chorizon.silttotal_r, " & _
"chorizon.claytotal_r, chorizon.ksat_r, chorizon.awc_r, " & _
"chorizon.wtenthbar_r, chorizon.wthirdbar_r, " & _
"chorizon.wfifteenbar_r, chorizon.wsatiated_r, " & _
"chorizon.chkey, chtexturegrp.texture, " & _
"chtexturegrp.stratextsflag, chtexturegrp.rvindicator, " & _
"chtexturegrp.chtgkey, component.comppct_r, component.compname, " & _
"component.compkind, component.majcompflag, component.cokey, " & _
"mapunit.musym, mapunit.muname, mapunit.mukind, mapunit.muacres, " & _
"mapunit.mukey " & _
"FROM (mapunit INNER JOIN (component INNER JOIN chorizon " & _
"ON component.cokey = chorizon.cokey) " & _
"ON mapunit.mukey = component.mukey) INNER JOIN chtexturegrp " & _
"ON chorizon.chkey = chtexturegrp.chkey"

myRecordSet.Open strSQL, CurrentProject.Connection, adOpenStatic, _
adLockOptimistic, adCmdText

'Supports queries
If myRecordSet.Supports(adDelete) Then
Debug.Print "Supports Delete"
Else
Debug.Print "Doesn't Support Delete"
End If
If myRecordSet.Supports(adAddNew) Then
Debug.Print "Supports AddNew"
Else
Debug.Print "Doesn't Support AddNew"
End If
If myRecordSet.Supports(adFind) Then
Debug.Print "Supports Find"
Else
Debug.Print "Doesn't Support Find"
End If
If myRecordSet.Supports(adIndex) Then
Debug.Print "Supports Index"
Else
Debug.Print "Doesn't Support Index"
End If

On Error GoTo HandleErrors

With myRecordSet
.MoveFirst
Do
'.AddNew
.Fields("musym") = "NewSym"
'.Fields("mustatus") = "Newst"
.Update
Debug.Print .Fields("musym").Value
.MoveNext
Loop Until .EOF
Debug.Print .RecordCount
End With

Exit Sub

 
Reply With Quote
 
 
 
 
Dan
Guest
Posts: n/a
 
      29th Apr 2004
Good afternoon!

The recordset is not independent of the tables. Whatever
you select in the SQL statements is what the code will
affect. I believe that in ADO you can create a recordset
in memory that is not bound to any table, but that is
different from what your code is doing here. Your object
variables are pointing to actual tables in your database
and the code is making changes to the records in those
tables. Hope this helps!

Dan


>-----Original Message-----
>Hello,
>
>Using the code below based on a select query, I have been

trying to loop through a recordset, while peforming the
methods AddNew or Delete. However, in the AddNew case, it
only goes through the loop once and adds one record
before stoppiong. In the Delete case, I get a runtime
error and cannot delete. This despite running Supports
queries that verify that these methods are supported.
>
>But then I find that my simple playing with the recordset

has been updating the actual database!!! The SELECT query
statement was taken from a query I actually saved in my
Access database, and though I thought I was just playing
with the recordset when I updated am actual field in the
saved query!!! It also updated an actual field in one of
the tables on which I based my SELECT query--that is, the
query was based on joining existing tables, but I would
have expected only the recordset, separate from the
original tables, to be affected. Am I misunderstanding
something here? Isn't the recordset independent of the
tables on which it is based? (code follows below)
>
>Richard
>
>=====================
>
> Dim myRecordSet, myRecordSetNew As ADODB.Recordset
> Set myRecordSet = New ADODB.Recordset
>
> Dim strSQL As String
> strSQL = "SELECT chorizon.hzname, chorizon.hzdepb_r, "

& _
> "chorizon.sandtotal_r, chorizon.silttotal_r, " & _
> "chorizon.claytotal_r, chorizon.ksat_r,

chorizon.awc_r, " & _
> "chorizon.wtenthbar_r, chorizon.wthirdbar_r, " & _
> "chorizon.wfifteenbar_r, chorizon.wsatiated_r, " & _
> "chorizon.chkey, chtexturegrp.texture, " & _
> "chtexturegrp.stratextsflag,

chtexturegrp.rvindicator, " & _
> "chtexturegrp.chtgkey, component.comppct_r,

component.compname, " & _
> "component.compkind, component.majcompflag,

component.cokey, " & _
> "mapunit.musym, mapunit.muname, mapunit.mukind,

mapunit.muacres, " & _
> "mapunit.mukey " & _
> "FROM (mapunit INNER JOIN (component INNER JOIN

chorizon " & _
> "ON component.cokey = chorizon.cokey) " & _
> "ON mapunit.mukey = component.mukey) INNER JOIN

chtexturegrp " & _
> "ON chorizon.chkey = chtexturegrp.chkey"
>
> myRecordSet.Open strSQL, CurrentProject.Connection,

adOpenStatic, _
> adLockOptimistic, adCmdText
>
> 'Supports queries
> If myRecordSet.Supports(adDelete) Then
> Debug.Print "Supports Delete"
> Else
> Debug.Print "Doesn't Support Delete"
> End If
> If myRecordSet.Supports(adAddNew) Then
> Debug.Print "Supports AddNew"
> Else
> Debug.Print "Doesn't Support AddNew"
> End If
> If myRecordSet.Supports(adFind) Then
> Debug.Print "Supports Find"
> Else
> Debug.Print "Doesn't Support Find"
> End If
> If myRecordSet.Supports(adIndex) Then
> Debug.Print "Supports Index"
> Else
> Debug.Print "Doesn't Support Index"
> End If
>
> On Error GoTo HandleErrors
>
> With myRecordSet
> .MoveFirst
> Do
> '.AddNew
> .Fields("musym") = "NewSym"
> '.Fields("mustatus") = "Newst"
> .Update
> Debug.Print .Fields("musym").Value
> .MoveNext
> Loop Until .EOF
> Debug.Print .RecordCount
> End With
>
> Exit Sub
>
>.
>

 
Reply With Quote
 
=?Utf-8?B?UmljaGFyZA==?=
Guest
Posts: n/a
 
      29th Apr 2004
Thanks for responding Dan. I suppose you are right. But I think I have a good manual (Litwin et al. 2001), and that does not seem at all clear to me

Richar


----- Dan wrote: ----

Good afternoon

The recordset is not independent of the tables. Whatever
you select in the SQL statements is what the code will
affect. I believe that in ADO you can create a recordset
in memory that is not bound to any table, but that is
different from what your code is doing here. Your object
variables are pointing to actual tables in your database
and the code is making changes to the records in those
tables. Hope this helps

Da


>-----Original Message----
>Hello
>>Using the code below based on a select query, I have been

trying to loop through a recordset, while peforming the
methods AddNew or Delete. However, in the AddNew case, it
only goes through the loop once and adds one record
before stoppiong. In the Delete case, I get a runtime
error and cannot delete. This despite running Supports
queries that verify that these methods are supported.
>>But then I find that my simple playing with the recordset

has been updating the actual database!!! The SELECT query
statement was taken from a query I actually saved in my
Access database, and though I thought I was just playing
with the recordset when I updated am actual field in the
saved query!!! It also updated an actual field in one of
the tables on which I based my SELECT query--that is, the
query was based on joining existing tables, but I would
have expected only the recordset, separate from the
original tables, to be affected. Am I misunderstanding
something here? Isn't the recordset independent of the
tables on which it is based? (code follows below
>>Richar
>>====================
>> Dim myRecordSet, myRecordSetNew As ADODB.Recordse

> Set myRecordSet = New ADODB.Recordse
>> Dim strSQL As Strin

> strSQL = "SELECT chorizon.hzname, chorizon.hzdepb_r, "

&
> "chorizon.sandtotal_r, chorizon.silttotal_r, " &
> "chorizon.claytotal_r, chorizon.ksat_r,

chorizon.awc_r, " &
> "chorizon.wtenthbar_r, chorizon.wthirdbar_r, " &
> "chorizon.wfifteenbar_r, chorizon.wsatiated_r, " &
> "chorizon.chkey, chtexturegrp.texture, " &
> "chtexturegrp.stratextsflag,

chtexturegrp.rvindicator, " &
> "chtexturegrp.chtgkey, component.comppct_r,

component.compname, " &
> "component.compkind, component.majcompflag,

component.cokey, " &
> "mapunit.musym, mapunit.muname, mapunit.mukind,

mapunit.muacres, " &
> "mapunit.mukey " &
> "FROM (mapunit INNER JOIN (component INNER JOIN

chorizon " &
> "ON component.cokey = chorizon.cokey) " &
> "ON mapunit.mukey = component.mukey) INNER JOIN

chtexturegrp " &
> "ON chorizon.chkey = chtexturegrp.chkey
>> myRecordSet.Open strSQL, CurrentProject.Connection,

adOpenStatic,
> adLockOptimistic, adCmdTex
>> 'Supports querie

> If myRecordSet.Supports(adDelete) The
> Debug.Print "Supports Delete
> Els
> Debug.Print "Doesn't Support Delete
> End I
> If myRecordSet.Supports(adAddNew) The
> Debug.Print "Supports AddNew
> Els
> Debug.Print "Doesn't Support AddNew
> End I
> If myRecordSet.Supports(adFind) The
> Debug.Print "Supports Find
> Els
> Debug.Print "Doesn't Support Find
> End I
> If myRecordSet.Supports(adIndex) The
> Debug.Print "Supports Index
> Els
> Debug.Print "Doesn't Support Index
> End I
>> On Error GoTo HandleError
>> With myRecordSe

> .MoveFirs
> D
> '.AddNe
> .Fields("musym") = "NewSym
> '.Fields("mustatus") = "Newst"
> .Update
> Debug.Print .Fields("musym").Value
> .MoveNext
> Loop Until .EOF
> Debug.Print .RecordCount
> End With
>> Exit Sub
>>.

>

 
Reply With Quote
 
John Vinson
Guest
Posts: n/a
 
      30th Apr 2004
On Thu, 29 Apr 2004 07:41:08 -0700, "Richard"
<(E-Mail Removed)> wrote:

>Am I misunderstanding something here?


Yes.

>Isn't the recordset independent of the tables on which it is based?


No. It's like an updateable View.

John W. Vinson[MVP]
Come for live chats every Tuesday and Thursday
http://go.compuserve.com/msdevapps?loc=us&access=public

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Edit recordset without updating database jno.aubrey@gmail.com Microsoft Access VBA Modules 1 20th Sep 2006 09:14 PM
Updating DAO.Recordset =?Utf-8?B?TWF0dA==?= Microsoft Access VBA Modules 0 10th Mar 2006 04:57 PM
Need help with updating a recordset Alain Microsoft Access Form Coding 0 18th Nov 2004 08:54 PM
updating a recordset Dawn Microsoft Access Form Coding 1 15th Jun 2004 08:52 AM
Updating recordset Chad Microsoft Access Forms 1 16th Dec 2003 12:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:29 AM.