請各位看清楚﹕
Acess中有個字段id是自動編號的。我現在要查它最后的一條編號的下一條自動編號﹐報出來。請不要用select Max(id) from tablename.
因為如果有40條記錄﹐按說現在增加進去編號為41﹐但是在加之前往前刪除31_40號記錄﹐所以最后記錄號碼為30﹐這樣查會出錯。也請不要告訴用SELECT IDENT_CURRENT('TABLENAME') ,這只對SQL有用﹐對Acess無用.....
Acess中有個字段id是自動編號的。我現在要查它最后的一條編號的下一條自動編號﹐報出來。請不要用select Max(id) from tablename.
因為如果有40條記錄﹐按說現在增加進去編號為41﹐但是在加之前往前刪除31_40號記錄﹐所以最后記錄號碼為30﹐這樣查會出錯。也請不要告訴用SELECT IDENT_CURRENT('TABLENAME') ,這只對SQL有用﹐對Acess無用.....
<%@ 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>
我用 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
conn.begintrans
rs.addnew
... ...
rs.update
rs.open "select max id as Maxid from table"
... ...
conn.committrans
真是的.....
反正最终你是要往表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这样要么一起执行,要么都不执行
'利用降序方法
jiangsheng(蒋晟.Net) 這段asp是用 book﹐但是效果不是很好﹐
總之﹐各位﹐謝謝了。