Insert a record in both MSSQL and access databases to obtain the id value of the automatic number immediately.

Source: Internet
Author: User

This document Article This article will explain how to update or add a record in ms SQL Server to get the value of its identity column immediately. This value is well known as the automatic number of access.
ID value. Well, I will not talk much about it, Code I don't want to write much. I only want to write the key words. I believe you can understand it at a glance. The method obtained in access is also included later.
There are a lot of articles on the Internet, but here is a summary.
Set testrs = server. Createobject ("ADODB. recordset") 'create the object to be tested
Testrs. Open "[testtable]", Conn, 1, 2 'assume that the conn has been created and the database has been connected.
Testrs. addnew
Testrs ("colname1") = "colname1"
Testrs ("colname2") = "colname2"
......
Testrs ("colnamen") = "colnamen"
Testrs. Update' call the update method to immediately write data in the memory to the database. The following sentence is critical.
Testrs. movelast will move the last record
Id = testrs ("ID") 'so that you can immediately get the automatic number of the new record.
Finally, do not forget to close the object.
Testrs. Close: Set testrs = nothing
Hehe, the above method I have been in Windows 2000 Server SP4 + ms SQL SERVER + ASP/Vb/Delphi and so on have been tested, are feasible. Because this article is intended for some basic people, the above Code cannot be directly run. If you have any questions about the above Code, please contact me: QQ: 115269, or go to my web site for entertainment: http://www.772.cn/

How to insert a record in the ACCESS database and then get the id value of the automatic number immediately
Ensure that the bookmark attribute is supported in the way the record set is obtained, for example, 1, 3.

After inserting a record with an automatic number field, obtain the bookmark attribute value of the record.

Temp = Rs. bookmark

Then

Rs. Bookmark = temp

Try it !!

Response. Write RS ("ID ").

By the way, I would like to explain what a bookmark attribute is, because there are some articles on the Internet that explain this :)

Returns the bookmarks that uniquely identify the current record in the recordset object (we can use this attribute to return its automatic number after adding a new record in access ), you can also set the current record of the recordset object to the record identified by the valid bookmarks.

Set and return values

Sets or returns a Variant Expression for calculating valid bookmarks.

Description

You can use the bookmark attribute to save the location of the current record and return it to the record at any time. Bookmarks can only be used in recordset objects that support bookmarks.

Each record of A recordset object has a unique bookmarks. To save the bookmarks of the current record, assign the value of the bookmark attribute to a variable. To quickly return to the record after moving to another record, set the bookmark attribute of the recordset object to the value of the variable.

You may not be able to view the value of the bookmarks or compare the bookmarks directly (the values of the two bookmarks pointing to the same record may be different ).

If you use the clone method to create a copy of the recordset, the original and copied recordset object bookmark attributes are set to the same and can be replaced. However, you cannot replace bookmarks that use different recordset objects, even if these bookmarks are created using the same data source or command.

When the remote data service is used on the client (Ador) recordset object, the bookmark attribute is always valid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.