請各位看清楚﹕
Acess中有個字段id是自動編號的。我現在要查它最后的一條編號的下一條自動編號﹐報出來。請不要用select Max(id) from tablename.
因為如果有40條記錄﹐按說現在增加進去編號為41﹐但是在加之前往前刪除31_40號記錄﹐所以最后記錄號碼為30﹐這樣查會出錯。也請不要告訴用SELECT  IDENT_CURRENT('TABLENAME')  ,這只對SQL有用﹐對Acess無用.....

解决方案 »

  1.   

    ASP的,将就看吧
    <%@ 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 = adUseClientobjRS.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")   book = 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 = book  ' 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>
      

  2.   

    我在写登陆界面的时候也遇到同样的问题
    我用 insert into 来添加一个新的人员,但是该人员的password字段值是用这条记录的自动编号字段的值+密码 这样的格式来存储的,也就是说我必须先添加,然后用 update 来设置密码,就遇到了和你相同的问题。我的办法是:在insert into 的同时将你自己生成的一个uid存储在新纪录的 password 里面,在 update 的时候只要找到这段值就可以了。
        以下是我的代码
        DoCmd.SetWarnings False
        Dim strGUID As String
        strGUID = CreateGUID   '建立一个GUID 
        '建立GUID有很多方法,你甚至可以建立一个你自己的GUID:  日期+时间+人员姓名+4位数的随机数字
        Debug.Print strGUID
        DoCmd.RunSQL "INSERT INTO tbl_family ( name, pwd ) SELECT '" & text4.Value & "' AS 表达式1, '" & strGUID & "' AS 表达式2"
        Dim strUID As String
        strUID = Trim(str(DLookup("id", "tbl_family", "name='" & text4.Value & "' and pwd='" & strGUID & "'")))
        DoCmd.RunSQL "UPDATE tbl_family SET tbl_family.pwd = md5('" & strUID & "|" & Text6.Value & "') WHERE tbl_family.id=" & strUID    DoCmd.SetWarnings True
     
      

  3.   

    呵呵。這個想法我也有過﹐包括嘗試放棄自動編號。其實我之所以查它是因為我要在這個表沒有更新之前把即將要寫的ID號碼寫入另外一個表。二表的關聯就是ID號碼相同。但是表一是關鍵字﹐表二不取任何關鍵字。它的識別碼就是表一的關鍵字。偶都想是不是做個假記錄了。而在此之前不能做任何插入﹐也就是講表二插完才能插表一。so在看有什么好法子....
      

  4.   

    我的方法是
    conn.begintrans
      rs.addnew 
        ... ...
      rs.update
      rs.open "select max id as Maxid from table"
        ... ...
    conn.committrans
      

  5.   

    樓上的倒扣你300分﹐看清楚哦﹐不能先加﹐也不能max(id)
    真是的.....
      

  6.   

    你将两个操作放在事务中即可呀
    反正最终你是要往表1中加数据的如:
    conn.begintrans
      rs.addnew 
        ... ...    
      rs.update
      rs.open "select max(id) as Maxid from table1"
      intMax=rs!maxid
      cn.execute "insert into table2(id1,name) select "&intMax &",'yoki'"  '表二插完插表1    ... ...
      ...........
    conn.committrans这样要么一起执行,要么都不执行
      

  7.   

    自动编号有其弊端,实在难处理的话,建议自动生成id列,可以用日期时间来identify
      

  8.   

    select top 1 id from yourtable order by id desc
    '利用降序方法
      

  9.   

    謝謝回貼各位﹐ yoki(小马哥) 的事務這個方法是可行的﹐呵呵﹑
     jiangsheng(蒋晟.Net) 這段asp是用 book﹐但是效果不是很好﹐
    總之﹐各位﹐謝謝了。