文章編號: 221931 - 上次校閱: 2007年1月24日 - 版次: 3.3

如何傳回插入至 Access 資料庫的記錄的自動編號值

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
全部展開 | 全部摺疊

結論

當新的資料錄加入至包含自動編號欄位的 ActiveX 資料物件 (ADO) 資料錄集時,立即報告插入資料錄的 [自動編號] 欄位的值傳回值為 0。

本文將告訴您,如何擷取資料錄插入至 Access 資料庫與 ADO AddNew() 及 Update() 方法的自動編號欄位的自動產生的值。這個範例使用 VBScript 動態伺服器網頁 (ASP) 頁面中。

其他相關資訊

下列程式碼假設在您電腦上具有 Adventure Works 資料庫,而且您有一個 ODBC 資料來源名稱 (DSN) 名為 AdvWorks 指向它。

這個範例也包括檔案可在 \Program Files\Common Files\System\Ado 中找到的 Adovbs.inc。

這個程式碼不會提示使用者輸入的任何輸入。它 Adventure Works 資料庫的 [客戶] 資料表中插入一筆記錄,並顯示新的資料錄的自動編號欄位 ([客戶編號]) 的值。

以下是要注意的一些關鍵項目:
  • [CursorLocation 必須 adUseClient。存取並不支援伺服器端資料指標。
  • 為了要填入 [自動編號] 欄位與自動產生的值,必須使用 Requery 方法能查詢資料錄集。
  • Requery 方法會將 absolutePosition (書籤) 從新插入的資料錄重設 requeried 資料錄集的第一筆記錄。 如此一來 absolutePosition 必須會呼叫 Requery,之前先儲存並且還原新插入的資料錄,在呼叫 Requery 後。
<%@ Language=VBScript %>
<HTML>
<BODY>

<!--#include file=adovbs.inc -->

<% 
Set objConn = CreateObject("ADODB.Connection")
Set objRS = CreateObject("ADODB.Recordset")

objConn.Open "DSN=advworks;"

'Access does not support a cursor engine so a client cursor must be used
objRS.CursorLocation = adUseClient

objRS.Open "SELECT * FROM Customers", objConn, adOpenStatic, adLockOptimistic

     ' when you invoke the method AddNew it adds a new record to the end of           
     ' your current recordset and places your cursor on that record.
     
objRS.AddNew
	objRS("CompanyName") = "Microsoft"
	objRS("ContactFirstName") = "Bob"
	objRS("ContactLastName") = "Smith"
objRS.Update

     ' when you invoke the method Update, it updates the database with the
     ' values of the new record that we just created.  To retrieve the 
     ' value of the Autonumber field we need to update the ADO recordset that
     ' currently have.

     ' When you do a Requery on your recordset, you lose your cursor.  So
     ' we need to store the location before we do the Requery, then reset
     ' it after the Requery.

'before the requery, the Autonumber field shows as 0
Response.Write "<br>ID before Requery = " & objRS("CustomerID")   

bookmark = objRS.absolutePosition  ' First, store the location of you cursor
objRS.Requery                      ' Next, update your recordset with the data from the database

'after the requery, the absolutePosition is the first record of the recordset
Response.Write "<br>ID before setting absolutePosition = " & objRS("CustomerID")

objRS.absolutePosition = bookmark  ' Finally, change your cursor back

'now we have the Autonumber value
Response.Write "<P>Added ID = " & objRS("CustomerID")

objRS.Close
objConn.Close
set objConn = nothing
set objRS = nothing
%>
</BODY>
</HTML>

				

?考

如需有關使用 AutoIncrement 欄位和 ADO 的詳細資訊,請參閱 「 Microsoft 知識庫 」 中下列文:
195910? (http://support.microsoft.com/kb/195910/EN-US/ ) 在 ADO 或 RDS 的資訊: 識別 (AutoIncrement) 資料行

這篇文章中的資訊適用於:
  • Microsoft ActiveX Data Objects 1.5
  • Microsoft ActiveX Data Objects 2.0
  • Microsoft Internet Information Server 3.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
關鍵字:?
kbmt kbdatabase kbfaq kbhowto kbinetdev KB221931 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:221931? (http://support.microsoft.com/kb/221931/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。