Recordset value changes with .update 
Author Message
 Recordset value changes with .update

If a table has an autonumber ID field and the following code segment is run
in Access 2000:
Public Sub test()
Set db = Currentdb
Set rs = db.OpenRecordset("tblTest")
rs.AddNew
Debug.Print rs![ID]
rs.Update
Debug.Print rs![ID]
rs.MoveLast
Debug.Print rs![ID]
rs.Close
End Sub

I get the correct value for ID for the first and third Debug.Prints, but
just after the .Update, the value changes to an already existing previous
record. Why is that? Shouldn't the current record still be the same after a
.Update unless I move to another record? Why do I have to do a .MoveLast to
point to the record I just updated?

Thanks for any help,
Dom



Sun, 24 Aug 2003 10:24:00 GMT  
 Recordset value changes with .update
I'm no expert, but I believe you'll find that .AddNew ... .Update creates a
new record but doesn't move to it.  Depending on whether the recordset is a
dynaset or a table-type recordset with an index, the record is created
either at the end of the recordset or in the position defined by the key.
Between .AddNew and .Update, the record you're dealing with is the new
record you're creating;  however, after .Update the current record is the
same one you were looking at before you executed .AddNew.

In the example you gave, with an ascending autonumber primary key in a
table-type recordset table, the new record is created at the end of the
recordset because the key places it there.  Therefore, after the .Update,
.MoveLast will position the recordset at the newly-created record.

--

Dirk Goldgar
(to reply via email, remove NOSPAM from address)

Quote:

>If a table has an autonumber ID field and the following code segment is run
>in Access 2000:
>Public Sub test()
>Set db = Currentdb
>Set rs = db.OpenRecordset("tblTest")
>rs.AddNew
>Debug.Print rs![ID]
>rs.Update
>Debug.Print rs![ID]
>rs.MoveLast
>Debug.Print rs![ID]
>rs.Close
>End Sub

>I get the correct value for ID for the first and third Debug.Prints, but
>just after the .Update, the value changes to an already existing previous
>record. Why is that? Shouldn't the current record still be the same after a
>.Update unless I move to another record? Why do I have to do a .MoveLast to
>point to the record I just updated?

>Thanks for any help,
>Dom



Sun, 24 Aug 2003 13:11:25 GMT  
 Recordset value changes with .update
What you said makes some sense, I'll have to check into this further.
However, Access did move to it un Access 97 as this code has been installed
(without the .MoveLast) for over 2 years and broke when updated to Access
2000.

Thanks for your help,
Dom


Quote:
> I'm no expert, but I believe you'll find that .AddNew ... .Update creates
a
> new record but doesn't move to it.  Depending on whether the recordset is
a
> dynaset or a table-type recordset with an index, the record is created
> either at the end of the recordset or in the position defined by the key.
> Between .AddNew and .Update, the record you're dealing with is the new
> record you're creating;  however, after .Update the current record is the
> same one you were looking at before you executed .AddNew.

> In the example you gave, with an ascending autonumber primary key in a
> table-type recordset table, the new record is created at the end of the
> recordset because the key places it there.  Therefore, after the .Update,
> .MoveLast will position the recordset at the newly-created record.

> --

> Dirk Goldgar
> (to reply via email, remove NOSPAM from address)


> >If a table has an autonumber ID field and the following code segment is
run
> >in Access 2000:
> >Public Sub test()
> >Set db = Currentdb
> >Set rs = db.OpenRecordset("tblTest")
> >rs.AddNew
> >Debug.Print rs![ID]
> >rs.Update
> >Debug.Print rs![ID]
> >rs.MoveLast
> >Debug.Print rs![ID]
> >rs.Close
> >End Sub

> >I get the correct value for ID for the first and third Debug.Prints, but
> >just after the .Update, the value changes to an already existing previous
> >record. Why is that? Shouldn't the current record still be the same after
a
> >.Update unless I move to another record? Why do I have to do a .MoveLast
to
> >point to the record I just updated?

> >Thanks for any help,
> >Dom



Mon, 25 Aug 2003 01:26:17 GMT  
 Recordset value changes with .update

Quote:
> What you said makes some sense, I'll have to check into this further.
> However, Access did move to it un Access 97 as this code has been
installed
> (without the .MoveLast) for over 2 years and broke when updated to Access
> 2000.

I find that surprising, though I've never tested it out myself, as the
Access 97 help file has this to say about the use of Recordset.AddNew:

------ start of quote from help file ------
The record that was current before you used AddNew remains current. If you
want to make the new record current, you can set the Bookmark property to
the bookmark identified by the LastModified property setting.
------ end of quote from help file ------

I'm using Access 97 myself, but I've never had occasion to test whether the
help file is correct in this case.

--

Dirk Goldgar
(remove NOSPAM from reply address)



Mon, 25 Aug 2003 05:42:27 GMT  
 Recordset value changes with .update
According to the help on the Addnew method (Access 2000) the record
that was current before the AddNew remains current. To go the new
record, set the bookmark using the LastModified property of the
recordset.

with rst
    .addnew
    !field2="stuff"
    .update
    .bookmark=.lastmodified
     msgbox "New id=" & !IdField
end with

--

Sandra Daigle, Microsoft Access MVP


Quote:
> If a table has an autonumber ID field and the following code segment
is run
> in Access 2000:
> Public Sub test()
> Set db = Currentdb
> Set rs = db.OpenRecordset("tblTest")
> rs.AddNew
> Debug.Print rs![ID]
> rs.Update
> Debug.Print rs![ID]
> rs.MoveLast
> Debug.Print rs![ID]
> rs.Close
> End Sub

> I get the correct value for ID for the first and third Debug.Prints,
but
> just after the .Update, the value changes to an already existing
previous
> record. Why is that? Shouldn't the current record still be the same
after a
> .Update unless I move to another record? Why do I have to do a
.MoveLast to
> point to the record I just updated?

> Thanks for any help,
> Dom



Tue, 26 Aug 2003 04:14:31 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Updating RecordSet with values of a second RecordSet

2. open recordset, change connection, update recordset?

3. need help on getting the auto-increment value of a field BEFORE updating recordset

4. Updating Timestamp value in an ADO Recordset

5. Changes to Display values w/o changing database values

6. Variable and field value changes after .Update in VBA

7. When value changes, update other field with current date

8. MSHFlexgrid not updating when recordset changes

9. MSHFlexgrid not updating when recordset changes

10. Changing values in a recordset

11. Problem updating a recordset after changing the data in a formatted databound textbox

12. Change Date values in a Recordset Coulmn

 

 
Powered by phpBB® Forum Software